-
Notifications
You must be signed in to change notification settings - Fork 8
/
example.go
90 lines (68 loc) · 1.78 KB
/
example.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"flag"
"log"
"runtime"
"time"
"local/adbbot"
)
var (
verbosity = flag.Int("v", 2, "verbosity")
ADB = flag.String("adb", "adb", "adb exec path")
DEV = flag.String("dev", "", "select device")
OnDevice = flag.Bool("od", true, "run on device")
APP = flag.String("app", "com.android.vending", "app package name")
TMPL = flag.String("tmpl", "tmpl.png", "template image")
)
func main() {
log.SetFlags(log.Ldate | log.Ltime)
flag.Parse()
runtime.GOMAXPROCS(runtime.NumCPU())
adbbot.Verbosity = *verbosity
bot := adbbot.NewLocalBot(*DEV, *ADB)
// run on android by adb user(shell)
bot.IsOnDevice = *OnDevice
Vlogln(2, "[adb]", "wait-for-device")
_, err := bot.Adb("wait-for-device")
if err != nil {
Vlogln(1, "adb err", err)
}
// press Home key
bot.KeyHome()
// start APP
bot.StartApp(*APP)
// wait
time.Sleep(time.Millisecond * 10000)
// create matching region between Point <100,635> and <9999,9999>
// reg := bot.NewRectAbs(100, 635, 9999, 9999)
// or All the screen (slow)
reg := bot.NewRectAll()
// create matching template
tmpl, err := bot.NewTmpl(*TMPL, reg)
if err != nil {
Vlogln(2, "load template image err", err)
} else {
// try to find target
// 10 times with 1000ms delay between each search
x, y, val := bot.FindExistReg(tmpl, 10, 1000)
if x == -1 && y == -1 {
Vlogln(2, "template not found", x, y, val)
} else {
Vlogln(2, "template found at", x, y, val)
}
}
infoname := time.Now().Format("20060102_150405")
err = bot.SaveScreen(infoname + ".png")
if err != nil {
Vlogln(2, "SaveScreen err", err)
} else {
Vlogln(2, "SaveScreen as file ", infoname+".png")
}
// force-stop APP
bot.KillApp(*APP)
}
func Vlogln(level int, v ...interface{}) {
if level <= *verbosity {
log.Println(v...)
}
}