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

时间:2017-06-06 08:01:48

标签: java tesseract tess4j

我正在开发一个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;
    }

谢谢。

2 个答案:

答案 0 :(得分:3)

由于Java Image IO中的一个错误(使用Java 9修复),当前版本的Java Tesseract Wrapper(编写此答案时为3.4.0)不适用于< Java 9.要使用较低的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);
}

答案 1 :(得分:0)

尝试升级到tess4j 3.4.1版。 这解决了我的问题。