Question

I'm having a really bad time dealing with RGB values in Java, which made me start trying small experiments with this.

I came down to this: loading an image, get it's rgb values and creating a new image with the same values. Unfortunately, this does not work (the images are displayed differently, see picture), as per the following code... Can some one see what's wrong?

BufferedImage oriImage=ImageIO.read(new  URL("http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png"));

int[] oriImageAsIntArray = new int[oriImage.getWidth()*oriImage.getHeight()];
oriImage.getRGB(0, 0, oriImage.getWidth(),oriImage.getHeight(), oriImageAsIntArray, 0, 1);

BufferedImage bfImage= new BufferedImage(oriImage.getWidth(),oriImage.getHeight(),
            BufferedImage.TYPE_INT_ARGB);

bfImage.setRGB(0,0,bfImage.getWidth(),bfImage.getHeight(),oriImageAsIntArray, 0, 1);

output

No correct solution

OTHER TIPS

Apparently, getRGB and setRGB were not being used correctly.

I changed the code to:

oriImage.getRGB(0, 0, oriImage.getWidth(),oriImage.getHeight(), oriImageAsIntArray, 0,  oriImage.getWidth());
(...)
bfImage.setRGB(0,0,bfImage.getWidth(),bfImage.getHeight(),oriImageAsIntArray, 0, bfImage.getWidth());

... and the picture displayed correctly. I still do not understand what this last argument is. In the JavaDoc, it is described as:

scansize - scanline stride for the rgbArray
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top