robotgo/examples/bitmap/main.go

186 lines
4.8 KiB
Go
Raw Permalink Normal View History

2018-02-01 16:07:44 +00:00
// Copyright 2016 The go-vgo Project Developers. See the COPYRIGHT
2017-05-25 14:25:28 +00:00
// file at the top-level directory of this distribution and at
// https://github.com/go-vgo/robotgo/blob/master/LICENSE
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
package main
import (
"fmt"
2018-03-17 08:59:25 +00:00
"log"
2017-05-25 14:25:28 +00:00
"github.com/go-vgo/robotgo"
2018-03-17 08:59:25 +00:00
"github.com/vcaesar/imgo"
2017-05-25 14:25:28 +00:00
// "go-vgo/robotgo"
)
2018-09-30 18:58:48 +00:00
func toBitmap(bmp robotgo.CBitmap) {
bitmap := robotgo.ToMMBitmapRef(bmp)
2017-05-25 14:25:28 +00:00
2018-01-18 13:39:27 +00:00
gbit := robotgo.ToBitmap(bitmap)
fmt.Println("go bitmap", gbit, gbit.Width)
2018-06-24 15:45:11 +00:00
cbit := robotgo.ToCBitmap(gbit)
// defer robotgo.FreeBitmap(cbit)
2018-06-24 15:45:11 +00:00
log.Println("cbit == bitmap: ", cbit == bitmap)
robotgo.SaveBitmap(cbit, "tocbitmap.png")
2018-09-30 18:58:48 +00:00
}
func findColor(bmp robotgo.CBitmap) {
bitmap := robotgo.ToMMBitmapRef(bmp)
2018-06-24 15:45:11 +00:00
2018-05-08 15:26:07 +00:00
// find the color in bitmap
2017-09-14 14:47:21 +00:00
color := robotgo.GetColor(bitmap, 1, 2)
fmt.Println("color...", color)
cx, cy := robotgo.FindColor(robotgo.CHex(color), bitmap, 1.0)
fmt.Println("pos...", cx, cy)
cx, cy = robotgo.FindColor(robotgo.CHex(color))
fmt.Println("pos...", cx, cy)
cx, cy = robotgo.FindColor(0xAADCDC, bitmap)
fmt.Println("pos...", cx, cy)
cx, cy = robotgo.FindColor(0xAADCDC, nil, 0.1)
fmt.Println("pos...", cx, cy)
cx, cy = robotgo.FindColorCS(0xAADCDC, 388, 179, 300, 300)
2017-09-18 13:00:22 +00:00
fmt.Println("pos...", cx, cy)
2017-09-14 14:47:21 +00:00
cnt := robotgo.CountColor(0xAADCDC, bitmap)
2018-01-24 13:44:18 +00:00
fmt.Println("count...", cnt)
cnt1 := robotgo.CountColorCS(0xAADCDC, 10, 20, 30, 40)
2018-01-24 13:44:18 +00:00
fmt.Println("count...", cnt1)
2018-09-30 18:58:48 +00:00
}
2018-01-24 13:44:18 +00:00
2019-03-16 16:17:17 +00:00
func bitmapString(bmp robotgo.CBitmap) {
2018-09-30 18:58:48 +00:00
bitmap := robotgo.ToMMBitmapRef(bmp)
2017-08-24 12:42:46 +00:00
2017-07-02 03:52:37 +00:00
// creates bitmap from string by bitmap
2017-05-25 14:25:28 +00:00
bitstr := robotgo.TostringBitmap(bitmap)
fmt.Println("bitstr...", bitstr)
// sbitmap := robotgo.BitmapFromstring(bitstr, 2)
// fmt.Println("...", sbitmap)
2019-04-22 17:49:34 +00:00
// sbitmap := robotgo.BitmapStr(bitstr)
sbitmap := robotgo.BitmapFromStr(bitstr)
fmt.Println("bitmap str...", sbitmap)
robotgo.SaveBitmap(sbitmap, "teststr.png")
2019-03-16 16:17:17 +00:00
}
func bitmapTool(bmp robotgo.CBitmap) {
bitmap := robotgo.ToMMBitmapRef(bmp)
// bitmap := robotgo.CaptureScreen(10, 20, 30, 40)
abool := robotgo.PointInBounds(bitmap, 1, 2)
fmt.Println("point in bounds...", abool)
// returns new bitmap object created from a portion of another
bitpos := robotgo.GetPortion(bitmap, 10, 10, 11, 10)
fmt.Println(bitpos)
2017-05-25 14:25:28 +00:00
2017-07-02 03:52:37 +00:00
// saves image to absolute filepath in the given format
2017-05-25 14:25:28 +00:00
robotgo.SaveBitmap(bitmap, "test.png")
robotgo.SaveBitmap(bitmap, "test31.tif", 1)
2018-09-30 18:58:48 +00:00
}
2017-05-25 14:25:28 +00:00
2018-09-30 18:58:48 +00:00
func decode() {
2018-03-17 08:59:25 +00:00
img, name, err := robotgo.DecodeImg("test.png")
if err != nil {
log.Println("decode image ", err)
}
fmt.Println("decode test.png", img, name)
byt := robotgo.OpenImg("test.png")
imgo.Save("test2.png", byt)
2018-03-18 11:27:45 +00:00
w, h := robotgo.GetImgSize("test.png")
fmt.Println("image width and hight ", w, h)
w, h = imgo.GetSize("test.png")
fmt.Println("image width and hight ", w, h)
2017-07-02 03:52:37 +00:00
// convert image
robotgo.Convert("test.png", "test.tif")
2018-09-30 18:58:48 +00:00
}
func bitmapTest(bmp robotgo.CBitmap) {
bitmap := robotgo.ToMMBitmapRef(bmp)
bit := robotgo.CaptureScreen(1, 2, 40, 40)
defer robotgo.FreeBitmap(bit)
fmt.Println("CaptureScreen...", bit)
// searches for needle in bitmap
fx, fy := robotgo.FindBitmap(bit, bitmap)
fmt.Println("FindBitmap------", fx, fy)
// fx, fy := robotgo.FindBit(bitmap)
// fmt.Println("FindBitmap------", fx, fy)
fx, fy = robotgo.FindBitmap(bit)
fmt.Println("FindBitmap------", fx, fy)
}
2019-05-17 14:47:50 +00:00
func findBitmap(bmp robotgo.CBitmap) {
fx, fy := robotgo.FindBitmap(robotgo.ToMMBitmapRef(bmp))
fmt.Println("findBitmap: ", fx, fy)
fx, fy = robotgo.FindCBitmap(bmp)
fmt.Println("findCBitmap: ", fx, fy)
fx, fy = robotgo.FindCBitmap(bmp, nil, 0.1)
fmt.Println("findCBitmap: ", fx, fy)
// open image bitmap
openbit := robotgo.OpenBitmap("test.tif")
fmt.Println("openBitmap...", openbit)
fx, fy = robotgo.FindBitmap(openbit)
fmt.Println("FindBitmap------", fx, fy)
fx, fy = robotgo.FindPic("test.tif")
fmt.Println("FindPic------", fx, fy)
}
2018-09-30 18:58:48 +00:00
func bitmap() {
////////////////////////////////////////////////////////////////////////////////
// Bitmap
////////////////////////////////////////////////////////////////////////////////
// gets all of the screen
abitMap := robotgo.CaptureScreen()
fmt.Println("abitMap...", abitMap)
// gets part of the screen
bitmap := robotgo.CaptureScreen(100, 200, 30, 30)
defer robotgo.FreeBitmap(bitmap)
fmt.Println("CaptureScreen...", bitmap)
cbit := robotgo.CBitmap(bitmap)
toBitmap(cbit)
findColor(cbit)
count := robotgo.CountBitmap(abitMap, bitmap)
fmt.Println("count...", count)
bitmapTest(cbit)
findBitmap(cbit)
2019-03-16 16:17:17 +00:00
bitmapString(cbit)
2018-09-30 18:58:48 +00:00
bitmapTool(cbit)
decode()
// free the bitmap
robotgo.FreeBitmap(abitMap)
// robotgo.FreeBitmap(bitmap)
}
2018-07-13 11:23:09 +00:00
func main() {
bitmap()
}