java - 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
解决方案
推荐阅读
- compiler-construction - 确定堆栈帧的操作数堆栈的大小
- git - 如何通过 git 将目录添加到某个分支(不是 master 分支)
- javascript - 如何在 svelte 中制作搜索过滤器
- python - 使用 Python 解析具有相似字符串的文件中的行
- javascript - 如何在不创建新服务器的情况下编辑我使用 node.js 创建的现有服务器
- c# - 运行 dotnet publish -c Release -o out ERROR 时,ASP.NET Core 项目中的 Dockerfile 失败
- javascript - 尽管使用了显示块,但伪元素未在 React on 段落标记中显示
- gradle - Heroku 无法识别 gradle - 找不到 pom.xml 文件
- android - bash:adb:在 Windows 10 中找不到命令
- c - C 中的计算器应该只接受浮点数/整数