Java exception "Exception in thread" main "java.lang.ClassCastException: [B cannot be passed to [I" when java.awt.image.BufferedImage.copyData () is called

In the following code, I am trying to combine 1024 * 1024 png into several large pngs. Code with this exception:

Exception in thread "main" java.lang.ClassCastException: [B cannot be cast to [I at sun.awt.image.IntegerInterleavedRaster.setDataElements(Unknown Source) at java.awt.image.BufferedImage.copyData(Unknown Source) at mloc.bs12.mapimagemerger.Merger.main(Merger.java:27) 

This is probably something small and stupid that I forgot, but I can not find anything bad in the code. Code:

 import java.awt.image.*; import javax.imageio.*; import java.io.*; public class Merger { public static void main(String[] args) { String toX, toY, toZ; try { toX = args[0]; toY = args[1]; toZ = args[2]; } catch(ArrayIndexOutOfBoundsException E) { //E.printStackTrace(); toX = "3"; toY = "5"; toZ = "4"; } int yproper = 1; for(int z = 1; z <= Integer.parseInt(toZ); z++) { BufferedImage img = new BufferedImage(Integer.parseInt(toX) * 1024, Integer.parseInt(toY) * 1024, BufferedImage.TYPE_INT_RGB); for(int x = 1; x <= Integer.parseInt(toX); x++) { for(int y = 1; y <= Integer.parseInt(toY); y++) { BufferedImage simg = img.getSubimage(x*1024, y*1024, 1024, 1024); BufferedImage tempimg = loadImage(x + "-" + y + "-" + z + ".png"); WritableRaster rsimg = simg.getRaster(); rsimg = tempimg.copyData(rsimg); <-- Error! yproper++; } } saveImage(img, z + ".png"); } } public static BufferedImage loadImage(String path) { BufferedImage bimg = null; try { bimg = ImageIO.read(new File(path)); } catch (Exception e) { e.printStackTrace(); } return bimg; } public static void saveImage(BufferedImage img, String path) { try { ImageIO.write(img, "png", new File(path)); } catch (Exception e) { e.printStackTrace(); } return; } } 

I think I already understood this. The images I uploaded were not the same as the images I created. (I'm still not sure what type they are, what type 13 is?) I have a few more problems, but this bug has been fixed. (More problems, as in this .)

+4
source share
1 answer

The library passes an array of bytes to an int array, which you cannot do.

I am not familiar with BufferedImage, but a qualified assumption would be that the PNG file you are reading is treated as byte values ​​instead of integer values.

+3
source

All Articles