There are two ways to compress images built into Java:
- 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 |
Its advantage is to be able to try not to affect the image quality in the case of a certain compression of the picture, the disadvantage is that the compression effect has limitations.
- Using the Imagewriteparam Setcompressionquality method to set the image quality generated when the output picture is Imagewrite, its advantage is that the image size can be compressed without changing the image size, and the compression efficiency is relatively high.
The following code is used together to control the size and size of compressed pictures at the same time.
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)); Outputs a picture in the cache to a byte array in accordance with the specified 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