Compression of Java pictures

Source: Internet
Author: User

1, if in the SPRINGMVC, will bring the generated Multipartfile file, the Multipartfile converted to file

1 multipartfile file1 =2 commonsmultipartfile cf=3 diskfileitem fi = /c5>4 File f = fi.getstorelocation ();

2, convert to the image class, and get some necessary values

1 Private StaticImage img; 2 Private Static floatwidth; 3 Private Static floatheight;4 5img = Imageio.read (f);//Constructing an Image object6width = float.parsefloat (string.valueof (Img.getwidth (NULL)));//get the source map width7Height = float.parsefloat (string.valueof (Img.getheight (NULL)));//get the source graph length8 9SYSTEM.OUT.PRINTLN ("Input picture size:" +f.length ()/1024+ "KB");

3. Compress pictures

1 //determine if the file can be converted into an image class2 if(Width!=-1){3    //The current example is equal scale to 320px wide4    //Zoom ratio5Float s=width/320;6    //the width to zoom7Integer w= (int) (width/s);8    //to scale the high9Integer h= (int) (height/s);Ten     One    //Create a template ABufferedImage image =NewBufferedImage (w, H,BUFFEREDIMAGE.TYPE_INT_RGB); -    //draw a zoomed-out diagram on a template -Image.getgraphics (). DrawImage (img, 0, 0, W, H,NULL);  the}

4. Convert the resulting picture to InputStream

New"gif"new Bytearrayinputstream (Os.tobytearray ());

Compression of Java pictures

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.