private int[] findTargetEdge(int direction, int[,] cache, int [] start, LockBitmap lockbmp) { int[] ret = { 0, 0 }; int curX = start[0]; int curY = start[1]; while (curX > 0 && curX < lockbmp.Width - 1) { curX = curX + direction; int newY = -1; for (int y = 0; y < 7; y++) { if (cache[curX, curY + y - 1] == 1 && cache[curX, curY + y] != 1 && cache[curX, curY + y + 1] != 1 && cache[curX, curY + y + 2] != 1 && cache[curX, curY + y + 3] != 1 && cache[curX, curY + y + 4] != 1) { newY = curY + y; //lockbmp.SetPixel(curX, newY, Color.FromArgb(0, 0, 255)); //lockbmp.SetPixel(curX, newY + 1, Color.FromArgb(0, 0, 255)); //lockbmp.SetPixel(curX, newY - 1, Color.FromArgb(0, 0, 255)); break; } } if (newY == -1) { break; } curY = newY; } ret[0] = curX; ret[1] = curY; return(ret); }
private void processScreenshot() { System.Drawing.Image img; Bitmap bmp; LockBitmap lockbmp; try { img = System.Drawing.Image.FromFile("screenshot_jp.png"); bmp = new System.Drawing.Bitmap(img); lockbmp = new LockBitmap(bmp); img.Dispose(); lockbmp.LockBits(); } catch { this.stopAutoJump(); MessageBox.Show("打开图片时出错,请确认ADB已连接并工作正常"); return; } Color curColor; Color curBgColor; Color curBgShadow; Color figureColor = Color.FromArgb(54, 59, 99); int[,] cache = new int[lockbmp.Width, lockbmp.Height]; bool isTargetFound = false; bool isFigureFound = false; int[] targetStart = { 0, 0 }; int[] figure = { 0, 0 }; int figureWidth = (int)(lockbmp.Width * 76 / 1080); int figureHeight = (int)(lockbmp.Width * 210 / 1080); for (int j = (int)(lockbmp.Height * 0.7); j > (int)(lockbmp.Height * 0.25); j--) { for (int i = (int)(lockbmp.Width - 1); i > 0; i--) { curColor = lockbmp.GetPixel(i, j); if (this.calColorErr(curColor, figureColor) < 5) { isFigureFound = true; figure[0] = i; figure[1] = j - (int)(lockbmp.Width * 18 / 1080); break; } } if (isFigureFound) { break; } } for (int j = (int)(lockbmp.Height * 0.25); j < figure[1]; j++) { curBgColor = lockbmp.GetPixel(0, j); curBgShadow = Color.FromArgb((int)(curBgColor.R * 0.7), (int)(curBgColor.G * 0.7), (int)(curBgColor.B * 0.7)); for (int i = 1; i < (int)(lockbmp.Width - 1); i++) { curColor = lockbmp.GetPixel(i, j); if (i >= (figure[0] - figureWidth / 2) && i <= (figure[0] + figureWidth / 2) && j >= figure[1] - figureHeight && j <= figure[1]) { cache[i, j] = 1; } else if (this.isBgColor(curBgColor, curBgShadow, curColor)) { // 需要识别背景色和阴影颜色 // lockbmp.SetPixel(i,j,Color.FromArgb(255, 0, 0)); cache[i, j] = 1; } } } for (int j = (int)(lockbmp.Height * 0.25); j < figure[1]; j++) { for (int i = 1; i < (int)(lockbmp.Width - 1); i++) { if (cache[i, j] != 1 && cache[i, j + 1] != 1 && cache[i, j + 2] != 1 && cache[i, j + 3] != 1 && cache[i, j + 4] != 1) { isTargetFound = true; targetStart[0] = i; targetStart[1] = j; break; } } if (isTargetFound) { break; } } if (!isFigureFound || !isTargetFound) { lockbmp.UnlockBits(); this.pictureBox1.Image = (System.Drawing.Image)bmp; return; } int[] targetLeft = this.findTargetEdge(-1, cache, targetStart, lockbmp); int[] targetRight = this.findTargetEdge(1, cache, targetStart, lockbmp); int[] targetMid = { 0, 0 }; targetMid[0] = (int)((targetLeft[0] + targetRight[0]) / 2); targetMid[1] = (int)((targetLeft[1] + targetRight[1]) / 2); for (int i = -15; i < 16; i++) { for (int j = -15; j < 16; j++) { lockbmp.SetPixel(targetMid[0] + i, targetMid[1] + j, Color.FromArgb(0, 255, 0)); lockbmp.SetPixel(figure[0] + i, figure[1] + j, Color.FromArgb(255, 0, 0)); } } double distance = Math.Sqrt((targetMid[0] - figure[0]) * (targetMid[0] - figure[0]) + (targetMid[1] - figure[1]) * (targetMid[1] - figure[1])); int duration = (int)((distance * this.convertRatio) / lockbmp.Width); lockbmp.UnlockBits(); this.pictureBox1.Image = (System.Drawing.Image)bmp; string[] cmd = { "shell input swipe 300 800 300 800 " + duration }; this.sendADBCmd(cmd); }
private void processScreenshot() { System.Drawing.Image img = System.Drawing.Image.FromFile("screenshot_jp.png"); // System.Drawing.Image bmp = new System.Drawing.Bitmap(img); Bitmap bmp = new System.Drawing.Bitmap(img); LockBitmap lockbmp = new LockBitmap(bmp); img.Dispose(); Color curColor; lockbmp.LockBits(); // find bottom background color Color[] bottomColorArr = new Color[10]; for (int i = 0; i < 10; i++) { bottomColorArr[i] = lockbmp.GetPixel((int)(lockbmp.Width * i * 0.1), lockbmp.Height - 1); } int maxColorCount = 0; int maxColorIndex = 0; for (int i = 0; i < 10; i++) { int curCount = 0; for (int j = 0; j < 10; j++) { if (i == j) { continue; } if (this.calColorErr(bottomColorArr[i], bottomColorArr[j]) < 50) { curCount++; } } if (curCount > maxColorCount) { maxColorIndex = i; maxColorCount = curCount; } } Color topCorlor = lockbmp.GetPixel(3, 1); Color bottomColor = bottomColorArr[maxColorIndex]; int bgError = this.calColorErr(topCorlor, bottomColor); int[,] cache = new int[lockbmp.Width, lockbmp.Height]; Color figureColor = Color.FromArgb(54, 59, 99); Color figureHeadColor = Color.FromArgb(67, 58, 89); bool isTargetFound = false; bool isFigureFound = false; int targetStartX = 0; int targetStartY = 0; int figureX = 0; int figureY = 0; int figureWidth = (int)(lockbmp.Width * 90 / 1080); int figureHeight = (int)(lockbmp.Width * 250 / 1080); for (int j = (int)(lockbmp.Height * 0.7); j > (int)(lockbmp.Height * 0.25); j--) { for (int i = (int)(lockbmp.Width - 1); i > 0; i--) { curColor = lockbmp.GetPixel(i, j); if (this.calColorErr(curColor, figureColor) < 6) { isFigureFound = true; figureX = i; figureY = j - (int)(lockbmp.Width * 18 / 1080); break; } } if (isFigureFound) { break; } } for (int j = (int)(lockbmp.Height * 0.25); j < (int)(lockbmp.Height * 0.7); j++) { for (int i = (int)(1); i < (int)(lockbmp.Width - 1); i++) { curColor = lockbmp.GetPixel(i, j); if (i >= (figureX - figureWidth / 2) && i <= (figureX + figureWidth / 2) && j >= figureY - figureHeight && j <= figureY) { cache[i, j] = 1; } else if (this.isBgColor(topCorlor, bottomColor, curColor, bgError)) { // lockbmp.SetPixel(i,j,Color.FromArgb(255, 0, 0)); cache[i, j] = 1; } else if (!isTargetFound) { if (cache[i - 1, j] == 1 || cache[i, j - 1] == 1 || cache[i - 1, j - 1] == 1) { isTargetFound = true; targetStartX = i; targetStartY = j; } } } } if (!isFigureFound || !isTargetFound) { lockbmp.UnlockBits(); this.pictureBox1.Image = (System.Drawing.Image)bmp; return; } int curX = targetStartX; int curY = targetStartY; while (true) { curX = curX + 1; int newY = -1; for (int y = -4; y < 5; y++) { if (cache[curX, curY + y] != 1 && cache[curX, curY + y - 1] == 1) { newY = curY + y; break; } } if (newY == -1) { break; } curY = newY; } int midX = (curX + targetStartX) / 2; int midY = (curY + targetStartY) / 2; double angleOfLine = Math.Atan((double)(curY - targetStartY) / (curX - targetStartX)); double length = Math.Sqrt((curX - targetStartX) * (curX - targetStartX) + (curY - targetStartY) * (curY - targetStartY)) / 2; midX = (int)(midX - length * Math.Cos(angleOfLine)); midY = (int)(midY + length * Math.Sin(angleOfLine)); for (int i = -15; i < 16; i++) { for (int j = -15; j < 16; j++) { lockbmp.SetPixel(midX + i, midY + j, Color.FromArgb(0, 255, 0)); lockbmp.SetPixel(figureX + i, figureY + j, Color.FromArgb(255, 0, 0)); } } lockbmp.UnlockBits(); double distance = Math.Sqrt((midX - figureX) * (midX - figureX) + (midY - figureY) * (midY - figureY)) / lockbmp.Width; this.pictureBox1.Image = (System.Drawing.Image)bmp; string[] cmd = { "shell input swipe 300 800 300 800 " + (int)(distance * 1480) }; this.sendADBCmd(cmd); }