Java實(shí)現(xiàn)圖片對(duì)比功能
之前用按鍵精靈寫過一些游戲輔助,里面有個(gè)函數(shù)叫FindPic,就上在屏幕范圍查找給定的一張圖片,返回查找到的坐標(biāo)位置。
現(xiàn)在,Java來實(shí)現(xiàn)這個(gè)函數(shù)類似的功能。
算法描述:
屏幕截圖,得到圖A,(查找的目標(biāo)圖片為圖B);
遍歷圖A的像素點(diǎn),根據(jù)圖B的尺寸,得到圖B四個(gè)角映射到圖A上的四個(gè)點(diǎn);
得到的四個(gè)點(diǎn)與圖B的四個(gè)角像素點(diǎn)的值比較。如果四個(gè)點(diǎn)一樣,執(zhí)行步驟4;否則,回到步驟2繼續(xù);
進(jìn)一步對(duì)比,將映射范圍內(nèi)的全部點(diǎn)與圖B全部的點(diǎn)比較。如果全部一樣,則說明圖片已找到;否則,回到步驟2繼續(xù);
這里,像素之間的比較是通過BufferedImage對(duì)象獲取每個(gè)像素的RGB值來比較的。如下,將BufferedImage轉(zhuǎn)換為int二維數(shù)組:
/** * 根據(jù)BufferedImage獲取圖片RGB數(shù)組 * @param bfImage * @return */ public static 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)獲取該點(diǎn)的顏色值是ARGB,而在實(shí)際應(yīng)用中使用的是RGB,所以需要將ARGB轉(zhuǎn)化成RGB,即bufImg.getRGB(w, h) & 0xFFFFFF。 result[h][w] = bfImage.getRGB(w, h) & 0xFFFFFF; } } return result; }
比較兩個(gè)像素點(diǎn)的RGB值是否相同,是通過異或操作比較的(據(jù)說比==效率更高),如果異或操作后得到的值為0,說明兩個(gè)像素點(diǎn)的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; import java.io.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; //查找目標(biāo)圖片 int scrShotImgWidth; //屏幕截圖寬度 int scrShotImgHeight; //屏幕截圖高度 int keyImgWidth; //查找目標(biāo)圖片寬度 int keyImgHeight; //查找目標(biāo)圖片高度 int[][] screenShotImageRGBData; //屏幕截圖RGB數(shù)據(jù) int[][] keyImageRGBData; //查找目標(biāo)圖片RGB數(shù)據(jù) int[][][] findImgData; //查找結(jié)果,目標(biāo)圖標(biāo)位于屏幕截圖上的坐標(biāo)數(shù)據(jù) public ImageFindDemo(String keyImagePath) { screenShotImage = this.getFullScreenShot(); keyImage = this.getBfImageFromPath(keyImagePath); screenShotImageRGBData = this.getImageGRB(screenShotImage); keyImageRGBData = this.getImageGRB(keyImage); scrShotImgWidth = screenShotImage.getWidth(); scrShotImgHeight = screenShotImage.getHeight(); keyImgWidth = keyImage.getWidth(); keyImgHeight = keyImage.getHeight(); //開始查找 this.findImage(); } /** * 全屏截圖 * @return 返回BufferedImage */ public BufferedImage getFullScreenShot() { BufferedImage bfImage = null; int width = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth(); int height = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight(); try { Robot robot = new Robot(); bfImage = robot.createScreenCapture(new Rectangle(0, 0, width, height)); } catch (AWTException e) { e.printStackTrace(); } return bfImage; } /** * 從本地文件讀取目標(biāo)圖片 * @param keyImagePath - 圖片絕對(duì)路徑 * @return 本地圖片的BufferedImage對(duì)象 */ public BufferedImage getBfImageFromPath(String keyImagePath) { BufferedImage bfImage = null; try { bfImage = ImageIO.read(new File(keyImagePath)); } catch (IOException e) { e.printStackTrace(); } return bfImage; } /** * 根據(jù)BufferedImage獲取圖片RGB數(shù)組 * @param bfImage * @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)獲取該點(diǎn)的顏色值是ARGB,而在實(shí)際應(yīng)用中使用的是RGB,所以需要將ARGB轉(zhuǎn)化成RGB,即bufImg.getRGB(w, h) & 0xFFFFFF。 result[h][w] = bfImage.getRGB(w, h) & 0xFFFFFF; } } return result; } /** * 查找圖片 */ public void findImage() { findImgData = new int[keyImgHeight][keyImgWidth][2]; //遍歷屏幕截圖像素點(diǎn)數(shù)據(jù) for(int y=0; y<scrShotImgHeight-keyImgHeight; y++) { for(int x=0; x<scrShotImgWidth-keyImgWidth; x++) { //根據(jù)目標(biāo)圖的尺寸,得到目標(biāo)圖四個(gè)角映射到屏幕截圖上的四個(gè)點(diǎn), //判斷截圖上對(duì)應(yīng)的四個(gè)點(diǎn)與圖B的四個(gè)角像素點(diǎn)的值是否相同, //如果相同就將屏幕截圖上映射范圍內(nèi)的所有的點(diǎn)與目標(biāo)圖的所有的點(diǎn)進(jìn)行比較。 if((keyImageRGBData[0][0]^screenShotImageRGBData[y][x])==0 && (keyImageRGBData[0][keyImgWidth-1]^screenShotImageRGBData[y][x+keyImgWidth-1])==0 && (keyImageRGBData[keyImgHeight-1][keyImgWidth-1]^screenShotImageRGBData[y+keyImgHeight-1][x+keyImgWidth-1])==0 && (keyImageRGBData[keyImgHeight-1][0]^screenShotImageRGBData[y+keyImgHeight-1][x])==0) { boolean isFinded = isMatchAll(y, x); //如果比較結(jié)果完全相同,則說明圖片找到,填充查找到的位置坐標(biāo)數(shù)據(jù)到查找結(jié)果數(shù)組。 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; } } } } } /** * 判斷屏幕截圖上目標(biāo)圖映射范圍內(nèi)的全部點(diǎn)是否全部和小圖的點(diǎn)一一對(duì)應(yīng)。 * @param y - 與目標(biāo)圖左上角像素點(diǎn)想匹配的屏幕截圖y坐標(biāo) * @param x - 與目標(biāo)圖左上角像素點(diǎn)想匹配的屏幕截圖x坐標(biāo) * @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(biggerY>=scrShotImgHeight || biggerX>=scrShotImgWidth) { return false; } xor = keyImageRGBData[smallerY][smallerX]^screenShotImageRGBData[biggerY][biggerX]; if(xor!=0) { return false; } } biggerX = x; } return true; } /** * 輸出查找到的坐標(biāo)數(shù)據(jù) */ 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(); } }
這種算法是精確比較,只要有一個(gè)像素點(diǎn)有差異,就會(huì)找不到圖片。當(dāng)然,如果想指定一個(gè)比較的精確度,我也有個(gè)思路,就是在算法步驟4比較映射范圍內(nèi)全部像素點(diǎn)的時(shí)候做個(gè)統(tǒng)計(jì),如果90%的點(diǎn)都相同,那就是說精確度是0.9。
另外,可能還要考慮效率問題,不過,我在我的應(yīng)用場(chǎng)景中并不太在意效率。如果有朋友看到這篇文章,對(duì)這個(gè)話題有更好的想法,請(qǐng)留言。
相關(guān)文章
SpringMVC中@controllerAdvice注解的詳細(xì)解釋
剛接觸SpringMVC應(yīng)該很少會(huì)見到這個(gè)注解,其實(shí)它的作用非常大,下面這篇文章主要給大家介紹了關(guān)于SpringMVC中@controllerAdvice注解的相關(guān)資料,需要的朋友可以參考下2022-02-02IDEA編譯時(shí)報(bào)常量字符串過長(zhǎng)的解決辦法
本文主要介紹了IDEA編譯時(shí)報(bào)常量字符串過長(zhǎng)的解決辦法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07java 將byte中的有效長(zhǎng)度轉(zhuǎn)換為String的實(shí)例代碼
下面小編就為大家?guī)硪黄猨ava 將byte中的有效長(zhǎng)度轉(zhuǎn)換為String的實(shí)例代碼。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11java.lang.Runtime.exec的左膀右臂:流輸入和流讀取詳解
這篇文章主要介紹了java.lang.Runtime.exec的左膀右臂:流輸入和流讀取詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Java設(shè)計(jì)模式之單例模式實(shí)例分析
這篇文章主要介紹了Java設(shè)計(jì)模式之單例模式,以實(shí)例形式較為詳細(xì)的分析了單例模式的概念、定義及簡(jiǎn)單實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-11-11