Java scale picture and output, support JPG and PNG input and same type output
Import Java.io.File;
Import java.io.IOException;
Import Java.awt.image.BufferedImage;
Import Java.awt.Image;
Import java.awt.image.AffineTransformOp;
Import Javax.imageio.ImageIO;
Import cn.sharcom.config.Constant;
Import Java.awt.geom.AffineTransform;
/** * * @param strdestfile * source file * @param strsourcefile * Target file * @param width * Output size Width * @param height * Output Size Height * @return/public static Integer ImageZoom1 (Str
ing strDestFile, String strsourcefile, int width, int height) {Integer result = null;
Open source File FileInput = new file (strsourcefile);
if (fileinput.isfile () = = False) {String msg = String.Format ("Imagezoom No found file%s", strsourcefile);
result = Constant.result_none;
return result;
}//suffix default jpg String strsuffix = "jpg";
1+ eliminates "."
int point = Strsourcefile.lastindexof (".") + 1; Get extension if (Point > 0 && Point < Strsourcefile.length ()) {int len = Strsourcefile.len based on incoming filename)
Gth ();
String Strfiletype = strsourcefile.substring (point, Len);
if (strfiletype.length () > 0) {strfiletype = Strfiletype.tolowercase (); Get a new extension strsuffix = Strfiletype;
}//Open file Fileoutput = null;
Read the picture bufferedimage bufferedimage = null;
try {fileoutput = new File (strdestfile);
BufferedImage = Imageio.read (fileInput);
catch (IOException e) {e.printstacktrace (); String msg = String.Format ("Imagezoom failed to read the file.")
%s ", strsourcefile);
result = Constant.result_error;
return result;
Image image = Bufferedimage.getscaledinstance (width, height, bufferedimage.scale_smooth);
Gets the original dimension int intheightold = Bufferedimage.getheight ();
int intwidthold = Bufferedimage.getwidth ();
Calculates the new dimension scale double Intwidthratio = (double) width/double intwidthold;
Double intheightratio = (double) height/(double) intheightold; Set proportional Affinetransformop affinetransformop = new Affinetransformop (affinetransform.getscaleinstance (IntWidthRatio, I
Ntheightratio), NULL); Image = AffInetransformop.filter (bufferedimage, NULL);
try {///output the file as a specified type Imageio.write ((bufferedimage) image, Strsuffix, fileoutput);
Set return tag result = constant.result_success; catch (Exception e) {String msg = String.Format ("Imagezoom write error%s,%s", E.getmessage (), Strdestfi
Le);
result = Constant.result_error;
return result;
return result;
public static void Main (string[] args) {ImageZoom1 ("c:/b.jpg", "c:/1239501964fe4306.jpg", 3000, 4000); }
Q Group discussion: 236201801