This might sound stupid, but "please" cope with me.
I got an image in jpg format which I want to read in an array or something (not sure)
I only want the bytes of the image, not the headers, size, thumb etc.
Basically what I'm trying to do is:
Read the image, change a few colors by changing the "bytes", & then save it.

On wickypedia it says that jpeg format begins with a 0xFF byte, but I'm not able to find this in the images I read. All I get is numbers, symbols etc...

Here's my code so far (totally idiotic)

InputStream in = new FileInputStream("test_file.jpg");
Reader reader = new InputStreamReader(in);
int r; byte b; char c;
while ((r = reader.read()) != -1){
b= (byte) r; c= (char) r;
System.out.println(" "+ b + " , " + r + " , " + c );
}catch (Exception e){ }

Am I missing something here, or is it totally in opposite direction?
Do I need to decompress/unzip the image data before reading it, or something else?

Please help me.
Thank you

4 Years
Discussion Span
Last Post by erms

Fortunately Sun/Oracle have already written 99% of the code you need. It's all in the standard API.
Use the ImageIO class to read the file into memory, ImageIO will handle all the details of decompressing the jpeg for you.That gives you an in-memory BufferedImage, so you can use its getRGB and setRGB methods to access or update individual pixels. See the Java API doc for details.

Edited by JamesCherrill: Changed for updating image


Billion thanks!!!
I tried the javax.imageio API & the hard part is already done!
Thank you so much

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.