Original address: http://www.blogjava.net/PrettyBoyCyb/archive/2006/11/13/80922.html
Picture processing in Java is a very frustrating thing to do. Currently, ImageIO in the Java API can convert GIF images to PNG images, and JPG images can be converted as normal. GIF to JPG is said to have a way, but to turn jpg into GIF, I took a lot of time to find a good solution.
The first introduction is a good code to scale the picture:
public static BufferedImage Resize (bufferedimage source, int targetw, int targeth){
Targetw,targeth indicates the target length and width, respectively.
int type = Source.gettype ();
BufferedImage target = null;
Double SX = (double) targetw/source.getwidth ();
Double sy = (double) targeth/source.getheight ();
This is where you want to achieve equal-to-scale scaling within the Targetw,targeth range. If you do not need to wait for scaling
Then comment The following if Else statement
if (Sx>sy)
{
SX = SY;
TARGETW = (int) (SX * SOURCE.GETWIDTH ());
}else{
SY = SX;
Targeth = (int) (SY * source.getheight ());
}
if (type = = Bufferedimage.type_custom){//handmade
ColorModel cm = Source.getcolormodel ();
WritableRaster raster = Cm.createcompatiblewritableraster (TARGETW, Targeth);
Boolean alphapremultiplied = Cm.isalphapremultiplied ();
target = new BufferedImage (cm, raster, alphapremultiplied, null);
} else
target = new BufferedImage (TARGETW, Targeth, type);
Graphics2D g = target.creategraphics ();
Smoother than Exlax:
G.setrenderinghint (Renderinghints.key_interpolation, renderinghints.value_interpolation_bicubic);
G.drawrenderedimage (source, affinetransform.getscaleinstance (SX, SY));
G.dispose ();
return target;
}
Next, Save the InputStream as a jpg file
public static void Saveimageasjpg (InputStream in, File savefile,int width,int hight)
Throws Exception{
BufferedImage Srcimage;
Srcimage = Imageio.read (in);
if (Width > 0 | | hight > 0)
{
Srcimage = Resize (srcimage, width, hight);
}
Imageio.write (Srcimage, "JPEG", saveFile);
In.close ();
}
Parameter explanation:
In:: is a JPG image of the InputStream
SaveFile:: Target file
Width:: Target width, if no zoom is required 0
Hight:: Target height, if no scaling is required 0
then save the InputStream as a GIF file:
private static void Saveimageasgif (InputStream in, File filetosave,int width, int hight)
Throws Exception {
BufferedImage Srcimage;
Srcimage = Imageio.read (in);
if (width>0 && hight >0)
{
Srcimage = Resize (srcimage, width, hight);
}
FileOutputStream out = new FileOutputStream (filetosave);
Gifencoder encoder = new Gifencoder (srcimage, out);
Encoder.encode ();
In.close ();
}
Parameter explanation:
in:: is a jpg or gif image of the InputStream
saveFile:: Target file
Width:: Target width, if no zoom is required 0
hight:: Target height, if no scaling is required 0
Gifencoder This class is not in the Java API , and I have been looking for a long time. It is Acme.JPM.Encoders.GifEncoder, just find this package, convert jpg to gif is not a problem.
Jpg Images use 24-bit encoding,PNG has png-24 and png-8 two kinds, but gif is 8-bit the encoding. If you force the jpg image stream to be split into a byte, it can be converted into a gif image, and there will be severe distortion even with standard color.
I used to Gif4j_light_trial_1.0.jar this bag, but I was disappointed with the package. Although it can synthesize multiple images into a animated gif , it is paid for and is available for one months free of charge. And, after using it to convert a jpg image into a gif , there will be a white horizontal line in the middle, not knowing if this is a system flaw or the hands and feet of the cost package.
The processing of pictures in Java