Java完成圖片比較功效。本站提示廣大學習愛好者:(Java完成圖片比較功效)文章只能為提供參考,不一定能成為您想要的結果。以下是Java完成圖片比較功效正文
之前用按鍵精靈寫過一些游戲幫助,外面有個函數叫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 = 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;
}
比擬兩個像素點的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;
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; //查找目的圖片
int scrShotImgWidth; //屏幕截圖寬度
int scrShotImgHeight; //屏幕截圖高度
int keyImgWidth; //查找目的圖片寬度
int keyImgHeight; //查找目的圖片高度
int[][] screenShotImageRGBData; //屏幕截圖RGB數據
int[][] keyImageRGBData; //查找目的圖片RGB數據
int[][][] findImgData; //查找成果,目的圖標位於屏幕截圖上的坐標數據
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;
}
/**
* 從當地文件讀取目的圖片
* @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獲得圖片RGB數組
* @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)獲得該點的色彩值是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][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])==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);
//假如比擬成果完整雷同,則解釋圖片找到,填充查找到的地位坐標數據到查找成果數組。
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(biggerY>=scrShotImgHeight || biggerX>=scrShotImgWidth) {
return false;
}
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。
別的,能夠還要斟酌效力成績,不外,我在我的運用場景中其實不太在乎效力。假如有同伙看到這篇文章,對這個話題有更好的設法主意,請留言。