[Java] Picture compression

Source: Internet
Author: User

There are two ways to compress images built into Java:

    1. The image size is compressed by the getScaledInstance method of image, and its compression strategy is:
      Scale_default Default image Scaling algorithm
      Scale_fast Zoom Speed First
      Scale_smooth Image Smoothing Priority
      It is the advantage of the image can not affect the quality of a certain compression of the picture, the disadvantage is that the compression effect has limitations.
    2. Using Imagewriteparam's Setcompressionquality method to set the image quality generated when the image is imagewrite output, it has the advantage of compressing the size of the picture without changing the size of the picture, and the compression efficiency is higher.

The following code is used together to control the size and size of the compressed picture.

Package Test;import Java.awt.image;import Java.awt.image.bufferedimage;import java.awt.image.colormodel;import Java.io.bytearrayinputstream;import Java.io.bytearrayoutputstream;import Java.io.file;import Java.util.Iterator; Import Javax.imageio.iioimage;import Javax.imageio.imageio;import Javax.imageio.imagetypespecifier;import Javax.imageio.imagewriteparam;import Javax.imageio.imagewriter;import Javax.imageio.stream.ImageOutputStream; public class Compresspicutil {public image cmopresspic (image image) throws Exception {int Newwidth;int newheight;//specifies the generated drawing width High if (Image.getwidth (NULL) > 1024) {//Here you can specify the conditions for scaling double rate = (double) image.getwidth (null)/1024;newwidth = 1024; Newheight = (int) (Image.getheight (null)/rate);} else {newwidth = Image.getwidth (null); newheight = Image.getheight (null);} BufferedImage compression compression policy by scaling to image.scale_fast speed first bufferedimage buffer = new BufferedImage (Newwidth, Newheight, BUFFEREDIMAGE.TYPE_INT_BGR); Buffer.getgraphics (). DrawImage (Image.getscaledinstance (NewwidtH, Newheight,image.scale_fast), 0, 0, NULL);//Get a ImageWriter output stream imagewriter writer = Null;imagetypespecifier type = Imag Etypespecifier.createfromrenderedimage (buffer); Iterator iter = Imageio.getimagewriters (type, "JPG"); if ( Iter.hasnext ()) {writer = (ImageWriter) iter.next ();} if (writer = = null) {return null;} Iioimage iioimage = new Iioimage (buffer, NULL, NULL);//Specify compression mode compression degree color mode imagewriteparam param = Writer.getdefaultwritepara M ();p Aram.setcompressionmode (imagewriteparam.mode_explicit);p aram.setcompressionquality (0.4F);//You can specify the degree of compression here 0-1.0colormodel ColorModel = Colormodel.getrgbdefault ();p Aram.setdestinationtype (new  Javax.imageio.ImageTypeSpecifier (ColorModel, ColorModel Createcompatiblesamplemodel (16, 16)); Prints the picture in the cache to the byte array as specified in the configuration Bytearrayoutputstream bytearrayoutputstream = new Bytearrayoutputstream (); Mageoutputstream outputstream = Imageio.createimageoutputstream (Bytearrayoutputstream); Writer.setOutput ( OutputStream); Writer.write (null, iioimage, param);//From the wordReading a picture in a section array bytearrayinputstream bytearrayinputstream = new Bytearrayinputstream (Bytearrayoutputstream.tobytearray ()) ; Image smallimage = Imageio.read (Bytearrayinputstream); return smallimage;}}


Its compression effect is (the compression time has been planed to read the file time):

File 1----Compressed before size 3378KB compression size 44KB compression time 639 ms Picture width 1024 picture height 640
File 2----Compressed before size 2240KB compression size 60KB compression time 295 ms Picture width 1024 picture height 640
File 3----Compressed before size 1161KB compression size 37KB compression time 224 ms Picture width 1024 picture height 640
File 4----Compressed before size 1309KB compression size 50KB compression time 194 ms Picture width 1024 picture height 576
File 5----Compressed before size 872KB compression size 26KB compression time 223 ms Picture width 1024 picture height 576

[Java] Picture compression

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.