Compress picture size (Java source code)

Source: Internet
Author: User

/** *  * Directly specify the compressed width: * @param oldFile * The file to be compressed * @param width * Compressed After the height of the * @param height * Compressed after * @return return the full path of the compressed file Path */public static file Zipimagefile (file oldFile, int width, int height) {if (oldFile = = null) {return null;} File NewImage = null;try {/** handles temporary files on the server */image srcfile = Imageio.read (oldFile);/** width, high set */bufferedimage tag = new B Ufferedimage (width, height,bufferedimage.type_int_rgb); Tag.getgraphics (). DrawImage (srcfile, 0, 0, width, height, NULL),/** compressed file name can then be customized */newimage = oldfile;/** compressed after the temporary storage location */fileoutputstream out = new FileOutputStream (newimage); JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out); JPEGEncodeParam Jep = Jpegcodec.getdefaultjpegencodeparam (tag),/** compression quality */jep.setquality (n, True); Encoder.encode ( tag, Jep); Out.close ();} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} return newimage;}

  

Compress picture Size (Java source code)

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.