带有Spring MVC的tess4j
我已经尝试将tess4j作为独立的Java程序使用,并且可以正常工作以提供文本输出。
现在,我试图创建一个Spring MVC Web项目,在pom中添加tess4j的依赖项,并且在我的项目中添加了tess4j源。
File imageFile = new File("D:/Data/jars/tess/eurotext.tif"); Tesseract instance = Tesseract.getInstance(); // JNA Interface Mapping
// Tesseract1 instance = new Tesseract1(); // JNA Direct Mapping
try {
result = instance.doOCR(imageFile);
System.out.println(result);
} catch (TesseractException e) {
System.err.println(e.getMessage());
}
当我试图在项目中运行一个独立的Java程序时,上面的代码可以正常工作。因此很明显,将jar文件添加到正确构建路径中。
但是,当我在控制器映射或服务中调用代码时,它将引发运行时异常。
SEVERE: Unsupported image format. May need to install JAI Image I/O package.https://java.net/projects/jai-imageio/
java.lang.RuntimeException: Unsupported image format. May need to install JAI Image I/O package.
https://java.net/projects/jai-imageio/
at net.sourceforge.vietocr.ImageIOHelper.getIIOImageList(ImageIOHelper.java:324)
at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:173)
at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:158)
at com.ocr.tesseract.TesseractExample.getTextFromImage(TesseractExample.java:27)
at com.cogz.tp.controller.HomeController.view(HomeController.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:748)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:822)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:662)
java.lang.RuntimeException: Unsupported image format. May need to install JAI Image I/O package.
https://java.net/projects/jai-imageio/
请让我知道缺少的内容。谢谢。
回答:
甚至我也遇到了使用tess4j
for
的类似问题DynamicWebProject
。但是感谢@nguyenq的评论对我的帮助,让我可以正常工作了。tess4j通常使用TIFF处理程序进行光学识别。默认ImageIO不提供它所需的依赖关系。因此,需要jai-
imageio.jar。我所做的只是ImageIO.scanForPlugins()
在调用执行的包装器类之前添加了一行doOCR
。我的lib中有以下jar:-
tess4j.jar
jai_imageio.jar
ghost4j-0.3.1.jar
jna.jar
junit-4.10.jar
这是示例代码:
TessractOCR tessocr = new TessractOCR(); ImageIO.scanForPlugins();
String extractedString = tessocr.extractTextFromImage(binarizrImage);
功能
public static String extractTextFromImage(BufferedImage image){ RenderedImage img = image;
String result =null;
try {
File outputfile = new File("saved.png");
ImageIO.write(img, "png", outputfile);
Tesseract instance = Tesseract.getInstance(); // JNA Interface Mapping
instance.setDatapath("E:\\OCR-data\\Tess4J-1.2-src\\Tess4J");
result = instance.doOCR(outputfile);
System.out.println(result);
} catch (Exception e) {
System.err.println(e.getMessage());
}
return result;
}
它可以工作100%:)
以上是 带有Spring MVC的tess4j 的全部内容, 来源链接: utcz.com/qa/398787.html