【问题标题】:Modify java code to read input file from Linux command line修改java代码以从Linux命令行读取输入文件
【发布时间】:2020-07-02 17:48:40
【问题描述】:

我是 Java 新手。这是使用 tess4j 从图像/pdf 到文本的 OCR 代码。我只想修改它,使其从命令行输入中获取 OCR_file.png/OCR_file.pdf 而不是通过指定路径如下-

package tess4j;

import java.io.File;
import java.io.*;
import net.sourceforge.tess4j.*;

public class Test{

public static void main(String[] args) {
    // ImageIO.scanForPlugins(); // for server environment
    File imageFile = new File("//home//desktop//OCR_file.png");
    ITesseract instance = new Tesseract(); // JNA Interface Mapping
    // ITesseract instance = new Tesseract1(); // JNA Direct Mapping
    instance.setDatapath("//home//desktop//tessdata"); // replace <parentPath> with path to parent directory of tessdata
    // instance.setLanguage("eng");

    try {
        String result = instance.doOCR(imageFile);
        System.out.println(result);
    } catch (TesseractException e) {
        System.err.println(e.getMessage());
    }
 }
}

【问题讨论】:

    标签: java linux ocr tesseract tess4j


    【解决方案1】:

    这就是(String[] args) 的用途,如果我没看错你的问题的话。

    如果你在命令行/终端运行test.java -"//home//desktop//OCR_file.pdf",它应该被保存到args[0]。

    所以如果你将 imageFile 初始化重写为:

    File imageFile = new File(args[0]);
    

    这应该可以工作,因为 args[0] 将是“//home//desktop//OCR_file.pdf”

    我还注意到您的 imageFile 初始化有一个错字。最后的扩展名是png,但你提到它应该是pdf。

    【讨论】:

      【解决方案2】:

      命令 - java tess4j.Test "/home/desktop/OCR_file.png"

      public static void main(String[] args) {
      // ImageIO.scanForPlugins(); // for server environment
      String path = args[0]; 
      File imageFile = new File(path);
      ITesseract instance = new Tesseract(); // JNA Interface Mapping
      // ITesseract instance = new Tesseract1(); // JNA Direct Mapping
      instance.setDatapath("//home//desktop//tessdata"); // replace <parentPath> with path to parent directory of tessdata
      // instance.setLanguage("eng");
      
      try {
          String result = instance.doOCR(imageFile);
          System.out.println(result);
      } catch (TesseractException e) {
          System.err.println(e.getMessage());
      }
      }
      

      【讨论】:

      • '不支持的法师格式。可能需要安装 JAI Image I/O 包。 java.net/projects/jai-imageiojava.lang.RuntimeException:不支持的图像格式。可能需要安装 JAI Image I/O 包。 java.net/projects/jai-imageio
      • 收到此错误。任何想法?之前使用相同的文件可以正常工作。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-02
      • 1970-01-01
      • 2016-01-03
      • 2021-07-19
      • 2019-02-20
      • 2021-10-13
      • 2018-07-08
      相关资源
      最近更新 更多