Tesseract 的OSGI环境下面一直报”java.lang.RuntimeException: Need to install JAI Image I/O package.“的解决办法
2015-05-22 01:19
537 查看
有帮助的文章:http://stackoverflow.com/questions/22014969/tess4j-with-spring-mvc http://stackoverflow.com/questions/22035048/imageio-jar-works-as-standalone-but-not-as-a-web-project http://stackoverflow.com/questions/27394852/getimagewritersbyformatname-doesnt-give-proper-values
最近做一个基于 tesseract-ocr-3.02+tess4j-1.5.0+servicemix-5.4.0(OSGI的一种方案)时老是报java.lang.RuntimeException: Need to install JAI Image I/O package
参照上面的帮助文件不断的尝试,最终找到原因是需要将jai_imageio.jar 放入jre/ext目录下面(我本机的JAVA地址 C:\Program Files\Java\jdk1.8.0\jre\lib\ext)
下面是部分代码:
@Override
public String getLocation(String filePath) throws IOException {
BufferedImage image = null;
try {
//you can either use URL or File for reading image using ImageIO
File imagefile = new File("D:\\simplechen\\study\\osgi\\demo\\images\\1.jpg");
image = ImageIO.read(imagefile);
//ImageIO Image write Example in Java
ImageIO.write(image, "jpg",new File("D:\\simplechen\\study\\osgi\\demo\\tmp\\1.jpg"));
ImageIO.write(image, "bmp",new File("D:\\simplechen\\study\\osgi\\demo\\tmp\\1.bmp"));
ImageIO.write(image, "gif",new File("D:\\simplechen\\study\\osgi\\demo\\tmp\\1.gif"));
ImageIO.write(image, "png",new File("D:\\simplechen\\study\\osgi\\demo\\tmp\\1.png"));
} catch (IOException e) {
e.printStackTrace();
}
String capctha = "";
getLogger().log(LogService.LOG_INFO, "从本地获取图片并解析; url=" + filePath);
System.err.println("you call getLocation" + filePath);
File dataFile = bundleContext.getDataFile(filePath);
if(dataFile.exists()){
ImageIO.scanForPlugins();
Tesseract instance = Tesseract.getInstance(); // JNA Interface Mapping
System.err.println("检验环境是否支持TIFF");
String format="tif";
System.out.println(format);
Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(format);
System.out.println(readers.hasNext());
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("tiff");
System.out.println(writers.hasNext());
try {
capctha = instance.doOCR(dataFile).trim();
System.err.println("capctha is "+capctha);
} catch (TesseractException e) {
e.printStackTrace();
getLogger().log(LogService.LOG_ERROR, "解析文件"+dataFile.getAbsolutePath()+"失败,原因:"+e);
}
} else {
getLogger().log(LogService.LOG_ERROR, "找不到文件"+filePath);
System.err.println("找不到文件");
}
return capctha;
}
注意:
下载 jai_imageio.jar (http://www.java2s.com/Code/Jar/j/Downloadjaiimageio11jar.htm)
最近做一个基于 tesseract-ocr-3.02+tess4j-1.5.0+servicemix-5.4.0(OSGI的一种方案)时老是报java.lang.RuntimeException: Need to install JAI Image I/O package
参照上面的帮助文件不断的尝试,最终找到原因是需要将jai_imageio.jar 放入jre/ext目录下面(我本机的JAVA地址 C:\Program Files\Java\jdk1.8.0\jre\lib\ext)
下面是部分代码:
@Override
public String getLocation(String filePath) throws IOException {
BufferedImage image = null;
try {
//you can either use URL or File for reading image using ImageIO
File imagefile = new File("D:\\simplechen\\study\\osgi\\demo\\images\\1.jpg");
image = ImageIO.read(imagefile);
//ImageIO Image write Example in Java
ImageIO.write(image, "jpg",new File("D:\\simplechen\\study\\osgi\\demo\\tmp\\1.jpg"));
ImageIO.write(image, "bmp",new File("D:\\simplechen\\study\\osgi\\demo\\tmp\\1.bmp"));
ImageIO.write(image, "gif",new File("D:\\simplechen\\study\\osgi\\demo\\tmp\\1.gif"));
ImageIO.write(image, "png",new File("D:\\simplechen\\study\\osgi\\demo\\tmp\\1.png"));
} catch (IOException e) {
e.printStackTrace();
}
String capctha = "";
getLogger().log(LogService.LOG_INFO, "从本地获取图片并解析; url=" + filePath);
System.err.println("you call getLocation" + filePath);
File dataFile = bundleContext.getDataFile(filePath);
if(dataFile.exists()){
ImageIO.scanForPlugins();
Tesseract instance = Tesseract.getInstance(); // JNA Interface Mapping
System.err.println("检验环境是否支持TIFF");
String format="tif";
System.out.println(format);
Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(format);
System.out.println(readers.hasNext());
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("tiff");
System.out.println(writers.hasNext());
try {
capctha = instance.doOCR(dataFile).trim();
System.err.println("capctha is "+capctha);
} catch (TesseractException e) {
e.printStackTrace();
getLogger().log(LogService.LOG_ERROR, "解析文件"+dataFile.getAbsolutePath()+"失败,原因:"+e);
}
} else {
getLogger().log(LogService.LOG_ERROR, "找不到文件"+filePath);
System.err.println("找不到文件");
}
return capctha;
}
注意:
下载 jai_imageio.jar (http://www.java2s.com/Code/Jar/j/Downloadjaiimageio11jar.htm)
相关文章推荐
- Android开发中java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx}: java.lang.NullPointerException 错误的集中原因及解决办法
- FAILED: Error in metadata: java.lang.RuntimeException: Unable to instantiate org.apache.解决办法
- FAILED: Error in metadata: java.lang.RuntimeException: Unable to instantiate org.apache.解决办法
- FAILED: Error in metadata: java.lang.RuntimeException: Unable to instantiate org.apache.解决办法
- FAILED: Error in metadata: java.lang.RuntimeException: Unable to instantiate org.apache.解决办法
- Android开发中java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx}: java.lang.NullPointerException 错误的集中原因及解决办法
- java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView解决办法
- Eclipse OS X Yosemite 提示– To open“Eclipse” you need to install the legacy Java SE 6 runtime的解决办法
- FAILED: Error in metadata: java.lang.RuntimeException: Unable to instantiate org.apache.解决办法
- java.lang.RuntimeException: Unable to instantiate activity ComponentInfo类似问题解决办法
- java.lang.RuntimeException: Unable to instantiate activity ComponentInfo异常解决
- java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap:报错解决
- ambari-server启动出现Caused by: java.lang.RuntimeException:java.lang.ClassNotFoundEception:com.mysql.jdbc.Driver问题解决办法(图文详解)
- MyEclipse 2016 在部署启动新项目时报 Could not publish to the server. java.lang.IndexOutOfBoundsException 解决办法
- 启动MyEclipse 出现java.lang.RuntimeException: No application id has been found 解决办法
- 解决java.lang.SecurityException: Package android does not belong to 10065
- java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()解决办法
- java.lang.RuntimeException: Unable to instantiate activity ComponentInfo异常解决
- unexpected token: * 和 java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to 解决办法
- 真机测试报错ERROR/AndroidRuntime: java.lang.RuntimeException: setParameters failed解决办法