I wrote a method to read images. I loop through a List of image-files and read them. After some iteration an ArrayIndexOutOfBoundsException appears when calling the Raster.setRect(Raster raster) method. The dimension and the bounds of the images are fine - see stacktrace.
public static BufferedImage readImage(File imageFile) {
// Find a suitable ImageReader
Iterator<ImageReader> readers = ImageIO.getImageReadersBySuffix("jpg");
ImageReader reader = null;
while (readers.hasNext()) {
reader = (ImageReader) readers.next();
if (reader.canReadRaster()) {
break;
}
}
// Stream the image file (the original CMYK image)
ImageInputStream input = null;
try {
input = ImageIO.createImageInputStream(imageFile);
} catch (IOException e) {
logger.error("Error creating InputStream on File {}", imageFile
.getName());
e.printStackTrace();
}
reader.setInput(input);
// Read the image raster
Raster raster = null;
try {
raster = reader.readRaster(0, null);
} catch (IOException e) {
logger
.error("Error reading Raster of file {}", imageFile
.getName());
e.printStackTrace();
}
// Create a new RGB image
BufferedImage bi = new BufferedImage(raster.getWidth(), raster
.getHeight(), BufferedImage.TYPE_INT_RGB);
// Fill the new image with the old raster
logger.debug("Height {} and width {} of original raster", raster
.getHeight(), raster.getWidth());
logger.debug("Height {} and width {} of new raster", bi.getRaster()
.getHeight(), bi.getRaster().getWidth());
logger.debug("NumBands original raster {}", bi.getRaster().getBounds());
logger.debug("NumBands new raster {}", bi.getRaster().getBounds());
bi.getRaster().setRect(raster);
// Close and flush the reader
try {
input.close();
} catch (IOException e) {
logger.error("Error closing the reader for file {}", imageFile
.getName());
e.printStackTrace();
}
return bi;
}
Here the stacktrace:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 310
at java.awt.image.SinglePixelPackedSampleModel.setPixels(SinglePixelPackedSampleModel.java:689)
at java.awt.image.WritableRaster.setPixels(WritableRaster.java:565)
at java.awt.image.WritableRaster.setRect(WritableRaster.java:467)
at java.awt.image.WritableRaster.setRect(WritableRaster.java:403)
at .. .utils.ImageUtils.readImage(ImageUtils.java:228)
at .. .image.scripts.ImageMatchingScript.main(ImageMatchingScript.java:86)
All the images have the same dimension 371x310
Here the debug information I printed:
13:53:17.065 [main] DEBUG .. .ImageUtils - Height 371 and width 310 of original raster
13:53:17.065 [main] DEBUG .. .ImageUtils - Height 371 and width 310 of new raster
13:53:17.065 [main] DEBUG .. .ImageUtils - NumBands original raster java.awt.Rectangle[x=0,y=0,width=310,height=371]
13:53:17.065 [main] DEBUG .. .ImageUtils - NumBands new raster java.awt.Rectangle[x=0,y=0,width=310,height=371]
The documentation says: ArrayIndexOutOfBoundsException - if the coordinates are not in bounds, or if fArray is too small to hold the input.
For me the dimensions/bounds seem fine, maybe its a problem with the fArray?