利用第三方jar包,实现识别图片上的文字。第三方支持地址:Spire.OCR for Java | 专业的图文识别组件,用以读取图片格式中的文本Spire.OCR for Java 是专为 Java 开发者设计的强大OCR库,提供高效的文字识别功能,能够从图像中准确识别和提取文本。https://www.e-iceblue.cn/Introduce/Spire-OCR-JAVA.html
1.maven项目,添加依赖
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.ocr</artifactId>
<version>1.9.0</version>
</dependency>
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
2.根据服务器类型,下载相关依赖文件
linux
windows
3.编写demo代码
@Test
public void getText() throws OcrException {
//指定依赖文件的路径
String dependencies = "dependencies/";
//指定要需要扫描的图片的路径
String imageFile = "C:\\Users\\user\\Desktop\\3.jpg";
//指定输出文件的路径
String outputFile = "test.txt";
//创建OcrScanner对象,并设置其依赖文件路径
OcrScanner scanner = new OcrScanner();
scanner.setDependencies(dependencies);
//扫描指定的图像文件
scanner.scan(imageFile);
//获取扫描的文本内容
String scannedText = scanner.getText().toString();
System.out.println(scannedText);
}
4.扩展
根据图片OCR识别,来获取身份证等证件图片上的信息,并用正则表达式解析识别的文字
@Test
public void testIdcard() throws OcrException {
String filePath = "C:\\Users\\85753\\user\\idcard\\";
for (int i = 1; i < 11; i++) {
long startTime = System.currentTimeMillis();
HashMap<String, String> hashMap = idCardOCR(filePath + i + ".jpg");
long endTime = System.currentTimeMillis();
System.out.println(hashMap);
System.out.println("一共耗时:" + (startTime - endTime) + "毫秒!");
}
}
public HashMap<String, String> idCardOCR(String filePath) throws OcrException {
//指定依赖文件的路径
String dependencies = "dependencies/";
//指定要需要扫描的图片的路径
String imageFile = filePath;
//创建OcrScanner对象,并设置其依赖文件路径
OcrScanner scanner = new OcrScanner();
scanner.setDependencies(dependencies);
scanner.scan(imageFile);//扫描指定的图像文件
//获取扫描的文本内容
String scannedText = scanner.getText().toString();
// 去除换行,去除无用数据
String result = scannedText.replaceAll("\r", "").replaceAll("\n", "");
result = result.replaceAll("Evaluation Warning : The version can be used only for evaluation purpose...", "");
System.out.println(result);
HashMap<String, String> map = new HashMap<>();
map = this.dealIDCardText(result);
return map;
}
/**
* 正则处理识别的身份证数据
*
* @param result
* @return
*/
public static HashMap<String, String> dealIDCardText(String result) {
HashMap<String, String> map = new HashMap<>();
String name = "";
String sex = "";
String nation = "";
String birth = "";
String address = "";
String idCard = "";
// 处理数据(正则表达式获取身份证信息,满足以下一条即可)
String regex = "姓名(\\S+)民族(\\S+)性别(\\S+)出生(\\d{4}年\\d{1,2}月\\d{1,2}日)住址(\\S+)公民身份号码(\\d{17}[0-9Xx])";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(result);
String regex2 = "(\\S+)姓名民族(\\S+)性别(\\S+)出生(\\d{4}年\\d{1,2}月\\d{1,2}日)住址(\\S+)(\\d{17}[0-9Xx])公民身份号码";
Pattern pattern2 = Pattern.compile(regex2);
Matcher matcher2 = pattern2.matcher(result);
String regex3 = "姓名(\\S+)性别(\\S+)民族(\\S+)出生(\\d{4}年\\d{1,2}月\\d{1,2}日)住址(\\S+)公民身份号码(\\d{17}[0-9Xx])";
Pattern pattern3 = Pattern.compile(regex3);
Matcher matcher3 = pattern3.matcher(result);
String regex4 = "姓名(\\S+)性别(\\S+)民族(\\S+)出生(\\d{4}年\\d{1,2}月\\d{1,2}日)住址(\\S+)(\\d{17}[0-9Xx])公民身份号码";
Pattern pattern4 = Pattern.compile(regex4);
Matcher matcher4 = pattern4.matcher(result);
String regex5 = "姓名(\\S+)民族(\\S+)性别(\\S+)出生(\\d{4}年\\d{1,2}月\\d{1,2}日)住址(\\S+)(\\d{17}[0-9Xx])公民身份号码";
Pattern pattern5 = Pattern.compile(regex5);
Matcher matcher5 = pattern5.matcher(result);
String regex6 = "(\\S+)姓名性别(\\S+)民族(\\S+)出生(\\d{4}年\\d{1,2}月\\d{1,2}日)住址(\\S+)(\\d{17}[0-9Xx])公民身份号码";
Pattern pattern6 = Pattern.compile(regex6);
Matcher matcher6 = pattern6.matcher(result);
if (matcher.find()) {
name = matcher.group(1);
nation = matcher.group(2);
sex = matcher.group(3);
birth = matcher.group(4);
address = matcher.group(5);
idCard = matcher.group(6);
} else if (matcher2.find()) {
name = matcher2.group(1);
nation = matcher2.group(2);
sex = matcher2.group(3);
birth = matcher2.group(4);
address = matcher2.group(5);
idCard = matcher2.group(6);
} else if (matcher3.find()) {
name = matcher3.group(1);
sex = matcher3.group(2);
nation = matcher3.group(3);
birth = matcher3.group(4);
address = matcher3.group(5);
idCard = matcher3.group(6);
} else if (matcher4.find()) {
name = matcher4.group(1);
sex = matcher4.group(2);
nation = matcher4.group(3);
birth = matcher4.group(4);
address = matcher4.group(5);
idCard = matcher4.group(6);
} else if (matcher5.find()) {
name = matcher5.group(1);
nation = matcher5.group(2);
sex = matcher5.group(3);
birth = matcher5.group(4);
address = matcher5.group(5);
idCard = matcher5.group(6);
} else if (matcher6.find()) {
name = matcher6.group(1);
sex = matcher6.group(2);
nation = matcher6.group(3);
birth = matcher6.group(4);
address = matcher6.group(5);
idCard = matcher6.group(6);
}
map.put("name", name);
map.put("sex", sex);
map.put("nation", nation);
map.put("birth", birth);
map.put("address", address);
map.put("idCard", idCard);
return map;
}
注意:因图片识别后的身份证数据顺序不固定,经过多次测试,将可能的结果通过正则表达式来解析。如果符合其中一个正则,就将匹配的数据返回。
参考:如何在 Java 项目中扫描识别图片中的文字