From 336b7cea9fb8c7315ee4590b67ccb09603b4e7e4 Mon Sep 17 00:00:00 2001 From: vcaesar Date: Tue, 12 Oct 2021 12:03:50 -0400 Subject: [PATCH] add free bitmap array support and Update README.md --- README.md | 17 +++++++++++++++++ README_zh.md | 17 +++++++++++++++++ robotgo.go | 9 ++++++++- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8a5274d..1ff7b90 100644 --- a/README.md +++ b/README.md @@ -186,6 +186,7 @@ import ( "fmt" "github.com/go-vgo/robotgo" + "github.com/vcaesar/imgo" ) func main() { @@ -197,6 +198,13 @@ func main() { sx, sy := robotgo.GetScreenSize() fmt.Println("get screen size: ", sx, sy) + + bit := robotgo.CaptureScreen(10, 10, 30, 30) + defer robotgo.FreeBitmap(bit) + robotgo.SaveBimap(bit, "test_1.png") + + img := robotgo.ToImage(bit) + imgo.Save("test.png", img) } ``` @@ -248,6 +256,15 @@ func opencv() { defer robotgo.FindBitmap(bit) fmt.Print("find bitmap: ") fmt.Println(robotgo.FindBitmap(bit)) + + bit0 := robotgo.CaptureScreen() + img := robotgo.ToImage(bit0) + bit1 := robotgo.CaptureScreen(10, 10, 30, 30) + img1 := robotgo.ToImage(bit1) + defer robotgo.FreeBitmapArr(bit0, bit1) + + fmt.Print("gcv find image: ") + fmt.Println(gcv.FindImg(img1, img)) } ``` diff --git a/README_zh.md b/README_zh.md index 502a0f4..1660c6c 100644 --- a/README_zh.md +++ b/README_zh.md @@ -184,6 +184,7 @@ import ( "fmt" "github.com/go-vgo/robotgo" + "github.com/vcaesar/imgo" ) func main() { @@ -195,6 +196,13 @@ func main() { sx, sy := robotgo.GetScreenSize() fmt.Println("get screen size: ", sx, sy) + + bit := robotgo.CaptureScreen(10, 10, 30, 30) + defer robotgo.FreeBitmap(bit) + robotgo.SaveBimap(bit, "test_1.png") + + img := robotgo.ToImage(bit) + imgo.Save("test.png", img) } ``` @@ -246,6 +254,15 @@ func opencv() { defer robotgo.FindBitmap(bit) fmt.Print("find bitmap: ") fmt.Println(robotgo.FindBitmap(bit)) + + bit0 := robotgo.CaptureScreen() + img := robotgo.ToImage(bit0) + bit1 := robotgo.CaptureScreen(10, 10, 30, 30) + img1 := robotgo.ToImage(bit1) + defer robotgo.FreeBitmapArr(bit0, bit1) + + fmt.Print("gcv find image: ") + fmt.Println(gcv.FindImg(img1, img)) } ``` diff --git a/robotgo.go b/robotgo.go index 0a192ed..2b232d2 100644 --- a/robotgo.go +++ b/robotgo.go @@ -1215,12 +1215,19 @@ func Convert(opath, spath string, args ...int) { SaveBitmap(bitmap, spath, mtype) } -// FreeBitmap free and dealloc bitmap +// FreeBitmap free and dealloc the C bitmap func FreeBitmap(bitmap C.MMBitmapRef) { // C.destroyMMBitmap(bitmap) C.bitmap_dealloc(bitmap) } +// FreeBitmapArr free and dealloc the C bitmap array +func FreeBitmapArr(bit ...C.MMBitmapRef) { + for i := 0; i < len(bit); i++ { + FreeBitmap(bit[i]) + } +} + // ReadBitmap returns false and sets error if |bitmap| is NULL func ReadBitmap(bitmap C.MMBitmapRef) bool { abool := C.bitmap_ready(bitmap)