Java Changes image file size

Source: Internet
Author: User
Tags string format

 PackageTest.common;ImportJava.awt.Graphics;ImportJava.awt.Image;ImportJava.awt.image.BufferedImage;ImportJava.io.ByteArrayOutputStream;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportJavax.imageio.ImageIO; Public classImgtest {PrivateImage img; Private Final Static intWIDTH = 147; Private Final Static intHEIGHT = 136; /*** Change the size of the picture to the width to size, then the height varies proportionally with the width@paramis the input stream of the picture being uploaded *@paramafter the OS changes the size of the image, the stream of the picture is output to the target OutputStream *@paramsize new Picture's width *@paramformat of new picture *@throwsIOException*/     Public StaticOutputStream Resizeimage (InputStream is, OutputStream OS,intSize, String format)throwsIOException {bufferedimage previmage=Imageio.read (IS); Doublewidth =previmage.getwidth (); DoubleHeight =previmage.getheight (); DoublePercent = size/width; intNewwidth = (int) (Width *percent); intNewheight = (int) (Height *percent); BufferedImage Image=Newbufferedimage (Newwidth, Newheight, BUFFEREDIMAGE.TYPE_INT_BGR); Graphics Graphics=Image.creategraphics (); Graphics.DrawImage (Previmage,0, 0, newwidth, Newheight,NULL);        Imageio.write (image, format, OS);        Os.flush ();        Is.close ();        Os.close (); Bytearrayoutputstream b=(bytearrayoutputstream) OS; returnOS; }         Public Static voidMain (string[] args) {Try{InputStream is=NewFileInputStream (NewFile ("Download/qrcode_test.jpg")); OutputStream OS=NewFileOutputStream (NewFile ("Download/qrcode_test_1.jpg")); Resizeimage (IS, OS,Ten, "PNG"); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }}

Java Changes image file size

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.