feat: add GetScreenShotBuffer, GetScreenShotBase64

This commit is contained in:
lilong.129
2025-03-19 18:16:00 +08:00
parent 7da305f577
commit ec93382e47
2 changed files with 26 additions and 5 deletions

View File

@@ -1 +1 @@
v5.0.0-beta-2503191748 v5.0.0-beta-2503191816

View File

@@ -2,6 +2,7 @@ package uixt
import ( import (
"bytes" "bytes"
"encoding/base64"
"fmt" "fmt"
"image" "image"
"image/gif" "image/gif"
@@ -45,10 +46,7 @@ func (s *ScreenResult) FilterTextsByScope(x1, y1, x2, y2 float64) ai.OCRTexts {
}) })
} }
// GetScreenResult takes a screenshot, returns the image recognition result func (dExt *XTDriver) GetScreenShotBuffer() (compressedBufSource *bytes.Buffer, err error) {
func (dExt *XTDriver) GetScreenResult(opts ...option.ActionOption) (screenResult *ScreenResult, err error) {
screenshotOptions := option.NewActionOptions(opts...)
// take screenshot // take screenshot
bufSource, err := dExt.ScreenShot() bufSource, err := dExt.ScreenShot()
if err != nil { if err != nil {
@@ -63,6 +61,29 @@ func (dExt *XTDriver) GetScreenResult(opts ...option.ActionOption) (screenResult
"compress screenshot failed %v", err) "compress screenshot failed %v", err)
} }
return compressBufSource, nil
}
func (dExt *XTDriver) GetScreenShotBase64() (base64Str string, err error) {
compressedBufSource, err := dExt.GetScreenShotBuffer()
if err != nil {
return "", err
}
base64Str = "data:image/jpeg;base64," +
base64.StdEncoding.EncodeToString(compressedBufSource.Bytes())
return base64Str, nil
}
// GetScreenResult takes a screenshot, returns the image recognition result
func (dExt *XTDriver) GetScreenResult(opts ...option.ActionOption) (screenResult *ScreenResult, err error) {
// get compressed screenshot buffer
compressBufSource, err := dExt.GetScreenShotBuffer()
if err != nil {
return nil, err
}
screenshotOptions := option.NewActionOptions(opts...)
// save compressed screenshot to file // save compressed screenshot to file
var fileName string var fileName string
optionsList := screenshotOptions.List() optionsList := screenshotOptions.List()