java 圖片識別文字(中英文混合)


調用 tess4j 庫來識別圖片文字

 

依賴的maven庫

<dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.26</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.26</version>
        </dependency>

<!-- https://mvnrepository.com/artifact/net.sourceforge.tess4j/tess4j -->
        <dependency>
            <groupId>net.sourceforge.tess4j</groupId>
            <artifactId>tess4j</artifactId>
            <version>5.1.1</version>
        </dependency>

 

圖片識別文字

package com;

import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract1;
import net.sourceforge.tess4j.TesseractException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

public class TestOCR {
    private static final Logger logger = LoggerFactory.getLogger(TestOCR.class);

    public static void main(String[] args) {
        String result = doOCR("字庫位置", "要識別的圖片地址");
        System.out.println(result);
    }

    private static String doOCR(String dataPath, String imgPath) {
        File imageFile = new File(imgPath);
        ITesseract instance = new Tesseract1();
        //字庫位置
        instance.setDatapath(dataPath);
        //eng+chi_sim代表中英文混合
        instance.setLanguage("eng+chi_sim");//eng :英文  chi_sim :簡體中文

        try {
            return instance.doOCR(imageFile);
        } catch (TesseractException e) {
            logger.error("", e);
        }

        return "";
    }
}

  

字庫下載

下載中文包:https://github.com/tesseract-ocr/tessdata 選擇chi_sim.traineddata文件進行下載,英文包在tess4j jar包中可以獲取。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM