利用Tess4J实现图片识别

 

1、下载java

1.进入官网下载页面git

https://sourceforge.net/projects/tess4j/github

 

2.点击downloadspa

 

3.下载后解压,目录以下,圈出的三个文件夹是须要用到的.net

 

2、使用Tess4Jcode

1.将dist和lib下的包导入java项目blog

 

2.将 tessdata 文件夹复制进项目的根目录图片

 

3.示范代码以下get

public class OCRDemo {

    public static void main(String[] args) {
        try {
            double start=System.currentTimeMillis();
            File imageFile = new File("C:\\Users\\dan\\Desktop\\12345.png");//图片位置
            ITesseract instance = new Tesseract();
            //instance.setDatapath("");//设置tessdata位置
            instance.setLanguage("chi_sim");//选择字库文件
            String result = instance.doOCR(imageFile);//开始识别
            double end=System.currentTimeMillis();
            System.out.println(result);//打印图片内容
            System.out.println("耗时"+(end-start)/1000+" s");
        } catch (TesseractException e) {
            e.printStackTrace();
        }
    }

}

注意事项:it

①若是tessdata没有放入根目录,务必设置teedata的位置

instance.setDatapath("");//设置tessdata位置

②选择字库文件不须要写上后缀,默认 tessdata 包中可能没有 chi_sim 这个中文包,须要本身下载

https://github.com/tesseract-ocr/tessdata

 

3、运行结果

 

官方字库识别率仍是偏低的,若是对精度要求高的话须要本身训练字库了

相关文章
相关标签/搜索