diff --git a/.circleci/images/primary/Dockerfile b/.circleci/images/primary/Dockerfile index ceaf93b..98f4a97 100644 --- a/.circleci/images/primary/Dockerfile +++ b/.circleci/images/primary/Dockerfile @@ -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 \ diff --git a/README.md b/README.md index 763b6bb..1c6e791 100644 --- a/README.md +++ b/README.md @@ -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)) diff --git a/examples/screen/main.go b/examples/screen/main.go index 6ea37f8..9e8ebe7 100644 --- a/examples/screen/main.go +++ b/examples/screen/main.go @@ -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") } } diff --git a/robotgo.go b/robotgo.go index b7b6888..7fcca4c 100644 --- a/robotgo.go +++ b/robotgo.go @@ -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) diff --git a/robotgo_test.go b/robotgo_test.go index 82d0e9e..a9bcc4c 100644 --- a/robotgo_test.go +++ b/robotgo_test.go @@ -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) diff --git a/screen.go b/screen.go index 1e23bba..5062b11 100644 --- a/screen.go +++ b/screen.go @@ -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)