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);
}
}
JAVA 截图+tess4j识别的更多相关文章
- JAVA使用Tess4J进行ocr识别
Tess4J是对Tesseract OCR API.的Java JNA 封装.使java能够通过调用Tess4J的API来使用Tesseract OCR.支持的格式:TIFF,JPEG,GIF,PNG ...
- JAVA OCR图片识别
今天闲来无聊,尝试了一下OCR识别,尝试了以下三种方案: 1.直接使用业界使用最广泛的Tesseract-OCR. Tesseract项目最初由惠普实验室支持,1996年被移植到Windows上,19 ...
- Windows文件路径转换为java中可识别的文件路径的转义方法,(另附转义多种格式)
ps:欢迎加qq好友:2318645572,交流学习 一:路径转化 Windows中的文件路径格式为 D:\eclipse\apache-tomcat-7.0.67\wtpwebapps\... Ja ...
- java 使用tess4j实现OCR的最简单样例
网上很多教程没有介绍清楚tessdata的位置,以及怎么配置,并且对中文库的描述也存在问题,这里介绍一个最简单的样例. 1.使用maven,直接引入依赖,确保你的工程JDK是1.8以上 <dep ...
- Java版本:识别Json字符串并分隔成Map集合
前言: 最近又看了点Java的知识,于是想着把CYQ.Data V5迁移到Java版本. 过程发现坑很多,理论上看大部分很相似,实践上代码写起来发现大部分都要重新思考方案. 遇到的C#转Java的一些 ...
- Java版 人脸识别SDK demo
虹软人脸识别SDK之Java版,支持SDK 1.1+,以及当前最新版本2.0,滴滴,抓紧上车! 前言 由于业务需求,最近跟人脸识别杠上了,本以为虹软提供的SDK是那种面向开发语言的,结果是一堆dll· ...
- Java使用J4L识别验证码
1.首先要下载j4l的相应文件和jar 下载地址:http://www.java4less.com/ocrtools/ocrtools.php?info=download 2.下载完成之后解压,文件目 ...
- Java版 人脸识别SDK dem
虹软人脸识别SDK之Java版,支持SDK 1.1+,以及2.0版本,滴滴,抓紧上车! 前言由于业务需求,最近跟人脸识别杠上了,本以为虹软提供的SDK是那种面向开发语言的,结果是一堆dll······ ...
- windows版 Java调用人脸识别离线sdk
最近因工作需求在java-web服务中调用人脸识别离线sdk,主要通过JNA及JNI技术,但均未调试通过,JNA调用时出现以下异常,一直未解决,求大佬指点,导常信息如下: in BaiduFaceAp ...
随机推荐
- 01 语言基础+高级:1-3 常用API第一部分_day07【Scanner类、Random类、ArrayList类】
day07[Scanner类.Random类.ArrayList类] Scanner类Random类ArrayList类 教学目标 能够明确API的使用步骤能够使用Scanner类获得键盘录入数据能够 ...
- sqlite如何避免重复建表(获取已经存在的表)
找到已经存在的所有表,手动判断是否需要建表 SELECT name FROM SQLITE_MASTER WHERE type='table'ORDER BY name" 建表时sqlite ...
- JSONObject和JSONArray的基本使用
一.JSONObject和JSONArray的数据表示形式 JSONObject的数据是用 { } 来表示的, 例如: { "name" : "佩奇", ...
- sockaddr_in 转成string
string strAcceptIp = inet_ntoa(remoteAddr.sin_addr);
- CentOS下MySQL忘记root密码解决方法【亲测】
1.修改MySQL的登录设置: # vim /etc/my.cnf 在[mysqld]的段中加上一句:skip-grant-tables 例如: [mysqld] datadir=/var/lib/m ...
- Office 365 邮件流
进入Exchange管理中心->点击左侧的“邮件流”->进入邮件流配置页面. 一.规则 规则也称传输规则,对通过组织传递的邮件,根据设定条件进行匹配,并对其进行操作.传输规则与众多电子邮件 ...
- js几个常用的弹层
js弹层技术很常见,自己每次用上网找,一找一大堆. 对比了几种,考虑通用性和易用性,这里记录两个. jQueryUI的http://jqueryui.com/dialog/#modal-form ar ...
- [USACO09OCT]谷仓里的回声Barn Echoes(hush、STL)
https://www.luogu.org/problem/P2957 题目描述 The cows enjoy mooing at the barn because their moos echo b ...
- C2. Power Transmission (Hard Edition)(线段相交)
This problem is same as the previous one, but has larger constraints. It was a Sunday morning when t ...
- D - Association for Control Over Minds Kattis - control (并查集+STL)
You are the boss of ACM (Association for Control over Minds), an upstanding company with a single go ...