The idea and code _java of Java to realize picture compression

Source: Internet
Author: User

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.

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.