如何使用Java实现屏幕找图功能
1个回答
展开全部
测试代码
[java] view plain copy
public static void main(String[] args) throws Exception {
findImage4FullScreen(ImageCognition.SIM_ACCURATE_VERY);
}
public static void findImage4FullScreen(int sim) throws Exception {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int w = (int) screenSize.getWidth();
int h = 200;
Robot robot = new Robot();
BufferedImage screenImg = robot.createScreenCapture(new Rectangle(0, 0,
w, h));//对屏幕指定范围进行截图,保存到BufferedImage中
OutputStream out = new FileOutputStream("data/images/screen.png");
ImageIO.write(screenImg, "png", out);//将截到的BufferedImage写到本地
InputStream in = new FileInputStream("data/images/search.png");
BufferedImage searchImg = ImageIO.read(in);//将要查找的本地图读到BufferedImage
//图片识别工具类
ImageCognition ic = new ImageCognition();
List<CoordBean> list = ic.imageSearch(screenImg, searchImg, sim);
for (CoordBean coordBean : list) {
System.out.println("找到图片,坐标是" + coordBean.getX() + ","
+ coordBean.getY());
//标注找到的图的位置
Graphics g = screenImg.getGraphics();
g.setColor(Color.BLACK);
g.drawRect(coordBean.getX(), coordBean.getY(),
searchImg.getWidth(), searchImg.getHeight());
g.setFont(new Font(null, Font.BOLD, 20));
g.drawString("←找到的图片在这里",
coordBean.getX() + searchImg.getWidth() + 5,
coordBean.getY() + 10 + searchImg.getHeight() / 2);
out = new FileOutputStream("data/images/result.png");
ImageIO.write(screenImg, "png", out);
}
}
额外的类
CoordBean
package cn.xt.imgCongnition;
public class CoordBean {
private int x;
private int y;
/**
* 获取x坐标
*
* @return x坐标
*/
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
/**
* 获取y坐标
*
* @return
*/
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
RgbImageComparerBean
package cn.xt.imgCongnition;
/**
* RGB 相关,图片相似度计算时使用
*
*/
public class RgbImageComparerBean {
/****** 颜色值数组,第一纬度为x坐标,第二纬度为y坐标 ******/
private int colorArray[][];
/*** 是否忽略此点,若为true,则不纳入比较的像素行列。 ***/
private boolean ignorePx[][];
/**** 图片的宽高 ****/
private int imgWidth;
private int imgHeight;
// 图片的像素总数
private int pxCount;
/**
* 获取图像的二维数组组成
*
* @return 图像的二维数组
*/
public int[][] getColorArray() {
return colorArray;
}
/**
* 要对比的像素总数,会自动筛选掉不对比的颜色
*
* @param pxCount
*/
public void setPxCount(int pxCount) {
this.pxCount = pxCount;
}
/**
* 设置颜色二维数组
*
* @param colorArray
* 颜色二维数组,一维为x轴,二维为y轴
*/
public void setColorArray(int[][] colorArray) {
this.colorArray = colorArray;
this.imgWidth = this.colorArray.length;
this.imgHeight = this.colorArray[0].length;
this.pxCount = this.imgWidth * this.imgHeight;
}
/**
* 是否忽略此点,若为true,则不纳入像素比较行列。
*
* @return 具体x,y坐标的那个像素点
*/
public boolean[][] getIgnorePx() {
return ignorePx;
}
/**
* 是否忽略此点,若为true,则不纳入像素比较行列。
*
* @param ignorePx
* 具体x,y坐标的那个像素点
*/
public void setIgnorePx(boolean[][] ignorePx) {
this.ignorePx = ignorePx;
}
/**
* 获取图像的宽度
*
* @return 图像宽度
*/
public int getImgWidth() {
return imgWidth;
}
/**
* 获取图像的高度
*
* @return 图像高度
*/
public int getImgHeight() {
return imgHeight;
}
/**
* 获取图像里像素的总数
*
* @return
*/
public int getPxCount() {
return pxCount;
}
}
[java] view plain copy
public static void main(String[] args) throws Exception {
findImage4FullScreen(ImageCognition.SIM_ACCURATE_VERY);
}
public static void findImage4FullScreen(int sim) throws Exception {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int w = (int) screenSize.getWidth();
int h = 200;
Robot robot = new Robot();
BufferedImage screenImg = robot.createScreenCapture(new Rectangle(0, 0,
w, h));//对屏幕指定范围进行截图,保存到BufferedImage中
OutputStream out = new FileOutputStream("data/images/screen.png");
ImageIO.write(screenImg, "png", out);//将截到的BufferedImage写到本地
InputStream in = new FileInputStream("data/images/search.png");
BufferedImage searchImg = ImageIO.read(in);//将要查找的本地图读到BufferedImage
//图片识别工具类
ImageCognition ic = new ImageCognition();
List<CoordBean> list = ic.imageSearch(screenImg, searchImg, sim);
for (CoordBean coordBean : list) {
System.out.println("找到图片,坐标是" + coordBean.getX() + ","
+ coordBean.getY());
//标注找到的图的位置
Graphics g = screenImg.getGraphics();
g.setColor(Color.BLACK);
g.drawRect(coordBean.getX(), coordBean.getY(),
searchImg.getWidth(), searchImg.getHeight());
g.setFont(new Font(null, Font.BOLD, 20));
g.drawString("←找到的图片在这里",
coordBean.getX() + searchImg.getWidth() + 5,
coordBean.getY() + 10 + searchImg.getHeight() / 2);
out = new FileOutputStream("data/images/result.png");
ImageIO.write(screenImg, "png", out);
}
}
额外的类
CoordBean
package cn.xt.imgCongnition;
public class CoordBean {
private int x;
private int y;
/**
* 获取x坐标
*
* @return x坐标
*/
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
/**
* 获取y坐标
*
* @return
*/
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
RgbImageComparerBean
package cn.xt.imgCongnition;
/**
* RGB 相关,图片相似度计算时使用
*
*/
public class RgbImageComparerBean {
/****** 颜色值数组,第一纬度为x坐标,第二纬度为y坐标 ******/
private int colorArray[][];
/*** 是否忽略此点,若为true,则不纳入比较的像素行列。 ***/
private boolean ignorePx[][];
/**** 图片的宽高 ****/
private int imgWidth;
private int imgHeight;
// 图片的像素总数
private int pxCount;
/**
* 获取图像的二维数组组成
*
* @return 图像的二维数组
*/
public int[][] getColorArray() {
return colorArray;
}
/**
* 要对比的像素总数,会自动筛选掉不对比的颜色
*
* @param pxCount
*/
public void setPxCount(int pxCount) {
this.pxCount = pxCount;
}
/**
* 设置颜色二维数组
*
* @param colorArray
* 颜色二维数组,一维为x轴,二维为y轴
*/
public void setColorArray(int[][] colorArray) {
this.colorArray = colorArray;
this.imgWidth = this.colorArray.length;
this.imgHeight = this.colorArray[0].length;
this.pxCount = this.imgWidth * this.imgHeight;
}
/**
* 是否忽略此点,若为true,则不纳入像素比较行列。
*
* @return 具体x,y坐标的那个像素点
*/
public boolean[][] getIgnorePx() {
return ignorePx;
}
/**
* 是否忽略此点,若为true,则不纳入像素比较行列。
*
* @param ignorePx
* 具体x,y坐标的那个像素点
*/
public void setIgnorePx(boolean[][] ignorePx) {
this.ignorePx = ignorePx;
}
/**
* 获取图像的宽度
*
* @return 图像宽度
*/
public int getImgWidth() {
return imgWidth;
}
/**
* 获取图像的高度
*
* @return 图像高度
*/
public int getImgHeight() {
return imgHeight;
}
/**
* 获取图像里像素的总数
*
* @return
*/
public int getPxCount() {
return pxCount;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询