Tesseract:OCR方法的索引超出范围异常

我正在使用Spring-MVC应用程序,在其中将Tesseract用于OCR.我正在传递的文件的索引超出范围异常.有任何想法吗?

错误日志:

et.sourceforge.tess4j.TesseractException: java.lang.IndexOutOfBoundsException
    at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:215)
    at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:196)
    at com.tooltank.spring.service.GroupAttachmentsServiceImpl.testOcr(GroupAttachmentsServiceImpl.java:839)
    at com.tooltank.spring.service.GroupAttachmentsServiceImpl.lambda$addAttachment$0(GroupAttachmentsServiceImpl.java:447)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IndexOutOfBoundsException
    at javax.imageio.stream.FileCacheImageOutputStream.seek(FileCacheImageOutputStream.java:170)
    at net.sourceforge.tess4j.util.ImageIOHelper.getImageByteBuffer(ImageIOHelper.java:297)
    at net.sourceforge.tess4j.Tesseract.setImage(Tesseract.java:397)
    at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:290)
    at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:212)
    ... 4 more

代码:

 private String testOcr(String fileLocation, int attachId) {
        try {
            File imageFile = new File(fileLocation);
            BufferedImage img = ImageIO.read(imageFile);
            BufferedImage blackNWhite = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
            Graphics2D graphics = blackNWhite.createGraphics();
            graphics.drawImage(img, 0, 0, null);
            String identifier = String.valueOf(new BigInteger(130, random).toString(32));
            String blackAndWhiteImage = previewPath + identifier + ".png";
            File outputfile = new File(blackAndWhiteImage);
            ImageIO.write(blackNWhite, "png", outputfile);

            ITesseract instance = new Tesseract();
            // Point to one folder above tessdata directory, must contain training data
            instance.setDatapath("/usr/share/tesseract-ocr/");
            // ISO 693-3 standard
            instance.setLanguage("deu");
            String result = instance.doOCR(outputfile);
            result = result.replaceAll("[^a-zA-Z0-9öÖäÄüÜß@\\s]", "");
            Files.delete(new File(blackAndWhiteImage).toPath());
            GroupAttachments groupAttachments = this.groupAttachmentsDAO.getAttachmenById(attachId);
            System.out.println("OCR Result is "+result);
            if (groupAttachments != null) {
                saveIndexes(result, groupAttachments.getFileName(), null, groupAttachments.getGroupId(), false, attachId);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();

        }
        return null;
    }

谢谢.

解决方法:

由于Java Image IO中的错误(已在Java 9中修复),因此当前版本的Java Tesseract Wrapper(写此答案时为3.4.0)不适用于< Java9.要使用较低的Java版本,您可以尝试对Tesseract ImageIOHelper类进行以下修复.只需在您的项目中复制该类并应用必要的更改,它就可以顺利地与文件和BufferedImages一起使用. 注意:此版本不使用原始类中使用的Tiff优化,可以在项目需要时添加它.

public static ByteBuffer getImageByteBuffer(RenderedImage image) throws IOException {
    //Set up the writeParam
    if (image instanceof BufferedImage) {
        return convertImageData((BufferedImage) image);
    }
    ColorModel cm = image.getColorModel();
    int width = image.getWidth();
    int height = image.getHeight();
    WritableRaster raster = cm
            .createCompatibleWritableRaster(width, height);
    boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
    Hashtable properties = new Hashtable();
    String[] keys = image.getPropertyNames();
    if (keys != null) {
        for (int i = 0; i < keys.length; i++) {
            properties.put(keys[i], image.getProperty(keys[i]));
        }
    }
    BufferedImage result = new BufferedImage(cm, raster,
            isAlphaPremultiplied, properties);
    image.copyData(raster);
    return convertImageData(result);
}
上一篇:求助!!!如何在Windows上运行mirai


下一篇:大屏25JAVA+selenium+tess4j识别登陆验证码截图与识别