Ok I know that the topic is a bit vague, but bear with me. I was wondering how can I get access to the raw data that an image is stored in on the hard drive. I am guessing that it is stored as a long list of hex values, but am prob wrong. Does anyone know exactly how the images is stored, and what the best way to access the raw data?
If you have a BufferedImage image:
byte[] data=((DataBufferByte)tex.getRaster().getDataBuffer()).getData()
Careful though you might need:
int[] data=((DataBufferInt)tex.getRaster().getDataBuffer()).getData()
or you’ll get a ClassCastException. Try one, if you get the exception, try the other ;D
That’ll give you the pixel data in int’s or byte’s directly tied to the Image’s raster.
great thanks.
Ok So I dont start a new thread, a quick question on dif topic:
If I write 1 byte of data to a file, it is stored as a 4kb file in Windows. I realize this is becasue of windows file structure, and am guessing that windows cannot have a file less than 4kb. Is this correct? can I store 1 byte into a file that takes up less than 4k on my windows machine?
deends on the cluster size of your filesystem, but default is 4kb, yes.
Ok back to the image question:
I created a 4 pixel image
R B
G G
when I get the byte array and display it I get the following
R 0 0 -2
B -4 0 1
G -1 0 3
G 3 -2 3
can anyone explain what this means? I was hoping to get values I can turn into binaries.
Can you post the code, so we know what you are doing.
File f = new File("image\\pixels.jpg");
BufferedImage src = javax.imageio.ImageIO.read(f);
byte[] data=((DataBufferByte)src.getRaster().getDataBuffer()).getData() ;
for (int i = 0; i < data.length; i++)
{
System.out.print(data[i] + " ");
}
You don’t want to use “getRaster().getDataBuffer()).getData()” on images that are loaded from file. Atleast not if you are a newbie. The problem is that the image can be stored in so many different ways. Finding how it is stored and how to interprit it can be a hassle. It is mostly used on images that you create your self that you need speedy access too, like a software backbuffer. Since you create the image yourself, you know how the data is stored.
Use int[] BufferedImage.getRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize) instead. It creates a copy of the image, and it the data is always stored as argb (alphe, red, green, blue). Then you have to shift and mask to get what you want.