Let's first take a look at the pictures and renderings to be identified
Rendering:
Image recognition needs to use the tess4j package, the following is the download address:
https://share.weiyun.com/5Hjv13T
After we get the package, unzip it, you can put it in any directory
After unzipping
Import the tess4j-3.4.7.jar and lib folders in tessdata and dist into the eclipse project as shown in the figure
After the import is completed, we build the path of all the packages in the lib, and then we can write the code.
First screenshot code
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); // Path where screenshots are saved File screenFile = new File(fileName); // If the path doesn't exist, create it if (!screenFile.getParentFile().exists()) { screenFile.getParentFile().mkdirs(); } / / Determine whether the file exists, create the file if it does not exist if(!screenFile.exists()&& !screenFile .isDirectory()) { screenFile.mkdir(); } File f = new File(screenFile, folder); ImageIO.write(image, "png", f); //automatically open /*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 (); } } }
Identification code:
public class OCRDemo { public static void main(String[] args) throws TesseractException { ITesseract instance = new Tesseract(); //If you do not put tessdata in the root directory, you need to specify an absolute path //instance.setDatapath("the absolute path of tessdata"); // we need to specify the recognition language instance.setLanguage("Set the language package yourself"); // Specify the recognition image File imgDir = new File("test.png"); String ocrResult = instance.doOCR(imgDir); // output recognition result System.out.println("OCR Result: \n" + ocrResult ); } }
Combine code:
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("Poker Analysis"); 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) { // do logic processing try { String str =null; str = screenshot(); JOptionPane.showMessageDialog(null,"Please take a screenshot of the picture to be displayed and press OK"); OCR(str); JOptionPane.showMessageDialog(null,"Analysis succeeded"); } 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); // Path where screenshots are saved File screenFile = new File(fileName); // If the path doesn't exist, create it if (!screenFile.getParentFile().exists()) { screenFile.getParentFile().mkdirs(); } / / Determine whether the file exists, create the file if it does not exist if(!screenFile.exists()&& !screenFile .isDirectory()) { screenFile.mkdir(); } File f = new File(screenFile, folder); ImageIO.write(image, "png", f); //automatically open /*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(); //If you do not put tessdata in the root directory, you need to specify an absolute path // instance.setDatapath("F:\\Tess4J\\tessdata"); // we need to specify the recognition language instance.setLanguage("eng"); // Specify the recognition image File imgDir = new File("F:\\poker\\20180502\\test.jpg"); String ocrResult = instance.doOCR(imgDir); // output recognition result System.out.println("OCR Result: \n" + ocrResult); } }