首页 > 解决方案 > Tesseract OCR 不适用于 Java Web 应用程序

问题描述

我正在尝试开发 Java Web Tesseract OCR 应用程序。以下代码完美运行:

public class App {

public String getImgText(String imageLocation) {
    ITesseract instance = new Tesseract();
    instance.setDatapath(Thread.currentThread().getContextClassLoader().getResource("tessdata").getPath());
    System.out.println("Thread.currentThread().getContextClassLoader().getResource(\"tessdata\").getPath() : "+Thread.currentThread().getContextClassLoader().getResource("tessdata").getPath());
    instance.setLanguage("eng");

    try {
        String imgText = instance.doOCR(new File(imageLocation));
        return imgText;
    } catch (TesseractException e) {
        e.getMessage();
        return "Error while reading image";
    }
}

public static void main(String[] args) {
    App app = new App();
    System.out.println(app.getImgText("/home/user/Desktop/1.png"));
}
}

但是当我尝试在我的 Java web(JSF) 应用程序中使用上面的代码时

  ITesseract instance = new Tesseract();

什么都没有打印出来。以下是我的网络应用程序的代码:

public String uploadImage(FileUploadEvent event) {
    System.out.println("webcore bean");
    //get uploaded file from the event
    UploadedFile uploadedFile = (UploadedFile) event.getFile();
    //create an InputStream from the uploaded file
    InputStream inputStr = null;
    try {
        inputStr = uploadedFile.getInputstream();
    } catch (IOException e) {
        //log error
    }

    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    String directory = externalContext.getInitParameter("uploadDirectory");
    String filename = FilenameUtils.getName(uploadedFile.getFileName());

    File destFile = new File(directory, "static" + getFileExtension(filename));

    //use org.apache.commons.io.FileUtils to copy the File
    try {
        FileUtils.copyInputStreamToFile(inputStr, destFile);
    } catch (IOException e) {
        //log error
    }
    System.out.println("getImageText(directory) : " + getImageText(directory));
    FacesMessage msg = new FacesMessage(event.getFile().getFileName() + " is uploaded.");
    FacesContext.getCurrentInstance().addMessage(null, msg);
    return null;
}

private String getImageText(String imageLocation) {
    try {
        System.out.println("Before ");
        ITesseract instance = new Tesseract1();
        System.out.println("After ");
        //instance.setDatapath("/usr/share/tesseract-ocr/4.00/tessdata");
        instance.setDatapath(Thread.currentThread().getContextClassLoader().getResource("tessdata").getPath());
        instance.setLanguage("eng");

        try {
            String imgText = instance.doOCR(new File(imageLocation));
            return imgText;
        } catch (TesseractException e) {
            e.getMessage();
            return "Error while reading image";
        }
    } catch (Exception e) {
        System.out.println("Before returning null");
        e.printStackTrace();
        return null;
    }

}

正在打印日志“之前”,但未打印日志“之后”。我正在使用以下技术:

a) Ubuntu 18.04 64 位操作系统

b) 网豆

c) 专家

d) 玻璃鱼 4.1

标签: javatesseractjsf-2.2tess4j

解决方案


推荐阅读