[英]Modify java code to read input file from Linux command line
I am new to Java.我是 Java 的新手。 This is a code for OCR from image/pdf to text using tess4j.
这是使用 tess4j 从图像/pdf 到文本的 OCR 代码。 I just want to modify it so that it takes OCR_file.png/OCR_file.pdf from input in the command line and not by specifying path as below-
我只想修改它,使其从命令行中的输入中获取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());
}
}
}
that's what the (String[] args)
is for, if I'm reading your question correctly.如果我正确阅读了您的问题,这就是
(String[] args)
的用途。
if you run test.java -"//home//desktop//OCR_file.pdf"
in command line/terminal it should be saved to args[0].如果您在命令行/终端中运行
test.java -"//home//desktop//OCR_file.pdf"
,则应将其保存到 args[0]。
so if you rewrite the imageFile initialization to:因此,如果您将 imageFile 初始化重写为:
File imageFile = new File(args[0]);
that should work since args[0] will be "//home//desktop//OCR_file.pdf"这应该可以工作,因为 args[0] 将是“//home//desktop//OCR_file.pdf”
I also noticed that your imageFile initialization has a typo.我还注意到您的 imageFile 初始化有错字。 the extension at the end is png, but you mentioned that it's supposed to be pdf.
最后的扩展名是 png,但你提到它应该是 pdf。
Command - java tess4j.Test "/home/desktop/OCR_file.png"命令- 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());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.