This example for you to share the Java implementation of picture compression code, for your reference, the specific content as follows
Import Java.awt.Image;
Import Java.awt.image.BufferedImage;
Import Java.io.ByteArrayOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Javax.imageio.ImageIO;
public class Imageprocess {/** * picture * * Private image img;
/** * Width/private int width;
/** * Height/private int height;
/** * File Format * * Private String ImageFormat; /** * constructor * @throws Exception/Public imageprocess (InputStream in,string fileName) throws exception{//Fabric
Image object img = Imageio.read (in);
Get the source graph width = img.getwidth (null);
Get source Graph Length height = img.getheight (null); File format imageformat = filename.substring (Filename.lastindexof (".")
+1); /** * Compressed by width or height * @param w int max width * @param h int max height/public byte[] Resizefix (int w, int h) thro
WS IOException {if (Width/height > w/h) {return resizebywidth (W);
else {return resizebyheight (h); }/** * To the width of the base, and so proportional to shrink the picture *param w int new width */public byte[] Resizebywidth (int w) throws IOException {int h = (int) (height * w/width);
Return Resize (W, h); /** * To height as the benchmark, proportional scaling picture * @param h int new Height * * Public byte[] Resizebyheight (int h) throws IOException {int
w = (int) (width * h/height);
Return Resize (W, h); /** * Force compression/Enlarge picture to Fixed size * @param w int new width * @param h int new height/public byte[] Resize (int w, int h) throws IOException {//Scale_smooth's thumbnail algorithm produces thumbnail images with a higher precedence than the speed of the resulting image quality is better but slower bufferedimage image = New BufferedImage (w
, H,BUFFEREDIMAGE.TYPE_INT_RGB); Image.getgraphics (). DrawImage (img, 0, 0, W, h, NULL);
Draw the narrowed figure bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
Imageio.write (image, ImageFormat, BAOs);
return Baos.tobytearray (); }
}
The above is the entire content of this article, I hope to help you learn, easy to achieve picture compression operation.