gwda_ext_opencv

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2020 License: MIT Imports: 12 Imported by: 1

README

gwda-ext-opencv

electricbubble/gwda 的图片匹配扩展库。

安装

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

  • macOS 建议直接用 Homebrew 安装
go get -u github.com/electricbubble/gwda-ext-opencv

使用

package main

import (
	"fmt"
	. "github.com/electricbubble/gwda"
	extOpenCV "github.com/electricbubble/gwda-ext-opencv"
	"log"
)

func main() {
	client, err := NewUSBClient()
	checkErr(err)
	session, err := client.NewSession()
	checkErr(err)

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

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

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

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

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

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

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

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

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

	// WDADebug = true

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

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

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

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

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

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

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

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

	// sessionExt.Debug(extOpenCV.DmNotMatch)

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

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

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

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

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

func undo(sExt *extOpenCV.SessionExt, n int) {
	pathUndo := "/Users/hero/Documents/temp/2020-05/opencv/undo.png"
	err := sExt.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 SessionExt

type SessionExt struct {
	MatchMode TemplateMatchMode
	Threshold float64
	// contains filtered or unexported fields
}

func Extend

func Extend(session *gwda.Session, threshold float64, matchMode ...TemplateMatchMode) (sExt *SessionExt, err error)

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

func (*SessionExt) CloseMjpegStream

func (sExt *SessionExt) CloseMjpegStream()

func (*SessionExt) ConnectMjpegStream

func (sExt *SessionExt) ConnectMjpegStream(httpClient *http.Client, mjpegURL string) (err error)

func (*SessionExt) Debug

func (sExt *SessionExt) Debug(dm DebugMode)

func (*SessionExt) DoubleTap

func (sExt *SessionExt) DoubleTap(pathname string) (err error)

func (*SessionExt) DoubleTapOffset

func (sExt *SessionExt) DoubleTapOffset(pathname string, xOffset, yOffset float64) (err error)

func (*SessionExt) Drag

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

func (*SessionExt) DragFloat

func (sExt *SessionExt) DragFloat(pathname string, toX, toY float64, pressForDuration ...float64) (err error)

func (*SessionExt) DragOffset

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

func (*SessionExt) DragOffsetFloat

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

func (*SessionExt) FindAllImageRect

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

func (*SessionExt) FindImageRectInUIKit

func (sExt *SessionExt) FindImageRectInUIKit(search string) (x, y, width, height float64, err error)

func (*SessionExt) ForceTouch

func (sExt *SessionExt) ForceTouch(pathname string, pressure float64, duration ...float64) (err error)

func (*SessionExt) ForceTouchOffset

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

func (*SessionExt) GesturePassword

func (sExt *SessionExt) GesturePassword(pathname string, password ...int) (err error)

func (*SessionExt) MappingToRectInUIKit

func (sExt *SessionExt) MappingToRectInUIKit(rect image.Rectangle) (x, y, width, height float64)

func (*SessionExt) OnlyOnceMatchMode

func (sExt *SessionExt) OnlyOnceMatchMode(matchMode TemplateMatchMode) (newExt *SessionExt)

func (*SessionExt) OnlyOnceThreshold

func (sExt *SessionExt) OnlyOnceThreshold(threshold float64) (newExt *SessionExt)

func (*SessionExt) PerformActions

func (sExt *SessionExt) PerformActions(actions *gwda.WDAActions) error

func (*SessionExt) PerformTouchActions

func (sExt *SessionExt) PerformTouchActions(touchActions *gwda.WDATouchActions) error

func (*SessionExt) Swipe

func (sExt *SessionExt) Swipe(pathname string, toX, toY int) (err error)

func (*SessionExt) SwipeDown

func (sExt *SessionExt) SwipeDown(pathname string, distance ...float64) (err error)

func (*SessionExt) SwipeDownOffset

func (sExt *SessionExt) SwipeDownOffset(pathname string, xOffset, yOffset float64, distance ...float64) (err error)

func (*SessionExt) SwipeFloat

func (sExt *SessionExt) SwipeFloat(pathname string, toX, toY float64) (err error)

func (*SessionExt) SwipeLeft

func (sExt *SessionExt) SwipeLeft(pathname string, distance ...float64) (err error)

func (*SessionExt) SwipeLeftOffset

func (sExt *SessionExt) SwipeLeftOffset(pathname string, xOffset, yOffset float64, distance ...float64) (err error)

func (*SessionExt) SwipeOffset

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

func (*SessionExt) SwipeOffsetFloat

func (sExt *SessionExt) SwipeOffsetFloat(pathname string, toX, toY, xOffset, yOffset float64) (err error)

func (*SessionExt) SwipeRight

func (sExt *SessionExt) SwipeRight(pathname string, distance ...float64) (err error)

func (*SessionExt) SwipeRightOffset

func (sExt *SessionExt) SwipeRightOffset(pathname string, xOffset, yOffset float64, distance ...float64) (err error)

func (*SessionExt) SwipeUp

func (sExt *SessionExt) SwipeUp(pathname string, distance ...float64) (err error)

func (*SessionExt) SwipeUpOffset

func (sExt *SessionExt) SwipeUpOffset(pathname string, xOffset, yOffset float64, distance ...float64) (err error)

func (*SessionExt) Tap

func (sExt *SessionExt) Tap(pathname string) error

func (*SessionExt) TapOffset

func (sExt *SessionExt) TapOffset(pathname string, xOffset, yOffset float64) (err error)

func (*SessionExt) TapWithNumber

func (sExt *SessionExt) TapWithNumber(pathname string, numberOfTaps int) (err error)

TapWithNumber sends one or more taps

func (*SessionExt) TapWithNumberOffset

func (sExt *SessionExt) TapWithNumberOffset(pathname string, numberOfTaps int, xOffset, yOffset float64) (err error)

func (*SessionExt) TouchAndHold

func (sExt *SessionExt) TouchAndHold(pathname string, duration ...float64) (err error)

func (*SessionExt) TouchAndHoldOffset

func (sExt *SessionExt) TouchAndHoldOffset(pathname string, xOffset, yOffset float64, duration ...float64) (err error)

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