• 使用 Tess4J 实现本地与远程图片的文字识别


    pom:

    1. <dependency>
    2. <groupId>net.sourceforge.tess4j</groupId>
    3. <artifactId>tess4j</artifactId>
    4. <version>5.11.0</version>
    5. </dependency>

    部分代码:

    1. package com.zy.datapickcli.sys.controller;
    2. import net.sourceforge.tess4j.Tesseract;
    3. import net.sourceforge.tess4j.TesseractException;
    4. import java.io.File;
    5. public class TempTest {
    6. public static void main(String[] args) throws TesseractException {
    7. File file = new File("D:\\1.png");
    8. System.out.println(recognizeText(file));
    9. }
    10. public static String recognizeText(File imageFile) throws TesseractException {
    11. Tesseract tesseract = new Tesseract();
    12. // 设定训练文件的位置(如果是标准英文识别,此步可省略)
    13. tesseract.setDatapath("D:\\tessdata");
    14. tesseract.setLanguage("chi_sim");
    15. return tesseract.doOCR(imageFile);
    16. }
    17. }

    data文件下载地址

    https://gitcode.com/tesseract-ocr/tessdata/tree/main

    其余参考代码:

    1. @Service
    2. public class OcrService {
    3. public String recognizeText(File imageFile) throws TesseractException {
    4. Tesseract tesseract = new Tesseract();
    5. // 设定训练文件的位置(如果是标准英文识别,此步可省略)
    6. tesseract.setDatapath("你的tessdata各语言集合包地址");
    7. tesseract.setLanguage("chi_sim");
    8. return tesseract.doOCR(imageFile);
    9. }
    10. public String recognizeTextFromUrl(String imageUrl) throws Exception {
    11. URL url = new URL(imageUrl);
    12. InputStream in = url.openStream();
    13. Files.copy(in, Paths.get("downloaded.jpg"), StandardCopyOption.REPLACE_EXISTING);
    14. File imageFile = new File("downloaded.jpg");
    15. return recognizeText(imageFile);
    16. }
    17. }

    执行效果:

  • 相关阅读:
    Hadoop对集群的一些操作的命令介绍
    记一次etcd数据恢复
    【设计模式】32.结构型模式-组合模式(Composite)
    利用条形码生成器在Word 2013中轻松制作条形码的方法
    Android9.0 mtk录像流程分析
    领域内容第18名
    【vue】浏览器播放提示音audio
    Linux 查看权限控制
    【前后端交互与HTTP协议】(HTTP协议、本地存储、Ajax&Fetch 与跨域请求)
    Redis常见问题
  • 原文地址:https://blog.csdn.net/u013008898/article/details/138183432