Packagehtml2pdf_2;ImportJava.awt.Image;ImportJava.awt.image.BufferedImage;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException; ImportJavax.imageio.ImageIO; ImportCom.sun.image.codec.jpeg.JPEGCodec;ImportCom.sun.image.codec.jpeg.JPEGEncodeParam;ImportCom.sun.image.codec.jpeg.JPEGImageEncoder; Public classImageziputil {/*** Equal proportion compressed picture file <br> Save original file, then compress, upload *@paramoldFile files to be compressed *@paramnewFile New File *@paramWidth widths//Set width (height in 0, equal scale) *@paramHeight altitude//Set height (width in 0, equal scale) *@paramQuality Quality *@returnreturns the full path of the compressed file*/ Public StaticString zipimagefile (File oldfile,file newFile,intWidthintheight,floatquality) { if(OldFile = =NULL) { return NULL; } Try { /**processing temporary files on the server*/Image srcfile=Imageio.read (OldFile); intW = srcfile.getwidth (NULL); //System.out.println (w); intH = srcfile.getheight (NULL); //System.out.println (h); DoubleBili; if(width>0) {Bili=width/(Double) W; Height= (int) (h*Bili); }Else{ if(height>0) {Bili=height/(Double) H; Width= (int) (w*Bili); } } /**wide, high setting*/bufferedimage Tag=Newbufferedimage (width, height, bufferedimage.type_int_rgb); Tag.getgraphics (). DrawImage (Srcfile,0, 0, width, height,NULL); //String Fileprex = Oldfile.getname (). substring (0, Oldfile.getname (). IndexOf ('. ')); /**file name after compression*/ //newimage = Fileprex + smallicon+ oldfile.getname (). substring (Fileprex.length ()); /**temporary storage location after compression*/FileOutputStream out=NewFileOutputStream (NewFile); JPEGImageEncoder Encoder=Jpegcodec.createjpegencoder (out); JPEGEncodeParam Jep=Jpegcodec.getdefaultjpegencodeparam (tag); /**Compression Quality*/jep.setquality (Quality,true); Encoder.encode (tag, Jep); Out.close (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } returnNewfile.getabsolutepath (); } /*** Compress image files by width height <br> Save original file, then compress and upload *@paramOldFile The full path of the file to be compressed *@paramnewFile New File *@paramWidth *@paramHeight *@paramQuality Quality *@returnreturns the full path of the compressed file*/ Public StaticString zipwidthheightimagefile (File oldfile,file newFile,intWidthintheight,floatquality) { if(OldFile = =NULL) { return NULL; } Try { /**processing temporary files on the server*/Image srcfile=Imageio.read (OldFile); intW = srcfile.getwidth (NULL); intH = srcfile.getheight (NULL); /**wide, high setting*/bufferedimage Tag=Newbufferedimage (width, height,bufferedimage.type_int_rgb); Tag.getgraphics (). DrawImage (Srcfile,0, 0, width, height,NULL); /**temporary storage location after compression*/FileOutputStream out=NewFileOutputStream (NewFile); JPEGImageEncoder Encoder=Jpegcodec.createjpegencoder (out); JPEGEncodeParam Jep=Jpegcodec.getdefaultjpegencodeparam (tag); /**Compression Quality*/jep.setquality (Quality,true); Encoder.encode (tag, Jep); Out.close (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } returnnewfile.tostring (); } /*** * @projectname project name: Html2pdf_2 * @packageclass Package and class name: Html2pdf_2.ImageZipUtil.java * @descrip tion function Description: Test *@authorAuthor: Zhouzhiwei *@paramParameters:@paramargs *@paramParameters:@throwsIOException *@returnreturn type: void * @createdate Establishment Date: June 3, 2015 PM 4:47:06*/ Public Static voidMain (String args[])throwsIOException {System.out.println (Imageziputil.zipimagefile (NewFile ("D:/qq20150602175637.png"),NewFile ("E:/c2.jpg"), 400, 128, 3f)); }}
Java compressed picture setting width high