Exception from the ImageIOHelper class

Malshani
2012-07-09
2013-05-22
  • Malshani
    Malshani
    2012-07-09

    while i was testing with tess4j for my app i found the following exception for the following image. Are there any image formats which are not supported for tess4j? What can be the reason for this situation and how can i solve this?
    Exception in thread "main" java.lang.ClassCastException: java.awt.image.DataBufferUShort cannot be cast to java.awt.image.DataBufferByte
    at Tess4j.vietocr.ImageIOHelper.convertImageData(ImageIOHelper.java:223)
    at Tess4j.vietocr.ImageIOHelper.getImageByteBuffer(ImageIOHelper.java:213)
    at Tess4j.resources.Tesseract1.doOCR(Tesseract1.java:195)
    at Tess4j.resources.Tesseract1.doOCR(Tesseract1.java:174)
    at Tess4j.resources.Tesseract1.doOCR(Tesseract1.java:157)
    at imageprocessor.ImageProcessor.processImage(ImageProcessor.java:95)
    at imageprocessor.ImageProcessor.main(ImageProcessor.java:55)

     
  • Quan Nguyen
    Quan Nguyen
    2012-08-04

    The API expects pixel data as DataBufferByte. You need to send the input in correct type.

     
  • Eduardo Mineo
    Eduardo Mineo
    2013-02-07

    Hi Malshani, I was struggling against this problem and I have finally got a workaround.

    The problem is that ImageIO.read reads an image using one of the available readers in ClassLoader and the resulting BufferedImage can be created using any kind of DataBuffer, depending on the selected reader implementation in ClassLoader.

    In net.sourceforge.vietocr.ImageIOHelper, the method convertImageData expects a BufferedImage with a DataBufferByte, but I have to be sure it will be there!

    In the ending of the calling method, getImageByteBuffer, you will see that a BufferedImage is created by ImageIO.read. That's the problem. The following code will assure the BufferedImage has a DataBufferByte

    ImageReader tiffReader = null;
    Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(TIFF_FORMAT);
    while (readers.hasNext()) {
        ImageReader reader = readers.next();
        if (reader instanceof com.sun.media.imageioimpl.plugins.tiff.TIFFImageReader) {
            tiffReader = reader;
        }
    }
    
    //        BufferedImage bi = ImageIO.read(ios);
    tiffReader.setInput(ios, true);
    BufferedImage bi = tiffReader.read(0);
    return convertImageData(bi);
    
     
  • Quan Nguyen
    Quan Nguyen
    2013-03-04

    I tried your workaround but encountered the same exception. What I ended up doing was convert the image to binary or grayscale before OCR.

     
    • maxpaynebupt
      maxpaynebupt
      2013-05-21

      hi, quan
      Could you please post your solution, I came into the same problem.

       
      • Quan Nguyen
        Quan Nguyen
        2013-05-22

        I used either ImageHelper.convertImageToBinary or convertImageToGrayscale.