Skip to content

Commit

Permalink
Merge pull request #693 from go-vgo/bitmap-pr
Browse files Browse the repository at this point in the history
Update: update screen capture code and examples
  • Loading branch information
vcaesar authored Oct 7, 2024
2 parents 8db59aa + 78d0170 commit ecc260e
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .circleci/images/primary/Dockerfile
Original file line number Diff line number Diff line change
@@ -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 \
Expand Down
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
```
Expand Down Expand Up @@ -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))
Expand Down
14 changes: 10 additions & 4 deletions examples/screen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down
4 changes: 2 additions & 2 deletions robotgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion robotgo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
9 changes: 4 additions & 5 deletions screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
package robotgo

import (
"errors"
"image"

"github.com/kbinani/screenshot"
Expand All @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit ecc260e

Please sign in to comment.