前言
CSDN前陣子推送了篇文章,講的是微信跳一跳的技術實現,大致瀏覽,發現難度不高,很適合練手。
思路
ADB得到屏幕截圖,轉換成bitmap逐像素分析圖像,得到跳躍起點和終點坐標,最后ADB按壓屏幕進行跳躍
相關知識
ADB創建
·在https://adb.clockworkmod.com提前下載ADB
·通過 Process類 創建進程運行ADB
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指令
·讀取手機型號
Cmd("shell getprop ro.product.model");
·獲取屏幕截圖
Cmd(@"shell screencap -p/sdcard/1.png"); //屏幕截圖并保存Cmd(@"pull /sdcard/1.pngE:/adb"); //上傳文件
·按壓屏幕
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對象,寫一個遍歷像素點的循環
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("找不到棋子!初始化失敗!"); bitmap.Dispose(); return; }
底部坐標被正確的取了出來
完美!現在取出頂點和底部坐標!
觀察發現
·背景顏色為漸變色,所以橫向比較,與前一個點差別最大的點就是頂點
·平面顏色一般為純色,也可能是漸變色,所以得到頂點后作豎向比較,最后一個與前點 差別最大的點就是底部坐標
·頂點的y軸坐標在區間[650-1050]
首先寫一個判斷顏色相似度的方法
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); }
還是寫一個遍歷點的循環,調用顏色相似度方法作橫向比較取出頂點坐標和底部坐標
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("未知的物體!初始化失??!"); 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拋磚引玉 僅供學習!
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答