Skip to main content
gamedev.net
🔒 Locked

[java] Images

Started by Tipotas688 Mar 18, 2009 at 11:31 AM 0 replies 950+ views
Original Post
Tipotas688
Tipotas688
Hello, I am trying to get the pixels from a buffered image along with their RGB values but I seem to get stuck a bit with the code: BufferedImage bufferedImage = new BufferedImage( bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_BYTE_BINARY ); byte[] dd = ( ( DataBufferByte ) bufferedImage.getRaster().getDataBuffer() ).getData(); byte[][] pixels = new byte[dd.length][dd.length]; for(int j=0;j[j] = dd[i*bi.getHeight()+j]; Am I doing it ok so far? And how do I get the RGB values in a 2D array of integers. Then I want to save the picture into java's standard formats, do I go with streams?
Lord_Evil
Lord_Evil
byte[][] pixels = new byte[dd.length][dd.length]; that array seems too big, you should use the dimensions of the buffered image instead (multiplied by bytes per pixel). Your code can easily result in an OutOfMemoryError: Assume a 32-bit 128x128 pixel image. The number of bytes (dd.length) would be 4 * 128 * 128 = 65536 (64K). Your code would create a 65536x65536 array (4294967296 bytes or 4GB!).

for(int j=0;j why dd.length/8 ? You have bytes not bits.

If you have all the bytes you can pack 4 of them into an integer to get an array of integer values. But why do you need that array? Why don't you just use the Java API to store the BufferedImage in another format?
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.