Skip to content

Commit a44bab1

Browse files
committed
refactor: Screencap and GetImage function return image.Image
1 parent b04646a commit a44bab1

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

controller.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package maa
66
*/
77
import "C"
88
import (
9+
"image"
910
"unsafe"
1011
)
1112

@@ -60,7 +61,7 @@ type Controller interface {
6061
PostScreencap() Job
6162

6263
Connected() bool
63-
GetImage() (ImageBuffer, bool)
64+
GetImage() (image.Image, bool)
6465
GetUUID() (string, bool)
6566
}
6667

@@ -235,10 +236,17 @@ func (c *controller) Connected() bool {
235236
}
236237

237238
// GetImage gets the image buffer of the last screencap request.
238-
func (c *controller) GetImage() (ImageBuffer, bool) {
239-
image := NewImageBuffer()
240-
got := C.MaaControllerGetImage(c.handle, C.MaaImageBufferHandle(image.Handle()))
241-
return image, got != 0
239+
func (c *controller) GetImage() (image.Image, bool) {
240+
imgBuffer := NewImageBuffer()
241+
defer imgBuffer.Destroy()
242+
got := C.MaaControllerGetImage(c.handle, C.MaaImageBufferHandle(imgBuffer.Handle()))
243+
img, err := imgBuffer.GetByRawData()
244+
245+
if err != nil {
246+
return nil, false
247+
}
248+
249+
return img, got != 0
242250
}
243251

244252
// GetUUID gets the UUID of the controller.

controller_custom.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ extern uint8_t _InputText(MaaStringView text, MaaTransparentArg handle_arg);
3838
*/
3939
import "C"
4040
import (
41+
"image"
4142
"unsafe"
4243
)
4344

@@ -52,7 +53,7 @@ type CustomControllerImpl interface {
5253
RequestUUID() (string, bool)
5354
StartApp(intent string) bool
5455
StopApp(intent string) bool
55-
Screencap() (ImageBuffer, bool)
56+
Screencap() (image.Image, bool)
5657
Click(x, y int32) bool
5758
Swipe(x1, y1, x2, y2, duration int32) bool
5859
TouchDown(contact, x, y, pressure int32) bool
@@ -140,10 +141,12 @@ func _StopAppAgent(intent string, handleArg C.MaaTransparentArg) C.uint8_t {
140141
func _ScreencapAgent(handleArg C.MaaTransparentArg, buffer C.MaaImageBufferHandle) C.uint8_t {
141142
ctrl := *(*CustomControllerImpl)(unsafe.Pointer(handleArg))
142143
img, ok := ctrl.Screencap()
143-
defer img.Destroy()
144144
if ok {
145145
imgBuffer := &imageBuffer{handle: buffer}
146-
imgBuffer.SetRawData(img.GetRawData(), img.GetWidth(), img.GetHeight(), img.GetType())
146+
err := imgBuffer.SetRawData(img)
147+
if err != nil {
148+
return C.uint8_t(0)
149+
}
147150
return C.uint8_t(1)
148151
}
149152
return C.uint8_t(0)

0 commit comments

Comments
 (0)