PackageCom.cy.thumb;ImportJava.awt.Rectangle;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.util.Iterator;ImportJavax.imageio.ImageIO;ImportJavax.imageio.ImageReadParam;ImportJavax.imageio.ImageReader;ImportJavax.imageio.stream.ImageInputStream;ImportCom.sun.image.codec.jpeg.JPEGCodec;ImportCom.sun.image.codec.jpeg.JPEGImageEncoder; Public classThumb { Public Static voidMain (string[] args)throwsIOException {//reduce the picture image.png to 5 times times the length and width ratio. Reduceimage ("E:/image.png", "E:/image1.png", 5); //set the image image.png to the length and width ofReduceimage ("E:/image.png", "e:/image2.png", 100, 100); } /*** Crop the image returns a byte array *@paramis picture input stream *@paramWidth cropped width of picture *@paramheight cut height of picture *@paramimageformat Output image format "jpeg jpg etc" *@return */ Public Static byte[] Clipimage (InputStream is,intWidthintheight, String imageformat) {Bytearrayoutputstream BOS=NewBytearrayoutputstream (); Try { //Constructing an Image objectBufferedImage src =Javax.imageio.ImageIO.read (IS); //Reduce edge lengthBufferedImage tag =Newbufferedimage (width, height, bufferedimage.type_int_rgb); //draw a zoomed-out pictureTag.getgraphics (). DrawImage (src, 0, 0, width, height,NULL); Imageio.write (tag, imageformat, BOS); } Catch(IOException e) {e.printstacktrace (); } returnBos.tobytearray (); } /*** Reset Image size *@paramSrcimagepath Read Picture path *@paramToimagepath Write picture path *@paramWidth re-set the width of the picture *@paramheight re-set the height of the picture *@throwsIOException*/ Public Static voidReduceimage (String srcimagepath,string Toimagepath,intWidthintHeightthrowsioexception{FileOutputStream out=NULL; Try{ //read in fileFile File =NewFile (Srcimagepath); //Constructing an Image objectBufferedImage src =javax.imageio.ImageIO.read (file); //Reduce edge lengthBufferedImage tag =Newbufferedimage (width, height, bufferedimage.type_int_rgb); //draw a zoomed-out pictureTag.getgraphics (). DrawImage (src, 0, 0, width, height,NULL); out=NewFileOutputStream (Toimagepath); JPEGImageEncoder Encoder=Jpegcodec.createjpegencoder (out); Encoder.encode (tag); }Catch(Exception e) {e.printstacktrace (); }finally{ if(Out! =NULL) {out.close (); } } } /*** Reduce image by magnification *@paramSrcimagepath Read Picture path *@paramToimagepath Write picture path *@paramratio reduction ratio wide, high together, etc. ratio reduction *@throwsIOException*/ Public Static voidReduceimage (String srcimagepath,string Toimagepath,intRatiothrowsioexception{FileOutputStream out=NULL; Try{ //read in fileFile File =NewFile (Srcimagepath); //Constructing an Image objectBufferedImage src =javax.imageio.ImageIO.read (file); intwidth =src.getwidth (); intHeight =src.getheight (); //Reduce edge lengthBufferedImage tag =NewBufferedImage (width/ratio, Height/ratio, BUFFEREDIMAGE.TYPE_INT_RGB); //draw a zoomed-out pictureTag.getgraphics (). DrawImage (src, 0, 0, Width/ratio, Height/ratio,NULL); out=NewFileOutputStream (Toimagepath); JPEGImageEncoder Encoder=Jpegcodec.createjpegencoder (out); Encoder.encode (tag); }Catch(Exception e) {e.printstacktrace (); }finally{ if(Out! =NULL) {out.close (); } } } /*** Crop the picture and save the new picture *@paramSrcpath Read Source picture path *@paramTopath Write picture path *@paramx Cut start point x coordinate *@paramy-cut start point y-coordinate *@paramWidth cut widths *@paramHeight Shear Altitude *@paramreadimageformat reading image format *@paramWriteimageformat Write picture format*/ Public Static voidCropimage (String Srcpath, String Topath,intXintYintWidthintheight, String readimageformat,string writeimageformat) {FileInputStream FIS=NULL ; Imageinputstream IIS=NULL ; Try{ //reading a picture fileFIS =NewFileInputStream (Srcpath); Iterator<ImageReader> readers =Imageio.getimagereadersbyformatname (Readimageformat); ImageReader Reader=Readers.next (); //Get Picture StreamIIS =Imageio.createimageinputstream (FIS); Reader.setinput (IIS,true); Imagereadparam param=Reader.getdefaultreadparam (); //Define a rectangleRectangle rect =NewRectangle (x, y, width, height); //provides a bufferedimage that is used as the target for decoding pixel data. param.setsourceregion (rect); BufferedImage Bi= Reader.read (0, param); //Save new pictureImageio.write (BI, Writeimageformat,NewFile (Topath)); }Catch(Exception e) {e.printstacktrace (); }finally{ Try{ if(fis!=NULL) {fis.close (); } if(iis!=NULL) {iis.close (); } }Catch(Exception e) {e.printstacktrace (); } } } }
Above section reference article: http://blog.csdn.net/u012486840/article/details/52937823
Reproduced below from: http://blog.csdn.net/u012481520/article/details/51802469#t0
A brief talk on the bufferedimage of Java
intwidth = 100; intHeight = 100; //1. Create a BufferedImage object with no transparent colorBufferedImage Bimage =Newbufferedimage (width, height,bufferedimage.type_int_rgb);//2. Create a BufferedImage object with transparent colorBimage =Newbufferedimage (width, height, bufferedimage.type_int_argb);//3. Create a BufferedImage object that is compatible with the screen..... Iv. bufferedimage--byte[]imageio.write (BufferedImage image,string format,outputstream out); The method can solve the problem well; The parameter image represents the obtained bufferedimage The format of the parameter, such as "GIF", and so on, the parameter out represents the output stream, if it is to be converted into a byte array, the output stream is bytearrayoutputstream, and after execution, only Tobytearray () is needed to get byte[]; Five,byte[]-->Bufferedimagebytearrayinputstream in=NewBytearrayinputstream (byte[]b];//B as the input stream;BufferedImage image = Imageio.read (InputStream in);//takes in as the input stream, reads the picture into the image, and here in can be Bytearrayinputstream ();
View Code
Java to manipulate pictures, just a small demo