[java] BufferedImage

Started by
2 comments, last by Vyper_uk 18 years ago
Can anyone help me out with understanding the buffered image class. I can use it to draw and load images but i need somehow to extract the array of pixels from inside of it. I need to do tests based on color of the pixels but it seems that the color is stored in an int variable. I understand that to make this variable for the pixel your pr together color arguments, but how do i find out the color of the pixel and use it to make a Color object? Ive serached on google and looked in a few books but found mostly info on creating Buffered Images. Thanks guys :-)
Advertisement
I took the following code from this tutorial.

int bytesPerPixel = -1, pix;				// 3 or 4 bytes per pixel?		if( img.getColorModel().hasAlpha() )			bytesPerPixel = 4;		else			bytesPerPixel = 3;			// Allocate a ByteBuffer		ByteBuffer unpackedPixels = 			ByteBuffer.allocateDirect(img.getWidth() * img.getHeight() * bytesPerPixel);				// Pack the pixels into the ByteBuffer in RGBA, 4 byte format.		for(int row = img.getHeight() - 1; row >= 0; row--) {			for (int col = 0; col < img.getWidth(); col++) {				pix = img.getRGB(col,row);  // Should return the pixel in format TYPE_INT_ARGB 				unpackedPixels.put((byte) ((pix >> 16) & 0xFF));      // red				unpackedPixels.put((byte) ((pix >> 8 ) & 0xFF));      // green				unpackedPixels.put((byte) ((pix      ) & 0xFF));      // blue				if (bytesPerPixel == 4) {					unpackedPixels.put((byte) ((pix >> 24) & 0xFF));  // alpha				}			}		}


The page's link.

Son Of Cain
a.k.a javabeats at yahoo.ca
im not so good with bytes, is there some way to use this data to make Color objects? It was helpfull though. Ty

Hey cant you just pass the int you get from the buffered image right to a color object? ill try that.
Bytes are nothing to worry about, just think of them as a very small int. And yes, just passing the int should work, although if you're creating a Color object for each pixel its likely to take up a fair bit of memory if you have large images

This topic is closed to new replies.

Advertisement