简体   繁体   English

修改 java 代码从 Linux 命令行读取输入文件

[英]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.

相关问题 如何修改此Java程序以从命令行中指定的文件中读取名称? - How can I modify this Java program to read in the names from a file designated in the command line? 尝试从Java的Linux命令行读取文件时发生ArrayIndexOutOfBoundsException:0? - ArrayIndexOutOfBoundsException:0 occurs when trying to read in a file from the Linux command line in Java? 如何从命令行在Java中读取文件? - How to read file from command line in java? Java程序从文本文件读取输入并进行相应的修改 - Java Program read input from a text file and modify it accordingly 在Java中从命令行读取用户输入的正确方法 - proper way to read user input from command line in java Java:如何从命令行中读取-input或--h之类的选项? - Java: how to read options from command line like -input or --h? 从Linux命令行运行Java程序需要一个附加文件 - Running Java program from Linux command line that requires an additional file 从 linux 终端上的用户输入读取文件名 - JAVA - Read file name from user input on linux terminal - JAVA 将java代码从用户输入更改为命令行参数 - Changing java code from user input to command line argument 如何从在java中的命令行参数中给出的文件中读取? - how to read from file that was giving in a command line argument in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM