有帮助的文章:
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; }