Merge pull request #693 from go-vgo/bitmap-pr

Update: update screen capture code and examples
This commit is contained in:
Evans 2024-10-07 14:00:37 -07:00 committed by GitHub
commit ecc260ea31
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 28 additions and 17 deletions

View File

@ -1,5 +1,5 @@
# FROM golang:1.10.1
FROM golang:1.22.3-stretch AS build
FROM golang:1.23.2-stretch AS build
# FROM govgo/go:1.11.1
RUN apt update && apt install -y --no-install-recommends \

View File

@ -268,13 +268,18 @@ func main() {
num := robotgo.DisplaysNum()
for i := 0; i < num; i++ {
robotgo.DisplayID = i
img1 := robotgo.CaptureImg()
img1, _ := robotgo.CaptureImg()
path1 := "save_" + strconv.Itoa(i)
robotgo.Save(img1, path1+".png")
robotgo.SaveJpeg(img1, path1+".jpeg", 50)
img2 := robotgo.CaptureImg(10, 10, 20, 20)
img2, _ := robotgo.CaptureImg(10, 10, 20, 20)
robotgo.Save(img2, "test_"+strconv.Itoa(i)+".png")
x, y, w, h := robotgo.GetDisplayBounds(i)
img3, err := robotgo.CaptureImg(x, y, w, h)
fmt.Println("Capture error: ", err)
robotgo.Save(img3, path1+"_1.png")
}
}
```
@ -354,8 +359,8 @@ func opencv() {
// bit1 := robotgo.CaptureScreen(10, 10, 30, 30)
// img1 := robotgo.ToImage(bit1)
// defer robotgo.FreeBitmapArr(bit0, bit1)
img := robotgo.CaptureImg()
img1 := robotgo.CaptureImg(10, 10, 30, 30)
img, _ := robotgo.CaptureImg()
img1, _ := robotgo.CaptureImg(10, 10, 30, 30)
fmt.Print("gcv find image: ")
fmt.Println(gcv.FindImg(img1, img))

View File

@ -29,23 +29,29 @@ func bitmap() {
gbitMap := robotgo.CaptureGo()
fmt.Println("Go CaptureScreen...", gbitMap.Width)
// fmt.Println("...", gbitmap.Width, gbitmap.BytesPerPixel)
// robotgo.SaveCapture("saveCapture.png", 10, 20, 100, 100)
robotgo.SaveCapture("saveCapture.png", 10, 20, 100, 100)
img := robotgo.CaptureImg()
img, err := robotgo.CaptureImg()
fmt.Println("error: ", err)
robotgo.Save(img, "save.png")
num := robotgo.DisplaysNum()
for i := 0; i < num; i++ {
robotgo.DisplayID = i
img1 := robotgo.CaptureImg()
img1, _ := robotgo.CaptureImg()
path1 := "save_" + strconv.Itoa(i)
robotgo.Save(img1, path1+".png")
robotgo.SaveJpeg(img1, path1+".jpeg", 50)
img2 := robotgo.CaptureImg(10, 10, 20, 20)
img2, _ := robotgo.CaptureImg(10, 10, 20, 20)
path2 := "test_" + strconv.Itoa(i)
robotgo.Save(img2, path2+".png")
robotgo.SaveJpeg(img2, path2+".jpeg", 50)
x, y, w, h := robotgo.GetDisplayBounds(i)
img3, err := robotgo.CaptureImg(x, y, w, h)
fmt.Println("Capture error: ", err)
robotgo.Save(img3, path2+"_1.png")
}
}

View File

@ -366,11 +366,11 @@ func CaptureGo(args ...int) Bitmap {
return ToBitmap(bit)
}
// CaptureImg capture the screen and return image.Image
// CaptureImg capture the screen and return image.Image, error
func CaptureImg(args ...int) (image.Image, error) {
bit := CaptureScreen(args...)
if bit == nil {
return nil, errors.New("capture error")
return nil, errors.New("Capture image not found.")
}
defer FreeBitmap(bit)

View File

@ -179,7 +179,8 @@ func TestImage(t *testing.T) {
err := SavePng(img, "robot_test.png")
tt.Nil(t, err)
img1 := CaptureImg(10, 10, 20, 20)
img1, err := CaptureImg(10, 10, 20, 20)
tt.Nil(t, err)
e := Save(img1, "robot_img.jpeg", 50)
tt.Nil(t, e)

View File

@ -11,7 +11,6 @@
package robotgo
import (
"errors"
"image"
"github.com/kbinani/screenshot"
@ -31,7 +30,7 @@ func GetDisplayRect(i int) Rect {
Size{W: w, H: h}}
}
// Capture capture the screenshot
// Capture capture the screenshot, use the CaptureImg default
func Capture(args ...int) (*image.RGBA, error) {
displayId := 0
if DisplayID != -1 {
@ -54,9 +53,9 @@ func Capture(args ...int) (*image.RGBA, error) {
// SaveCapture capture screen and save the screenshot to image
func SaveCapture(path string, args ...int) error {
img := CaptureImg(args...)
if img == nil {
return errors.New("Capture image not found")
img, err := CaptureImg(args...)
if err != nil {
return err
}
return Save(img, path)