Java Pictures HD compression (1)

Source: Internet
Author: User

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)

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.