Because the site needs to upload the image width to determine the scale and quality compression, in order to improve the overall loading speed, so I look for processing methods on the Internet,
Most of the Internet is the introduction of Google Image processing component thumbnails. At first I tried with thumbnails, but I didn't know what the reason was, no effect, no error.
Because of the relationship between time, no further study, another find his way. Later found the following method, the advantage of this method is completely based on the Java API, the call is very simple, if only scaling and compression, this method is sufficient.
Code:
1 /**2 * Zoom picture (compress picture quality, change picture size)3 * Wakahara chart width is less than the new width, then the width is the same! 4 * @paramnewwidth New Width5 * @paramquality picture quality parameter 0.7f equivalent to 70% quality6 */7 Public Static voidimageresize (file originalfile, file Resizedfile,8 intNewwidth,floatQuality)throwsIOException {9 Ten if(Quality > 1) { One Throw NewIllegalArgumentException ( A"Picture quality must be set in 0.1-1 range"); - } - theImageIcon II =NewImageIcon (Originalfile.getcanonicalpath ()); -Image i =ii.getimage (); -Image Resizedimage =NULL; - + intIwidth = I.getwidth (NULL); - intIheight = I.getheight (NULL); + A if(Iwidth <newwidth) { atNewwidth =iwidth; - } - if(Iwidth >iheight) { -Resizedimage = I.getscaledinstance (Newwidth, (Newwidth *iheight) -/iwidth, Image.scale_smooth); -}Else { inResizedimage = I.getscaledinstance ((newwidth * iwidth)/Iheight, - newwidth, Image.scale_smooth); to } + - //This code ensures, the pixels in the image is loaded. theImage temp =NewImageIcon (resizedimage). GetImage (); * $ //Create the buffered image.Panax NotoginsengBufferedImage BufferedImage =NewBufferedImage (Temp.getwidth (NULL), -Temp.getheight (NULL), BUFFEREDIMAGE.TYPE_INT_RGB); the + //Copy image to buffered image. AGraphics g =bufferedimage.creategraphics (); the + //Clear Background and paint the image. - G.setcolor (color.white); $G.fillrect (0, 0, Temp.getwidth (NULL), Temp.getheight (NULL)); $G.drawimage (temp, 0, 0,NULL); - g.dispose (); - the //soften. - floatSoftenfactor = 0.05f;Wuyi float[] Softenarray = {0, softenfactor, 0, Softenfactor, the1-(Softenfactor * 4), Softenfactor, 0, softenfactor, 0 }; -Kernel Kernel =NewKernel (3, 3, Softenarray); WuConvolveop cOp =NewConvolveop (kernel, convolveop.edge_no_op,NULL); -BufferedImage = Cop.filter (BufferedImage,NULL); About $ //Write the JPEG to a file. -FileOutputStream out =NewFileOutputStream (resizedfile); - - //encodes image as a JPEG data stream AJPEGImageEncoder encoder =Jpegcodec.createjpegencoder (out); + theJPEGEncodeParam param =Encoder - . Getdefaultjpegencodeparam (bufferedimage); $ theParam.setquality (Quality,true); the the Encoder.setjpegencodeparam (param); the Encoder.encode (bufferedimage); -}//Example Usage
Call:
1 // image Compression Processing (zoom + mass compression to reduce high width and data size) 2 Imageresize (imgfile,imgfile,1200,0.7f); // width greater than 1200, scaled to 1200, mass compression 30%, a picture close to 200k compressed approximately 90 K
A Java image scaling and mass compression method