Read/convert/write images using ImageIO
Posted on 2005-04-29
I am creating a servlet that will read an image, convert it to a jpeg and store it as a BLOB in MySQL. I am new to java and am currently testing how to manipulate images before I tackle the next part of storing it in the DB
So far I am just trying to read either a GIF or JPG image and write it back out to the browser as a JPG. So far I have:
InputStream in = f.getInputStream();
BufferedImage image = ImageIO.read(in);
If I test this uisng a jpeg it works fine, but when I try to load a gif I get back a file encoding error in the browser. I know that the GIF is getting read properly as I can inspect in memory and it looks fine.
Do I need to use a special encoder that can transcode the gif data? I was under the impression that once the image is in the BufferedImage format it could be written in any way.
I'm missing something, but just dont know what.
Eventually I will need to scale the image before I save/send it.