Java Picture High quality compression

Source: Internet
Author: User

/**

* Edit picture according to wide height

*

* @param Outpath

* Output File path

* @param width

* Output File width

* @param height

* High Output file

* @throws Exception

*/

public static void Dealimage (String filePath, String outpath, Integer width,

Integer height) throws Exception {

To read a local file:

InputStream is = new FileInputStream (FilePath);

Determine the image size 0---500k for 4 times times compression----1024k 6 times times compression 1024 more than 8 times times compression

File picture = new file (FilePath);

int cutmultiple = 2;

if (picture.exists ()) {

int picsize =integer.parseint (new DecimalFormat ("0"). Format (Picture.length ()/1024.0);//rounding

try {

int picsize = (int) (Picture.length ()/1024.0);//Non-rounding

if (picsize<=512) {

cutmultiple=4;

}else if (picsize>512 && picsize<=1024) {

cutmultiple=6;

}else if (picsize>1024) {

cutmultiple=8;

}

} catch (Exception e) {//False fault tolerant processing

cutmultiple=2;

}

}

Image image = Imageio.read (IS);

float tagsize = 200;

int old_w = Image.getwidth (null);

int old_h = Image.getheight (null);

int tempsize;

BufferedImage tag = new BufferedImage (old_w/cutmultiple, Old_h/cutmultiple, Bufferedimage.type_int_rgb);

Tag.getgraphics (). DrawImage (image, 0, 0, old_w/cutmultiple, old_h/cutmultiple, NULL);

FileOutputStream newimage = new FileOutputStream (Outpath);

JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (newimage);

Encoder.encode (tag);

Newimage.close ();

}


Java Picture High quality compression

Related Article

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.