?
Solved

Read/convert/write images using ImageIO

Posted on 2005-04-29
19
Medium Priority
?
771 Views
Last Modified: 2008-02-01
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.getFileName());
BufferedImage image = ImageIO.read(in);
response.setContentType("image/jpeg");
ImageIO.write(image,"JPEG",response.getOutputStream());

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
0
Comment
Question by:snaef
  • 10
  • 6
17 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13898377
> Do I need to use a special encoder that can transcode the gif data?  

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.
0
 
LVL 92

Expert Comment

by:objects
ID: 13898389
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13898780
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
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 1

Author Comment

by:snaef
ID: 13898950
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?  
0
 
LVL 92

Expert Comment

by:objects
ID: 13898973
your code should work fine, what exactly is the erro you get?
0
 
LVL 92

Expert Comment

by:objects
ID: 13898979
0
 
LVL 92

Expert Comment

by:objects
ID: 13898982
>  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?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1500 total points
ID: 13899007
Image i = new ImageIcon("x.gif").getImage();
BufferedImage bi = new BufferedImage(i.getWidth(null), i.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
g.drawImage(i, 0, 0, null);
ImageIO.write(bi, "JPG", out);
0
 
LVL 92

Expert Comment

by:objects
ID: 13899014
How is that going to make a difference?
0
 
LVL 92

Expert Comment

by:objects
ID: 13899037
Are you saying that ImageIO cannot read GIF's?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13899098
I never tried. I know (unless things changed recently) that it can't write them
0
 
LVL 92

Expert Comment

by:objects
ID: 13899117
> 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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13899131
>> 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
0
 
LVL 92

Expert Comment

by:objects
ID: 13899210
Try the solution posted here:

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20878039.html?

Funnily in this question CEHJ suggested using ImageIO to read the gif, go figure :D
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13899225
There's no great mystery - i just happened to have forgotten that ImageIO can read images, that's all
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13899229
(read images of the gif kind that is ;-))
0
 
LVL 92

Expert Comment

by:objects
ID: 13899231
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

0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month3 days, 11 hours left to enroll

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question