前言
csdn前陣子推送了篇文章,講的是微信跳一跳的技術實現,大致瀏覽,發現難度不高,很適合練手。
思路
adb得到屏幕截圖,轉換成bitmap逐像素分析圖像,得到跳躍起點和終點坐標,最后adb按壓屏幕進行跳躍
相關知識
adb創建
·在https://adb.clockworkmod.com提前下載adb
·通過 process類 創建進程運行adb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
process p = new process(); p.startinfo = new processstartinfo() { filename = @"e:\adb\adb.exe" , arguments = str, //要執行的命令 useshellexecute = false , //拒絕使用系統自帶的shell redirectstandardinput = true , //接受輸入 redirectstandardoutput = true , //接受輸出 redirectstandarderror = true , //接受錯誤 createnowindow = true , //不創建窗口 }; p.start(); string s = p.standardoutput.readtoend(); //讀取輸出 p.waitforexit(); |
常用adb指令
·讀取手機型號
1
|
cmd( "shell getprop ro.product.model" ); |
·獲取屏幕截圖
1
2
|
cmd( @"shell screencap -p/sdcard/1.png" ); //屏幕截圖并保存 cmd( @"pull /sdcard/1.pnge:\adb" ); //上傳文件 |
·按壓屏幕
1
2
|
cmd( string .format( "shellinput swipe {0} {1} {2} {3} {4}" , x0, y0, x1, y1, time)); //從0點點擊到1點持續time毫秒 |
adb算是搞定了,現在寫個界面,獲取屏幕截圖!
取棋子坐標思路
觀察發現
·棋子的顏色為固定值,逐取出棋子底部顏色為 rgb(55, 52,92)
·棋子的底部y軸坐標在區間[1000,1250]
實例化gitmap對象,寫一個遍歷像素點的循環
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
|
bitmap bitmap = new bitmap( @"e:\adb\1.png" ); pointchess =newpoint(); //棋子顏色 color.fromargb(55, 52, 92)) for ( int y = 1000; y < 1250;y++) { for ( int x = 0; x <bitmap.width; x++) { if (bitmap.getpixel(x,y) == color.fromargb(57, 58, 102)) { chess.x = x; chess.y = y; break ; } } if (chess != new point()) { break ; } } if (chess == new point()) { messagebox.show( "找不到棋子!初始化失?。?quot; ); bitmap.dispose(); return ; } |
底部坐標被正確的取了出來
完美!現在取出頂點和底部坐標!
觀察發現
·背景顏色為漸變色,所以橫向比較,與前一個點差別最大的點就是頂點
·平面顏色一般為純色,也可能是漸變色,所以得到頂點后作豎向比較,最后一個與前點 差別最大的點就是底部坐標
·頂點的y軸坐標在區間[650-1050]
首先寫一個判斷顏色相似度的方法
1
2
3
4
5
6
7
8
|
bool colorabout(colorcolor0, color color1) { int i = 20; //顏色差值 int r =math.max(color0.r,color1.r)- math.min(color0.r, color1.r); int g = math.max(color0.g,color1.g) - math.min(color0.g, color1.g); int b = math.max(color0.b,color1.b) - math.min(color0.b, color1.b); return !((math.max(math.max(r,g),b) + math.min(math.min(r, g), b)) > i); } |
還是寫一個遍歷點的循環,調用顏色相似度方法作橫向比較取出頂點坐標和底部坐標
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
|
point rectvertex = new point(); point rectend = new point(); for ( int y = 650; y < 1050;y++) { for ( int x = 1; x <bitmap.width; x++) { booliscolorabout = !colorabout(bitmap.getpixel(x - 1, y), bitmap.getpixel(x, y)); if ((x < chess.x - 75 || x > chess.x + 75)&& iscolorabout) //排除棋子坐標,避免錯誤的將棋子作頂點 { rectvertex.x = x; rectvertex.y = y; break ; } } if (rectvertex != new point()) { break ; } } if (rectvertex == new point()) { messagebox.show( "未知的物體!初始化失?。?quot; ); bitmap.dispose(); return ; } colorrectcolor = bitmap.getpixel(rectvertex.x,rectvertex.y+1); if (rectend == new point()) { for ( int y = rectvertex.y; y< 1200; y++) { booliscolorabout = colorabout(rectcolor, bitmap.getpixel(rectvertex.x, y)); if (iscolorabout) { rectend.x = rectvertex.x; rectend.y = y; } } } |
ok!取出了坐標剩下的就是計算距離(正好前幾天才學的兩點距離公式)和跳躍了!開始循環!
lanq 2017.1.6 github-wecharjump
拋磚引玉 僅供學習!
更多內容大家可以參考專題《微信跳一跳》進行學習。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/qq_15505341/article/details/78987317