Java compressed picture setting width high

Source: Internet
Author: User

 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.