之前用按鍵精靈寫過一些遊戲輔助,裡面有個函數叫FindPic,就上在螢幕範圍查找給定的一張圖片,返回查找到的座標位置。
現在,Java來實作這個函數類似的函數。
演算法描述:
螢幕截圖,得到圖A,(查找的目標圖片為圖B);
遍歷圖A的像素點,根據圖B的尺寸,得到圖B四個角映射到圖A上的四個點;
所得的四個點與圖B的四個角像素點的值比較。如果四個點一樣,執行步驟4;否則,回到步驟2繼續;
進一步對比,將映射範圍內的全部點與圖B全部的點比較。如果全部一樣,則表示圖片已找到;否則,回到步驟2繼續;
這裡,像素之間的比較是透過BufferedImage物件取得每個像素的RGB值來比較的。如下,將BufferedImage轉換為int二維數組:
/** * 根據BufferedImage取得圖片RGB陣列* @param bfImage * @return */ public static int[][] getImageGRB(BufferedImage bfImage) { int width = bfImage.getWidth(); int height = int width = bfImage.getWidth(); int height = inta. int[][] result = new int[height][width]; for (int h = 0; h < height; h++) { for (int w = 0; w < width; w++) { //使用getRGB(w, h)取得該點的顏色值是ARGB,而實際應用中使用的是RGB,所以需要將ARGB轉換成RGB,也就是bufImg.getRGB(w, h) & 0xFFFFFF。 result[h][w] = bfImage.getRGB(w, h) & 0xFFFFFF; } } return result; }
比較兩個像素點的RGB值是否相同,是透過異或操作比較的(據說比==效率更高),如果異或操作後得到的值為0,表示兩個像素點的RGB一樣,否則不一樣。
下面附上演算法完整java程式碼:
package com.jebysun.test.imagefind; import java.awt.AWTException; import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.image.BufferedImage; .File; import java.io.IOException; import javax.imageio.ImageIO; /** * 螢幕上尋找指定圖片* @author Jeby Sun * @date 2014-09-13 * @website http://www.jebysun.com */ public class ImageFindDemo { BufferedImage screenShotImage; / /螢幕截圖BufferedImage keyImage; //找出目標圖片int scrShotImgWidth; //螢幕截圖寬度int scrShotImgHeight; //螢幕截圖高度int keyImgWidth; //找出目標圖片寬度int keyImgHeight; //找出目標圖片高度int[][] screenShotImageRGBData;螢幕截圖RGB資料數據高度int[][] screenShotImageRGBData;螢幕截圖RGB資料資料」[RGB資料] keyImageRGBData; //找出目標圖片RGB資料int[][][] findImgData; //找出結果,目標圖示位於螢幕截圖上的座標資料public ImageFindDemo(String keyImagePath) { screenShotImage = this.getFullScreenShot(); keyImage = this.getBfImageFromPath(keyImagePath); keyImage = this.getBfImageFromPath(keyImagePath); screenShotImageImageIh.m. keyImageRGBData = this.getImageGRB(keyImage); scrShotImgWidth = screenShotImage.getWidth(); scrShotImgHeight = screenShotImage.getHeight(); keyImage.getHeight(); //開始尋找this.findImage(); } /** * 全螢幕截圖* @return 回傳BufferedImage */ public BufferedImage getFullScreenShot() { BufferedImage bfImage = null; int width = (Image bfImage = null; int width = (Image) Toolkit.getDefaultToolkit().getScreenSize().getWidth(); int height = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight(); try { Robot robot = new Robot(); new Rectangle(0, 0, width, height)); } catch (AWTException e) { e.printStackTrace(); } return bfImage; } /** * 從本機檔案讀取目標圖片* @param keyImagePath - 圖片絕對路徑* @return 本機圖片的BufferedImage物件* / public BufferedImage getBfImageFromPath(String keyImagePath) { BufferedImage bfImage = null; try { bfImage = ImageIO.read(new File(keyImagePath)); } catch (IOException e) { e.printStackTrace(); } return bfImage; } /** * 根據BufferedImage @param bfImage @parambfImage; * @return */ public int[][] getImageGRB(BufferedImage bfImage) { int width = bfImage.getWidth(); int height = bfImage.getHeight(); int[][] result = new int[height][width]; for (int h = 0; h < height; h++) { for (int w = 0; w < width; w++) { //使用getRGB(w, h)取得該點的顏色值是ARGB,而在實際應用中使用的是RGB,所以需要將ARGB轉換成RGB,也就是bufImg.getRGB(w, h) & 0xFFFFFF。 result[h][w] = bfImage.getRGB(w, h) & 0xFFFFFF; } } return result; } /** * 找圖片*/ public void findImage() { findImgData = new int[keyImgHeight][keyImgWidth][keyImgWidth][keyImgWidth][keyImgWidth][keyImgWidth][keyImgWidth] 2]; //遍歷螢幕截圖素點資料for(int y=0; y<scrShotImgHeight-keyImgHeight; y++) { for(int x=0; x<scrShotImgWidth-keyImgWidth; x++) { //根據目標圖的尺寸,得到目標圖四個角落映射到螢幕截圖上的四個點, / /判斷截圖上對應的四個點與圖B的四個角像素點的值是否相同, //如果相同就將螢幕截圖上映射範圍內的所有的點與目標圖的所有的點進行比較。 if((keyImageRGBData[0][0]^screenShotImageRGBData[y][x])==0 && (keyImageRGBData[0][keyImgWidth-1]^screenShotImageRGBData[y][x+keyImgWidth-1]^0enShotImageRGBData[y][x+keyImgWidth-1])000 &&&&&&&&W (keyImageRGBData[keyImgHeight-1][keyImgWidth-1]^screenShotImageRGBData[y+keyImgHeight-1][x+keyImgWidth-1])==0 && (keyImageRGBData[keyImgHeight-1][04])==0 && (keyImageRGBData[keyImgHeight-1][30]^screen>ShotkeIh-1][304m-1][30]m-1][304m-1][304m-1][304m-1][30]4)4GB 1][x])==0) { boolean isFinded = isMatchAll(y, x); //如果比較結果完全相同,則表示圖片找到,填入查找到的位置座標資料到查找結果陣列。 if(isFinded) { for(int h=0; h<keyImgHeight; h++) { for(int w=0; w<keyImgWidth; w++) { findImgData[h][w][0] = y+h; findImgData[ h][w][1] = x+w; } } return; } } } } } /** *判斷螢幕截圖上目標圖映射範圍內的全部點是否全部和小圖的點一一對應。 * @param y - 與目標圖左上角像素點想匹配的螢幕截圖y座標* @param x - 與目標圖左上角像素點想匹配的螢幕截圖x座標* @return */ public boolean isMatchAll(int y, int x) { int biggerY = 0; int biggerX = 0; int xor = 0; for(int smallerY=0; smallerY<keyImgHeight; smallerY++) { biggerY = y+smallerY; for(int smallerX=0; smallerX<keyImgWidth; smallerX++) { biggerX = x+smallerX; if(biggerYYscr; } xor = keyImageRGBData[smallerY][smallerX]^screenShotImageRGBData[biggerY][biggerX]; if(xor!=0) { return false; } } biggerX = x; } return true; } /** * 輸出查找到的座標(資料*/ private void printFindData() { for(int y=0; y<keyImgHeight; y++) { for(int x=0; x<keyImgWidth; x++) { System.out.print("("+this.findImgData[y][x][0]+", "+this. findImgData[y][x][1]+")"); } System.out.println(); } } public static void main(String[] args) { String keyImagePath = "D:/key.png"; ImageFindDemo demo = new ImageFindDemo(keyImagePath); demo.printFindData(); } }
這種演算法是精確比較,只要有一個像素點有差異,就會找不到圖片。當然,如果想指定一個比較的精確度,我也有個思路,就是在演算法步驟4比較映射範圍內全部像素點的時候做個統計,如果90%的點都相同,那就是說精確度是0.9。
另外,可能還要考慮效率問題,不過,我在我的應用程式場景中並不太在意效率。如果有朋友看到這篇文章,對這個主題有更好的想法,請留言。