change: FindScreenText return text rect

This commit is contained in:
lilong.129 2025-03-17 15:09:04 +08:00
parent 3e7e9b0ef9
commit 7f69052be6
5 changed files with 15 additions and 15 deletions

View File

@ -1 +1 @@
v5.0.0-beta-2503171436
v5.0.0-beta-2503171509

View File

@ -86,13 +86,10 @@ func (t OCRText) Size() types.Size {
}
func (t OCRText) Center() PointF {
return getRectangleCenterPoint(t.Rect)
}
func getRectangleCenterPoint(rect image.Rectangle) (point PointF) {
rect := t.Rect
x, y := float64(rect.Min.X), float64(rect.Min.Y)
width, height := float64(rect.Dx()), float64(rect.Dy())
point = PointF{
point := PointF{
X: x + width*0.5,
Y: y + height*0.5,
}

View File

@ -145,7 +145,7 @@ func (dExt *XTDriver) GetScreenTexts(opts ...option.ActionOption) (ocrTexts ai.O
return screenResult.Texts, nil
}
func (dExt *XTDriver) FindScreenText(text string, opts ...option.ActionOption) (point ai.PointF, err error) {
func (dExt *XTDriver) FindScreenText(text string, opts ...option.ActionOption) (textRect ai.OCRText, err error) {
options := option.NewActionOptions(opts...)
if options.ScreenShotFileName == "" {
opts = append(opts, option.WithScreenShotFileName(fmt.Sprintf("find_screen_text_%s", text)))
@ -155,16 +155,15 @@ func (dExt *XTDriver) FindScreenText(text string, opts ...option.ActionOption) (
return
}
result, err := ocrTexts.FindText(text, opts...)
textRect, err = ocrTexts.FindText(text, opts...)
if err != nil {
log.Warn().Msgf("FindText failed: %s", err.Error())
return
}
point = result.Center()
log.Info().Str("text", text).
Interface("point", point).Msgf("FindScreenText success")
return
Interface("textRect", textRect).Msgf("FindScreenText success")
return textRect, nil
}
func (dExt *XTDriver) FindUIResult(opts ...option.ActionOption) (point ai.PointF, err error) {

View File

@ -12,13 +12,14 @@ func (dExt *XTDriver) TapByOCR(text string, opts ...option.ActionOption) error {
opts = append(opts, option.WithScreenShotFileName(fmt.Sprintf("tap_by_ocr_%s", text)))
}
point, err := dExt.FindScreenText(text, opts...)
textRect, err := dExt.FindScreenText(text, opts...)
if err != nil {
if actionOptions.IgnoreNotFoundError {
return nil
}
return err
}
point := textRect.Center()
return dExt.TapAbsXY(point.X, point.Y, opts...)
}

View File

@ -47,8 +47,8 @@ func TestDriverExt(t *testing.T) {
driverExt.TapByOCR("推荐")
texts, _ := driverExt.GetScreenTexts()
t.Log(texts)
point, _ := driverExt.FindScreenText("hello")
t.Log(point)
textRect, _ := driverExt.FindScreenText("hello")
t.Log(textRect)
err := driverExt.TapByCV(
option.WithScreenShotUITypes("deepseek_send"),
@ -98,13 +98,14 @@ func TestDriverExt_FindScreenText(t *testing.T) {
func TestDriverExt_Seek(t *testing.T) {
driver := setupDriverExt(t)
point, err := driver.FindScreenText("首页")
textRect, err := driver.FindScreenText("首页")
assert.Nil(t, err)
size, err := driver.WindowSize()
assert.Nil(t, err)
width := size.Width
point := textRect.Center()
y := point.Y - 40
for i := 0; i < 5; i++ {
err := driver.Swipe(0.5, 0.8, 0.5, 0.2)
@ -180,4 +181,6 @@ func TestDriverExt_Action_Risk(t *testing.T) {
err = driver.Swipe(0.5, 0.5, 0.5, 0.9,
option.WithSwipeOffset(-50, 50, -50, 50))
assert.Nil(t, err)
err = driver.TapByOCR("首页", option.WithTapOffset(-10, 10))
}