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