uixt

package module
v0.0.0-...-cf15959 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 27, 2022 License: MIT Imports: 19 Imported by: 0

README

gwda-ext

electricbubble/gwda 的扩展库,包括图片识别(OpenCV)、OCR 识别、辅助函数等。

安装

必须先安装好 OpenCV,安装步骤可参考 hybridgroup/gocv

go get github.com/debugtalk/gwda-ext

使用

package main

import (
	"fmt"
	. "github.com/electricbubble/gwda"
	gwdaExt "github.com/debugtalk/gwda-ext"
	"log"
)

func main() {
	driver, err := NewUSBDriver(nil)
	checkErr(err)

	driverExt, err := gwdaExt.Extend(driver, 0.95)
	checkErr(err, "扩展 driver ,指定匹配阀值为 95%(在不修改或者使用 `OnlyOnceThreshold` 的情况下)")

	pathZero := "/Users/hero/Documents/temp/2020-05/opencv/flag0.png"
	err = driverExt.Tap(pathZero)
	checkErr(err, "找到图片(匹配度 >= 95%)后点击(默认 x 向右👉偏移 50%, y 向下👇偏移 50%)")

	err = driverExt.TapOffset(pathZero, 0.1, 0.1)
	checkErr(err, "找到图片(匹配度 >= 95%)后点击( x 向右👉偏移 10%, y 向下👇偏移 10%)")

	pathSeven := "/Users/hero/Documents/temp/2020-05/opencv/flag7.png"
	err = driverExt.TapOffset(pathSeven, 0.2, 0.8)
	checkErr(err, "找到图片(匹配度 >= 95%)后点击( x 向右👉偏移 20%, y 向下👇偏移 80%)")

	err = driverExt.DoubleTap(pathSeven)
	checkErr(err, "找到图片(匹配度 >= 95%)后双击(默认 x 向右👉偏移 50%, y 向下👇偏移 50%)")

	err = driverExt.DoubleTapOffset(pathSeven, 0.1, 0.25)
	checkErr(err, "找到图片(匹配度 >= 95%)后点击( x 向右👉偏移 10%, y 向下👇偏移 25%)")

	pathSlash := "/Users/hero/Documents/temp/2020-05/opencv/flag.png"
	err = driverExt.SwipeDown(pathSlash, 0.5)
	checkErr(err, "向下👇滑动,滑动距离为图片高度的 50%(默认从图片的正中间顶部向底部滑动,默认滑动距离为 1个 图片高度)")

	err = driverExt.SwipeDownOffset(pathSlash, 0.25, 1)
	checkErr(err, "向下👇滑动( x 向右👉偏移 25%, y 向下👇偏移 100% )")

	err = driverExt.SwipeDownOffset(pathSlash, -0.25, -0.8)
	checkErr(err, "向下👇滑动( x 向左👈偏移 25%, y 向上👆偏移 80% )")

	// WDADebug = true

	// 撤销 3次 操作
	undo(driverExt, 3)

	err = driverExt.SwipeUp(pathSlash, 0.5)
	checkErr(err, "向上👆滑动,滑动距离为图片高度的 50%(默认从图片的正中间底部向顶部滑动,默认滑动距离为 1个 图片高度)")

	err = driverExt.SwipeUpOffset(pathSlash, 0.9, 0.6)
	checkErr(err, "向上👆滑动(起始滑动点 x 向右👉偏移 90%, y 向下👇偏移 60% )")

	err = driverExt.OnlyOnceThreshold(0.92).SwipeUpOffset(pathSlash, -0.1, -0.05, 0.3)
	checkErr(err, "向上👆滑动,临时指定匹配阀值为 92%,滑动距离为图片高度的 30%(起始滑动点 x 向左👈偏移 10%, y 向上👆偏移 5% )")

	// 撤销 3次 操作
	undo(driverExt, 3)

	err = driverExt.SwipeLeft(pathSlash, 0.5)
	checkErr(err, "向左👈滑动,滑动距离为图片宽度的 50%(默认从图片的正中间右侧向左侧滑动,默认滑动距离为 1个 图片宽度)")

	err = driverExt.SwipeLeftOffset(pathSlash, 0.5, 0.55)
	checkErr(err, "向左👈滑动(起始滑动点 x 向右👉偏移 50%, y 向下👇偏移 55% )")

	err = driverExt.OnlyOnceThreshold(0.92).SwipeLeftOffset(pathSlash, -0.15, -0.25)
	checkErr(err, "向左👈滑动,临时指定匹配阀值为 92%(起始滑动点 x 向左👈偏移 15%, y 向上👆偏移 25% )")

	// driverExt.Debug(gwdaExt.DmNotMatch)

	// 撤销 3次 操作
	undo(driverExt, 3)

	err = driverExt.SwipeRight(pathSlash, 0.5)
	checkErr(err, "向右👉滑动,滑动距离为图片宽度的 50%(默认从图片的正中间左侧向右侧滑动,默认滑动距离为 1个 图片宽度)")

	err = driverExt.SwipeRightOffset(pathSlash, 0.5, 0.6)
	checkErr(err, "向右👉滑动(起始滑动点 x 向右👉偏移 50%, y 向下👇偏移 60% )")

	err = driverExt.OnlyOnceThreshold(0.90).SwipeRightOffset(pathSlash, -0.25, -0.05)
	checkErr(err, "向右👉滑动(起始滑动点 x 向左👈偏移 25%, y 向上👆偏移 5% )")

	// 撤销 10次 操作
	undo(driverExt, 10)
}

func undo(dExt *gwdaExt.DriverExt, n int) {
	pathUndo := "/Users/hero/Documents/temp/2020-05/opencv/undo.png"
	err := dExt.TapWithNumber(pathUndo, n)
	checkErr(err, fmt.Sprintf("撤销 %d次 操作\n", n))
}

func checkErr(err error, msg ...string) {
	if err != nil {
		log.Fatalln(msg, err)
	}
}

示例代码运行预览

gwda-ext-opencv

手势密码

首先是抠出一张手势密码中的触摸点小图,如下图被圈中的其中一个

gesture-password

GesturePassword 函数会通过这个 触摸点小图 找到全部,并根据上图所示的进行排序。

比如,这里需要一个 M 的手势密码,根据排序后的索引值,我们只需要传入 []int{6, 3, 0, 4, 2, 5, 8} 可参考 gesture_test.go

这里就不放预览图了,觉得有兴趣的可以自己尝试下,这里也只是一个简单的应用方向。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DebugMode

type DebugMode int
const (
	// DmOff no output
	DmOff DebugMode = iota
	// DmEachMatch output matched and mismatched values
	DmEachMatch
	// DmNotMatch output only values that do not match
	DmNotMatch
)

type DriverExt

type DriverExt struct {
	gwda.WebDriver

	MatchMode TemplateMatchMode
	Threshold float64
	// contains filtered or unexported fields
}

func Extend

func Extend(driver gwda.WebDriver, threshold float64, matchMode ...TemplateMatchMode) (dExt *DriverExt, err error)

Extend 获得扩展后的 Driver, 并指定匹配阀值, 获取当前设备的 Scale, 默认匹配模式为 TmCcoeffNormed, 默认关闭 OpenCV 匹配值计算后的输出

func (*DriverExt) CloseMjpegStream

func (dExt *DriverExt) CloseMjpegStream()

func (*DriverExt) ConnectMjpegStream

func (dExt *DriverExt) ConnectMjpegStream(httpClient *http.Client) (err error)

func (*DriverExt) Debug

func (dExt *DriverExt) Debug(dm DebugMode)

func (*DriverExt) DoubleTap

func (dExt *DriverExt) DoubleTap(param interface{}) (err error)

func (*DriverExt) DoubleTapOffset

func (dExt *DriverExt) DoubleTapOffset(param string, xOffset, yOffset float64) (err error)

func (*DriverExt) Drag

func (dExt *DriverExt) Drag(pathname string, toX, toY int, pressForDuration ...float64) (err error)

func (*DriverExt) DragFloat

func (dExt *DriverExt) DragFloat(pathname string, toX, toY float64, pressForDuration ...float64) (err error)

func (*DriverExt) DragOffset

func (dExt *DriverExt) DragOffset(pathname string, toX, toY int, xOffset, yOffset float64, pressForDuration ...float64) (err error)

func (*DriverExt) DragOffsetFloat

func (dExt *DriverExt) DragOffsetFloat(pathname string, toX, toY, xOffset, yOffset float64, pressForDuration ...float64) (err error)

func (*DriverExt) FindAllImageRect

func (dExt *DriverExt) FindAllImageRect(search string) (rects []image.Rectangle, err error)

func (*DriverExt) FindImageRectInUIKit

func (dExt *DriverExt) FindImageRectInUIKit(search string) (x, y, width, height float64, err error)

func (*DriverExt) FindTextByOCR

func (dExt *DriverExt) FindTextByOCR(search string) (x, y, width, height float64, err error)

func (*DriverExt) FindUIElement

func (dExt *DriverExt) FindUIElement(param string) (ele gwda.WebElement, err error)

func (*DriverExt) FindUIRectInUIKit

func (dExt *DriverExt) FindUIRectInUIKit(search string) (x, y, width, height float64, err error)

func (*DriverExt) ForceTouch

func (dExt *DriverExt) ForceTouch(pathname string, pressure float64, duration ...float64) (err error)

func (*DriverExt) ForceTouchOffset

func (dExt *DriverExt) ForceTouchOffset(pathname string, pressure, xOffset, yOffset float64, duration ...float64) (err error)

func (*DriverExt) GesturePassword

func (dExt *DriverExt) GesturePassword(pathname string, password ...int) (err error)

func (*DriverExt) MappingToRectInUIKit

func (dExt *DriverExt) MappingToRectInUIKit(rect image.Rectangle) (x, y, width, height float64)

func (*DriverExt) OnlyOnceMatchMode

func (dExt *DriverExt) OnlyOnceMatchMode(matchMode TemplateMatchMode) (newExt *DriverExt)

func (*DriverExt) OnlyOnceThreshold

func (dExt *DriverExt) OnlyOnceThreshold(threshold float64) (newExt *DriverExt)

func (*DriverExt) PerformActions

func (dExt *DriverExt) PerformActions(actions *gwda.W3CActions) error

func (*DriverExt) PerformTouchActions

func (dExt *DriverExt) PerformTouchActions(touchActions *gwda.TouchActions) error

func (*DriverExt) ScreenShot

func (dExt *DriverExt) ScreenShot(fileName string) (string, error)

ScreenShot takes screenshot and saves image file to $CWD/screenshots/ folder

func (*DriverExt) Swipe

func (dExt *DriverExt) Swipe(pathname string, toX, toY int) (err error)

func (*DriverExt) SwipeDown

func (dExt *DriverExt) SwipeDown(pathname string, distance ...float64) (err error)

func (*DriverExt) SwipeDownOffset

func (dExt *DriverExt) SwipeDownOffset(pathname string, xOffset, yOffset float64, distance ...float64) (err error)

func (*DriverExt) SwipeFloat

func (dExt *DriverExt) SwipeFloat(pathname string, toX, toY float64) (err error)

func (*DriverExt) SwipeLeft

func (dExt *DriverExt) SwipeLeft(pathname string, distance ...float64) (err error)

func (*DriverExt) SwipeLeftOffset

func (dExt *DriverExt) SwipeLeftOffset(pathname string, xOffset, yOffset float64, distance ...float64) (err error)

func (*DriverExt) SwipeOffset

func (dExt *DriverExt) SwipeOffset(pathname string, toX, toY int, xOffset, yOffset float64) (err error)

func (*DriverExt) SwipeOffsetFloat

func (dExt *DriverExt) SwipeOffsetFloat(pathname string, toX, toY, xOffset, yOffset float64) (err error)

func (*DriverExt) SwipeRight

func (dExt *DriverExt) SwipeRight(pathname string, distance ...float64) (err error)

func (*DriverExt) SwipeRightOffset

func (dExt *DriverExt) SwipeRightOffset(pathname string, xOffset, yOffset float64, distance ...float64) (err error)

func (*DriverExt) SwipeUp

func (dExt *DriverExt) SwipeUp(pathname string, distance ...float64) (err error)

func (*DriverExt) SwipeUpOffset

func (dExt *DriverExt) SwipeUpOffset(pathname string, xOffset, yOffset float64, distance ...float64) (err error)

func (*DriverExt) Tap

func (dExt *DriverExt) Tap(param interface{}) error

func (*DriverExt) TapOffset

func (dExt *DriverExt) TapOffset(param string, xOffset, yOffset float64) (err error)

func (*DriverExt) TapWithNumber

func (dExt *DriverExt) TapWithNumber(param string, numberOfTaps int) (err error)

TapWithNumber sends one or more taps

func (*DriverExt) TapWithNumberOffset

func (dExt *DriverExt) TapWithNumberOffset(param string, numberOfTaps int, xOffset, yOffset float64) (err error)

func (*DriverExt) TouchAndHold

func (dExt *DriverExt) TouchAndHold(pathname string, duration ...float64) (err error)

func (*DriverExt) TouchAndHoldOffset

func (dExt *DriverExt) TouchAndHoldOffset(pathname string, xOffset, yOffset float64, duration ...float64) (err error)

type OCRResult

type OCRResult struct {
	Text   string  `json:"text"`
	Points []Point `json:"points"`
}

type OCRService

type OCRService interface {
	FindText(text string, imageBuf []byte) (rect image.Rectangle, err error)
}

type Point

type Point struct {
	X float32 `json:"x"`
	Y float32 `json:"y"`
}

type ResponseOCR

type ResponseOCR struct {
	Code      int         `json:"code"`
	Message   string      `json:"message"`
	OCRResult []OCRResult `json:"ocrResult"`
}

type TemplateMatchMode

type TemplateMatchMode int

TemplateMatchMode is the type of the template matching operation.

const (
	// TmSqdiff maps to TM_SQDIFF
	TmSqdiff TemplateMatchMode = iota
	// TmSqdiffNormed maps to TM_SQDIFF_NORMED
	TmSqdiffNormed
	// TmCcorr maps to TM_CCORR
	TmCcorr
	// TmCcorrNormed maps to TM_CCORR_NORMED
	TmCcorrNormed
	// TmCcoeff maps to TM_CCOEFF
	TmCcoeff
	// TmCcoeffNormed maps to TM_CCOEFF_NORMED
	TmCcoeffNormed
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL