Fuzzy_VB
asked on
converting and resizing a Gif image
hi, i want to convert a gif image to a jpg and then resize it to thumbnail size.
i tried using the ImageIO to read a gif and write a jpg but the jpg ends up being a broken image.
i dont mind even saving it as a gif.
but how do i resize it?
i am using the ACME GifEncoder to write the image
working code:
BufferedImage input = ImageIO.read(new File("c:/test.gif"));
FileOutputStream fos = new FileOutputStream(new File("c:/test2.gif"));
GifEncoder encoder = new GifEncoder(input,fos);
encoder.encode();
main prize goes to anyone who can show me how to resize and save it as a jpg. but even gif will do.
thanks.
-Fuzz
i tried using the ImageIO to read a gif and write a jpg but the jpg ends up being a broken image.
i dont mind even saving it as a gif.
but how do i resize it?
i am using the ACME GifEncoder to write the image
working code:
BufferedImage input = ImageIO.read(new File("c:/test.gif"));
FileOutputStream fos = new FileOutputStream(new File("c:/test2.gif"));
GifEncoder encoder = new GifEncoder(input,fos);
encoder.encode();
main prize goes to anyone who can show me how to resize and save it as a jpg. but even gif will do.
thanks.
-Fuzz
Sorry - line 2 above is redundant
ASKER
thanks... but the main question is how to resize it before saving it in any format.
Thanks
Thanks
Oh OK, sorry - i thought you didn't have the to-jpg working at all.
See the following source. In your case, you can dispense with the jpeg encoding classes, as you're using ImageIO
http://www.geocities.com/marcoschmidt.geo/java-save-jpeg-thumbnail.html
See the following source. In your case, you can dispense with the jpeg encoding classes, as you're using ImageIO
http://www.geocities.com/marcoschmidt.geo/java-save-jpeg-thumbnail.html
ASKER
thanks for your help CEHJ
i have tried saving it as a jpg the same way you showed me.. and i have seen that link before.. if you can tell me why my jpg ends up as a broken picture i will award you the points.
no exceptions are thrown at all, and the file is created but the picture does not work.
-Fuzz :)
i have tried saving it as a jpg the same way you showed me.. and i have seen that link before.. if you can tell me why my jpg ends up as a broken picture i will award you the points.
no exceptions are thrown at all, and the file is created but the picture does not work.
-Fuzz :)
If you can post a compilabe example for me to test, i'll have a look
ASKER
here is the convert method and what i imported.
-------------------------- ---------- ----------
import javax.imageio.*;
import java.awt.image.BufferedIma ge;
import java.io.*;
public void convert(){
try{
File inputFile = new File("c:/test.gif");
BufferedImage input = ImageIO.read(inputFile);
ImageIO.write(input, "jpg", new File("c:/test2.jpg"));
}catch(Exception e){
System.out.println(e);
}
}
-------------------------- ---------- ---------- ---
-Fuzz
--------------------------
import javax.imageio.*;
import java.awt.image.BufferedIma
import java.io.*;
public void convert(){
try{
File inputFile = new File("c:/test.gif");
BufferedImage input = ImageIO.read(inputFile);
ImageIO.write(input, "jpg", new File("c:/test2.jpg"));
}catch(Exception e){
System.out.println(e);
}
}
--------------------------
-Fuzz
...but that's without the thumbnail stuff of course..?
ASKER
yes... i think first things first :)
i cant resize the jpg if a jpg doesnt work.. if it works, i will then use the thumbnail example you posted.
-Fuzz
i cant resize the jpg if a jpg doesnt work.. if it works, i will then use the thumbnail example you posted.
-Fuzz
It's a pity to say, but i've found the images libraries are simply not very good, so this doesn't surprise me.
Have you tried the original code at that link?
Have you tried the original code at that link?
ASKER
yes.. and that code works like a charm.
that if i test it with an existing jpg.
but converting a gif to a jpg is the problem.
i take it you tried my code and it never worked for you?
-Fuzz
that if i test it with an existing jpg.
but converting a gif to a jpg is the problem.
i take it you tried my code and it never worked for you?
-Fuzz
>>i take it you tried my code and it never worked for you?
I haven't as if it doesn't work satisfactorily for you, it's not going to for me ;-)
>>but converting a gif to a jpg is the problem
meaning if you use the same code, but start with the gif it then doesn't work?
I haven't as if it doesn't work satisfactorily for you, it's not going to for me ;-)
>>but converting a gif to a jpg is the problem
meaning if you use the same code, but start with the gif it then doesn't work?
ASKER
thats correct.
the thumnail code works.
but
ImageIO.write(input, "jpg", new File("c:/test2.jpg"));
does not... if i convert it to a "png" it works... but i need a gif or a jpg.
and i need to resize it.
if it is possible to even keep it as a gif thats fine, but how do i resize it.
-Fuzz
the thumnail code works.
but
ImageIO.write(input, "jpg", new File("c:/test2.jpg"));
does not... if i convert it to a "png" it works... but i need a gif or a jpg.
and i need to resize it.
if it is possible to even keep it as a gif thats fine, but how do i resize it.
-Fuzz
>>but
ImageIO.write(input, "jpg", new File("c:/test2.jpg"));
does not...
Well my last suggestion was as follows
a. Read gif using ImageIO
b. Write jpg using JPEGEncoder (i.e. *not* ImageIO)
Did you try that?
ImageIO.write(input, "jpg", new File("c:/test2.jpg"));
does not...
Well my last suggestion was as follows
a. Read gif using ImageIO
b. Write jpg using JPEGEncoder (i.e. *not* ImageIO)
Did you try that?
>>if it is possible to even keep it as a gif thats fine, but how do i resize it.
And that you could do using the 'thumbnail part' of the code at that link.
And that you could do using the 'thumbnail part' of the code at that link.
Where are we at on this one now Fuzzy_VB?
ASKER
im trying out the JPEGEncoder here..
http://forum.java.sun.com/thread.jsp?thread=263816&forum=20&message=998474
i havnt forgotten ;)
http://forum.java.sun.com/thread.jsp?thread=263816&forum=20&message=998474
i havnt forgotten ;)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok... everything is working
here is the very untidy example for those who need it
try{
File inputFile = new File("c:/test.gif");
Image img = ImageIO.read(inputFile);
BufferedImage bImg = new BufferedImage(img.getWidth (null),img .getHeight (null),Buf feredImage .TYPE_INT_ RGB);
Graphics g;
FileOutputStream jpegOut;
jpegOut = new FileOutputStream("c:/test. jpg");
g = bImg.createGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
JPEGImageEncoder encoder1 =
JPEGCodec.createJPEGEncode r(jpegOut) ;
JPEGEncodeParam param1 =
encoder1.getDefaultJPEGEnc odeParam(b Img);
param1.setQuality(0.75f, true);
encoder1.encode(bImg, param1);
jpegOut.close();
// this is where the resizing is done
String args[] = {"c:/test.jpg","c:/test2.j pg","80"," 80","80"};
Image image = Toolkit.getDefaultToolkit( ).getImage (args[0]);
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(imag e, 0);
mediaTracker.waitForID(0);
// determine thumbnail size from WIDTH and HEIGHT
int thumbWidth = Integer.parseInt(args[2]);
int thumbHeight = Integer.parseInt(args[3]);
double thumbRatio = (double)thumbWidth / (double)thumbHeight;
int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
double imageRatio = (double)imageWidth / (double)imageHeight;
if (thumbRatio < imageRatio) {
thumbHeight = (int)(thumbWidth / imageRatio);
} else {
thumbWidth = (int)(thumbHeight * imageRatio);
}
// draw original image to thumbnail image object and
// scale it to the new size on-the-fly
BufferedImage thumbImage = new BufferedImage(thumbWidth,
thumbHeight, BufferedImage.TYPE_INT_RGB );
Graphics2D graphics2D = thumbImage.createGraphics( );
graphics2D.setRenderingHin t(Renderin gHints.KEY _INTERPOLA TION,
RenderingHints.VALUE_INTER POLATION_B ILINEAR);
graphics2D.drawImage(image , 0, 0, thumbWidth, thumbHeight, null);
// save thumbnail image to OUTFILE
BufferedOutputStream out = new BufferedOutputStream(new
FileOutputStream(args[1])) ;
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncode r(out);
JPEGEncodeParam param = encoder.
getDefaultJPEGEncodeParam( thumbImage );
int quality = Integer.parseInt(args[4]);
quality = Math.max(0, Math.min(quality, 100));
param.setQuality((float)qu ality / 100.0f, false);
encoder.setJPEGEncodeParam (param);
encoder.encode(thumbImage) ;
System.out.println("Done." );
System.exit(0);
}
catch (Exception e)
{
e.printStackTrace();
}
thanks CEHJ
here is the very untidy example for those who need it
try{
File inputFile = new File("c:/test.gif");
Image img = ImageIO.read(inputFile);
BufferedImage bImg = new BufferedImage(img.getWidth
Graphics g;
FileOutputStream jpegOut;
jpegOut = new FileOutputStream("c:/test.
g = bImg.createGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
JPEGImageEncoder encoder1 =
JPEGCodec.createJPEGEncode
JPEGEncodeParam param1 =
encoder1.getDefaultJPEGEnc
param1.setQuality(0.75f, true);
encoder1.encode(bImg, param1);
jpegOut.close();
// this is where the resizing is done
String args[] = {"c:/test.jpg","c:/test2.j
Image image = Toolkit.getDefaultToolkit(
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(imag
mediaTracker.waitForID(0);
// determine thumbnail size from WIDTH and HEIGHT
int thumbWidth = Integer.parseInt(args[2]);
int thumbHeight = Integer.parseInt(args[3]);
double thumbRatio = (double)thumbWidth / (double)thumbHeight;
int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
double imageRatio = (double)imageWidth / (double)imageHeight;
if (thumbRatio < imageRatio) {
thumbHeight = (int)(thumbWidth / imageRatio);
} else {
thumbWidth = (int)(thumbHeight * imageRatio);
}
// draw original image to thumbnail image object and
// scale it to the new size on-the-fly
BufferedImage thumbImage = new BufferedImage(thumbWidth,
thumbHeight, BufferedImage.TYPE_INT_RGB
Graphics2D graphics2D = thumbImage.createGraphics(
graphics2D.setRenderingHin
RenderingHints.VALUE_INTER
graphics2D.drawImage(image
// save thumbnail image to OUTFILE
BufferedOutputStream out = new BufferedOutputStream(new
FileOutputStream(args[1]))
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncode
JPEGEncodeParam param = encoder.
getDefaultJPEGEncodeParam(
int quality = Integer.parseInt(args[4]);
quality = Math.max(0, Math.min(quality, 100));
param.setQuality((float)qu
encoder.setJPEGEncodeParam
encoder.encode(thumbImage)
System.out.println("Done."
System.exit(0);
}
catch (Exception e)
{
e.printStackTrace();
}
thanks CEHJ
No problem. Pity it doesn't produce satisfactory results with ImageIO :-(
btw, are your *sure* it doesn't if you just use ImageIO.write from here onwards?:
>>// save thumbnail image to OUTFILE
btw, are your *sure* it doesn't if you just use ImageIO.write from here onwards?:
>>// save thumbnail image to OUTFILE
BufferedImage input = ImageIO.read(new File("c:/test.gif"));
FileOutputStream fos = new FileOutputStream(new File("c:/test2.gif"));
ImageIO.write(input, "jpg", new File("c:/test2.jpg"));