Skip to content

Commit ecc260e

Browse files
authored
Merge pull request #693 from go-vgo/bitmap-pr
Update: update screen capture code and examples
2 parents 8db59aa + 78d0170 commit ecc260e

File tree

6 files changed

+28
-17
lines changed

6 files changed

+28
-17
lines changed

.circleci/images/primary/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# FROM golang:1.10.1
2-
FROM golang:1.22.3-stretch AS build
2+
FROM golang:1.23.2-stretch AS build
33
# FROM govgo/go:1.11.1
44

55
RUN apt update && apt install -y --no-install-recommends \

README.md

+9-4
Original file line numberDiff line numberDiff line change
@@ -268,13 +268,18 @@ func main() {
268268
num := robotgo.DisplaysNum()
269269
for i := 0; i < num; i++ {
270270
robotgo.DisplayID = i
271-
img1 := robotgo.CaptureImg()
271+
img1, _ := robotgo.CaptureImg()
272272
path1 := "save_" + strconv.Itoa(i)
273273
robotgo.Save(img1, path1+".png")
274274
robotgo.SaveJpeg(img1, path1+".jpeg", 50)
275275

276-
img2 := robotgo.CaptureImg(10, 10, 20, 20)
276+
img2, _ := robotgo.CaptureImg(10, 10, 20, 20)
277277
robotgo.Save(img2, "test_"+strconv.Itoa(i)+".png")
278+
279+
x, y, w, h := robotgo.GetDisplayBounds(i)
280+
img3, err := robotgo.CaptureImg(x, y, w, h)
281+
fmt.Println("Capture error: ", err)
282+
robotgo.Save(img3, path1+"_1.png")
278283
}
279284
}
280285
```
@@ -354,8 +359,8 @@ func opencv() {
354359
// bit1 := robotgo.CaptureScreen(10, 10, 30, 30)
355360
// img1 := robotgo.ToImage(bit1)
356361
// defer robotgo.FreeBitmapArr(bit0, bit1)
357-
img := robotgo.CaptureImg()
358-
img1 := robotgo.CaptureImg(10, 10, 30, 30)
362+
img, _ := robotgo.CaptureImg()
363+
img1, _ := robotgo.CaptureImg(10, 10, 30, 30)
359364

360365
fmt.Print("gcv find image: ")
361366
fmt.Println(gcv.FindImg(img1, img))

examples/screen/main.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,29 @@ func bitmap() {
2929
gbitMap := robotgo.CaptureGo()
3030
fmt.Println("Go CaptureScreen...", gbitMap.Width)
3131
// fmt.Println("...", gbitmap.Width, gbitmap.BytesPerPixel)
32-
// robotgo.SaveCapture("saveCapture.png", 10, 20, 100, 100)
32+
robotgo.SaveCapture("saveCapture.png", 10, 20, 100, 100)
3333

34-
img := robotgo.CaptureImg()
34+
img, err := robotgo.CaptureImg()
35+
fmt.Println("error: ", err)
3536
robotgo.Save(img, "save.png")
3637

3738
num := robotgo.DisplaysNum()
3839
for i := 0; i < num; i++ {
3940
robotgo.DisplayID = i
40-
img1 := robotgo.CaptureImg()
41+
img1, _ := robotgo.CaptureImg()
4142
path1 := "save_" + strconv.Itoa(i)
4243
robotgo.Save(img1, path1+".png")
4344
robotgo.SaveJpeg(img1, path1+".jpeg", 50)
4445

45-
img2 := robotgo.CaptureImg(10, 10, 20, 20)
46+
img2, _ := robotgo.CaptureImg(10, 10, 20, 20)
4647
path2 := "test_" + strconv.Itoa(i)
4748
robotgo.Save(img2, path2+".png")
4849
robotgo.SaveJpeg(img2, path2+".jpeg", 50)
50+
51+
x, y, w, h := robotgo.GetDisplayBounds(i)
52+
img3, err := robotgo.CaptureImg(x, y, w, h)
53+
fmt.Println("Capture error: ", err)
54+
robotgo.Save(img3, path2+"_1.png")
4955
}
5056
}
5157

robotgo.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -366,11 +366,11 @@ func CaptureGo(args ...int) Bitmap {
366366
return ToBitmap(bit)
367367
}
368368

369-
// CaptureImg capture the screen and return image.Image
369+
// CaptureImg capture the screen and return image.Image, error
370370
func CaptureImg(args ...int) (image.Image, error) {
371371
bit := CaptureScreen(args...)
372372
if bit == nil {
373-
return nil, errors.New("capture error")
373+
return nil, errors.New("Capture image not found.")
374374
}
375375
defer FreeBitmap(bit)
376376

robotgo_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ func TestImage(t *testing.T) {
179179
err := SavePng(img, "robot_test.png")
180180
tt.Nil(t, err)
181181

182-
img1 := CaptureImg(10, 10, 20, 20)
182+
img1, err := CaptureImg(10, 10, 20, 20)
183+
tt.Nil(t, err)
183184
e := Save(img1, "robot_img.jpeg", 50)
184185
tt.Nil(t, e)
185186

screen.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
package robotgo
1212

1313
import (
14-
"errors"
1514
"image"
1615

1716
"github.com/kbinani/screenshot"
@@ -31,7 +30,7 @@ func GetDisplayRect(i int) Rect {
3130
Size{W: w, H: h}}
3231
}
3332

34-
// Capture capture the screenshot
33+
// Capture capture the screenshot, use the CaptureImg default
3534
func Capture(args ...int) (*image.RGBA, error) {
3635
displayId := 0
3736
if DisplayID != -1 {
@@ -54,9 +53,9 @@ func Capture(args ...int) (*image.RGBA, error) {
5453

5554
// SaveCapture capture screen and save the screenshot to image
5655
func SaveCapture(path string, args ...int) error {
57-
img := CaptureImg(args...)
58-
if img == nil {
59-
return errors.New("Capture image not found")
56+
img, err := CaptureImg(args...)
57+
if err != nil {
58+
return err
6059
}
6160

6261
return Save(img, path)

0 commit comments

Comments
 (0)