[英]How can I modify this Java program to read in the names from a file designated in the command line?
[英]Modify java code to read input file from Linux command line
我是 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());
}
}
}
如果我正確閱讀了您的問題,這就是(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。
命令- 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.