JAVA 截图+tess4j识别
我们先来看看要识别的图片和效果图
效果图:
图片识别需要用到tess4j这个包,下面是下载地址:
https://share.weiyun.com/5Hjv13T
我们拿到包以后解压出来,随便你放到哪个目录
解压出来后
把tessdata和dist里面的tess4j-3.4.7.jar, lib文件夹导入到eclipse项目里面如图
导入完成以后,我们把lib里面的包全部构建一下路径,后面就可以写代码了。
先上一段截图代码
package image; import java.awt.Desktop; import java.awt.Dimension; import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; import javax.imageio.ImageIO; public class CaptureScreen { public static void captureScreen(String fileName, String folder) throws Exception { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Rectangle screenRectangle = new Rectangle(screenSize); Robot robot = new Robot(); BufferedImage image = robot.createScreenCapture(screenRectangle); // 截图保存的路径 File screenFile = new File(fileName); // 如果路径不存在,则创建 if (!screenFile.getParentFile().exists()) { screenFile.getParentFile().mkdirs(); } //判断文件是否存在,不存在就创建文件 if(!screenFile.exists()&& !screenFile .isDirectory()) { screenFile.mkdir(); } File f = new File(screenFile, folder); ImageIO.write(image, "png", f); //自动打开 /*if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) Desktop.getDesktop().open(f);*/ } public static void main(String[] args) { Date dt=new Date(); SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); SimpleDateFormat sdf1=new SimpleDateFormat("yyyyMMddHHmmss"); String data=sdf.format(dt); String rd=sdf1.format(dt); try { captureScreen("F:\\poker\\"+data,rd+".png"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
识别代码:
public class OCRDemo { public static void main(String[] args) throws TesseractException { ITesseract instance = new Tesseract(); //如果未将tessdata放在根目录下需要指定绝对路径 //instance.setDatapath("the absolute path of tessdata"); // 我们需要指定识别语种 instance.setLanguage("自己设定语言包"); // 指定识别图片 File imgDir = new File("test.png"); String ocrResult = instance.doOCR(imgDir); // 输出识别结果 System.out.println("OCR Result: \n" + ocrResult ); } }
结合代码:
package tess4j; import java.awt.Dimension; import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; import javax.imageio.ImageIO; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import net.sourceforge.tess4j.ITesseract; import net.sourceforge.tess4j.Tesseract; import net.sourceforge.tess4j.TesseractException; public class tess4j { public static void main(String[] args) throws TesseractException { menu(); } public static void menu() { JFrame jf = new JFrame("扑克分析"); jf.setSize(300,200); jf.setVisible(true); JButton jb = new JButton("分析"); jb.setBounds(70, 60, 150, 50); jf.add(jb); jb.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // 进行逻辑处理即可 try { String str =null; str = screenshot(); JOptionPane.showMessageDialog(null,"请将要显示的图片截取出来再按确定"); OCR(str); JOptionPane.showMessageDialog(null,"分析成功"); } catch (TesseractException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); } public static String screenshot() { Date dt=new Date(); SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); SimpleDateFormat sdf1=new SimpleDateFormat("yyyyMMddHHmmss"); String data=sdf.format(dt); String rd=sdf1.format(dt); try { String str = "F:\\poker\\"+data+"\\"+rd+".png"; new CaptureScreen().captureScreen("F:\\poker\\"+data,rd+".png"); return str; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static class CaptureScreen { public void captureScreen(String fileName, String folder) throws Exception { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Rectangle screenRectangle = new Rectangle(screenSize); Robot robot = new Robot(); BufferedImage image = robot.createScreenCapture(screenRectangle); // 截图保存的路径 File screenFile = new File(fileName); // 如果路径不存在,则创建 if (!screenFile.getParentFile().exists()) { screenFile.getParentFile().mkdirs(); } //判断文件是否存在,不存在就创建文件 if(!screenFile.exists()&& !screenFile .isDirectory()) { screenFile.mkdir(); } File f = new File(screenFile, folder); ImageIO.write(image, "png", f); //自动打开 /*if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) Desktop.getDesktop().open(f);*/ } } public static void OCR(String str) throws TesseractException { System.out.println(str); ITesseract instance = new Tesseract(); //如果未将tessdata放在根目录下需要指定绝对路径 // instance.setDatapath("F:\\Tess4J\\tessdata"); // 我们需要指定识别语种 instance.setLanguage("eng"); // 指定识别图片 File imgDir = new File("F:\\poker\\20180502\\test.jpg"); String ocrResult = instance.doOCR(imgDir); // 输出识别结果 System.out.println("OCR Result: \n" + ocrResult); } }