ImportJava.awt.image.BufferedImage;ImportJava.io.File;ImportJava.io.FileOutputStream;ImportJavax.imageio.ImageIO;ImportCom.sun.image.codec.jpeg.JPEGCodec;ImportCom.sun.image.codec.jpeg.JPEGEncodeParam;ImportCom.sun.image.codec.jpeg.JPEGImageEncoder; Public class narrowimage { /** * @param im * Original image * @param resizetimes * multiples, such as 0.5 is reduced by half, 0 .98 and so on. Double type * @return Returns the processed image */ PublicBufferedImageZoomimage(String src) {BufferedImage result =NULL;Try{File Srcfile =NewFile (SRC);if(!srcfile.exists ()) {System.out.println ("file does not exist"); } BufferedImage im = Imageio.read (srcfile);/* Width and height of the original image */ intwidth = Im.getwidth ();intHeight = im.getheight ();//Compression calculation floatResizetimes =0.3F/ * This parameter is to be converted to a multiple, if 1 is converted to 1 time times * / /* Width and height of the adjusted picture */ intTowidth = (int) (width * resizetimes);intToheight = (int) (height * resizetimes);/ * New Build result picture * /result =NewBufferedImage (Towidth, Toheight, Bufferedimage.type_int_rgb); Result.getgraphics (). DrawImage (Im.getscaledinstance (Towidth, Toheight, Java . AWT. Image.scale_smooth),0,0,NULL); }Catch(Exception e) {System.out.println ("An exception occurred while creating a thumbnail"+ e.getmessage ()); }returnResult } Public Boolean writehighquality(BufferedImage im, String filefullpath) {Try{/ * Output to file stream * /FileOutputStream NewImage =NewFileOutputStream (Filefullpath); JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (newimage); JPEGEncodeParam Jep = Jpegcodec.getdefaultjpegencodeparam (IM);/ * Compression quality * /Jep.setquality (0.9Ftrue); Encoder.encode (IM, Jep);/ * near JPEG encoding * /Newimage.close ();return true; }Catch(Exception e) {return false; } } Public Static void Main(string[] args) {String Inputfoler ="E:\\11.jpg";/ * Fill in the folder where you want to zoom out the full address of the image * /String OutputFolder ="E:\\12.jpg";/ * Fill in the folder where you converted your pictures * *Narrowimage Narrowimage =NewNarrowimage (); Narrowimage.writehighquality (Narrowimage.zoomimage (Inputfoler), OutputFolder); }}
Java Pictures HD compression (1)