snaef
asked on
Read/convert/write images using ImageIO
Hello,
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();
System.out.println(f.getFi leName());
BufferedImage image = ImageIO.read(in);
response.setContentType("i mage/jpeg" );
ImageIO.write(image,"JPEG" ,response. getOutputS tream());
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.
Thanks,
Scott
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();
System.out.println(f.getFi
BufferedImage image = ImageIO.read(in);
response.setContentType("i
ImageIO.write(image,"JPEG"
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.
Thanks,
Scott
Just read the source gif image in the normal way in Java, create a new BufferedImage of the same size, paint the source graphics context to the target one and then write it to the stream as a JPEG
ASKER
objects,
My problem is not encoding the Image as a gif, I do not want to return a gif to the client I want to return a JPG craeted from a gif.
CEHJ,
Can you provide sample code?
My problem is not encoding the Image as a gif, I do not want to return a gif to the client I want to return a JPG craeted from a gif.
CEHJ,
Can you provide sample code?
your code should work fine, what exactly is the erro you get?
following shows how to scale your image
http://www.javaalmanac.com/egs/java.awt.image/CreateTxImage.html
http://www.javaalmanac.com/egs/java.awt.image/CreateTxImage.html
> I know that the GIF is getting read properly as I can inspect in memory and it looks fine.
How exactly are you doing this?
How exactly are you doing this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
How is that going to make a difference?
Are you saying that ImageIO cannot read GIF's?
I never tried. I know (unless things changed recently) that it can't write them
> I never tried.
It can. And if you actually have no idea its probably best to state that up front instead of suggesting unnecessary (and expensive) alternatives :)
> I know (unless things changed recently) that it can't write them
Already been mentioned.
It can. And if you actually have no idea its probably best to state that up front instead of suggesting unnecessary (and expensive) alternatives :)
> I know (unless things changed recently) that it can't write them
Already been mentioned.
>> instead of suggesting unnecessary (and expensive) alternatives :)
LOL. Better than suggesting the use of Gif encoders
So all you need to do is read the gif with ImageIO and write it as JPEG snaef
LOL. Better than suggesting the use of Gif encoders
So all you need to do is read the gif with ImageIO and write it as JPEG snaef
Try the solution posted here:
https://www.experts-exchange.com/questions/20878039/converting-and-resizing-a-Gif-image.html?
Funnily in this question CEHJ suggested using ImageIO to read the gif, go figure :D
https://www.experts-exchange.com/questions/20878039/converting-and-resizing-a-Gif-image.html?
Funnily in this question CEHJ suggested using ImageIO to read the gif, go figure :D
There's no great mystery - i just happened to have forgotten that ImageIO can read images, that's all
(read images of the gif kind that is ;-))
The question actually states its being used (successfully) to read images,
then you say you have never tried to read images
and now you say you have forgotten that it can read images
ROTFL
then you say you have never tried to read images
and now you say you have forgotten that it can read images
ROTFL
yes, ImageIO does not support it I believe.
> I was under the impression that once the image is in the BufferedImage format it could be written in any way.
As long as an encoder exists.