spring boot项目pom文件中添加以下依赖
<!-- https://mvnrepository.com/artifact/org.bytedeco/javacv-platform -->
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.5</version>
</dependency>
单类代码实现,复制到idea编辑器里,右键run运行即可。
OCR方法参数说明,
1.lng 语言类型 分为两种 1.eng 英语 2.chi_sim 中文简体
2.dataPath 语言数据集文件夹路径

3.imagePath 需要识别的图片文件路径

import org.bytedeco.javacpp.BytePointer;
import org.bytedeco.leptonica.PIX;
import org.bytedeco.leptonica.global.lept;
import org.bytedeco.tesseract.TessBaseAPI;
public class OcrTest {
public static String OCR(String lng,String dataPath,String imagePath) {
TessBaseAPI api=new TessBaseAPI();
if (api.Init(dataPath, lng)!=0){
System.out.println("error");
}
PIX image= lept.pixRead(imagePath);
if (image==null){
return "";
}
api.SetImage(image);
BytePointer outText=api.GetUTF8Text();
String result=outText.getString();
api.End();
outText.deallocate();
lept.pixDestroy(image);
return result;
}
public static void main(String[] args) {
String text= OCR("chi_sim", "E:\\traineddata", "C:\\Users\\tarzan\\Desktop\\image\\test5.png");
System.out.println(text);
}
}
测试样例结果
test1.jpg

test2.jpg

test3.jpg

test4.jpg

test5.jpg

到此这篇关于不到十行实现javaCV图片OCR文字识别的文章就介绍到这了,更多相关javaCV OCR文字识别内容请搜索社区以前的文章或继续浏览下面的相关文章希望大家以后多多支持社区! |