资源简介
纯java实现的区域找图功能
代码片段和文件信息
package cn.xt.test;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import javax.imageio.ImageIO;
import cn.xt.imgCongnition.CoordBean;
import cn.xt.imgCongnition.ImageCognition;
public class Image {
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));
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 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);
}
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2016-12-12 00:07 ImageCognition\
文件 290 2016-12-12 00:06 ImageCognition\.classpath
文件 390 2016-12-12 00:09 ImageCognition\.project
目录 0 2016-12-06 13:12 ImageCognition\.settings\
文件 598 2016-12-06 13:12 ImageCognition\.settings\org.eclipse.jdt.core.prefs
目录 0 2016-12-12 00:09 ImageCognition\bin\
目录 0 2016-12-12 00:09 ImageCognition\bin\cn\
目录 0 2016-12-12 00:09 ImageCognition\bin\cn\xt\
目录 0 2016-12-12 00:09 ImageCognition\bin\cn\xt\test\
文件 3456 2016-12-12 00:09 ImageCognition\bin\cn\xt\test\Image.class
目录 0 2016-12-12 00:07 ImageCognition\data\
目录 0 2016-12-12 00:09 ImageCognition\data\images\
文件 40136 2016-12-12 00:09 ImageCognition\data\images\result.png
文件 39482 2016-12-12 00:09 ImageCognition\data\images\screen.png
文件 1358 2016-12-11 22:48 ImageCognition\data\images\search.png
文件 4103 2016-12-12 00:06 ImageCognition\image_cognition.jar
目录 0 2016-12-06 13:41 ImageCognition\src\
目录 0 2016-12-06 13:41 ImageCognition\src\cn\
目录 0 2016-12-06 13:42 ImageCognition\src\cn\xt\
目录 0 2016-12-12 00:09 ImageCognition\src\cn\xt\test\
文件 2050 2016-12-12 00:06 ImageCognition\src\cn\xt\test\Image.java
- 上一篇:Java文档管理系统论文
- 下一篇:Java设计模式-32讲地址.zip
评论
共有 条评论