Packageutil;ImportJava.awt.Rectangle;ImportJava.awt.image.BufferedImage;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.InputStream;ImportJava.util.Iterator;ImportJavax.imageio.ImageIO;ImportJavax.imageio.ImageReadParam;ImportJavax.imageio.ImageReader;ImportJavax.imageio.stream.ImageInputStream; Public classImgcut { Public voidReadusingimagereader (string src, String dest,intWinth)throwsException {//get a picture readerIterator readers = imageio.getimagereadersbyformatname ("JPG"); ImageReader Reader=(ImageReader) readers.next (); //get a picture to read the inflowInputStream Source =NewFileInputStream (SRC); Imageinputstream IIS=Imageio.createimageinputstream (source); Reader.setinput (IIS,true); //Picture ParametersImagereadparam param =Reader.getdefaultreadparam (); //100,200 is the upper left position, 300 is the width of 300, that is, from 100 to take 300 wide, is the transverse 100~400, the same vertical 200~350 area to take height//Rectangle rect = new Rectangle ( +,--);// intHH = 0; if(H > 100) HH= (h-100)/3; Rectangle rect=NewRectangle (0, hh, 227, 100); Param.setsourceregion (rect); BufferedImage Bi= Reader.read (0, param); Imageio.write (BI,"JPG",NewFile (dest)); } Public Static voidMain (string[] args)throwsException {//The Main method is tested withImgcut T =Newimgcut (); T.readusingimagereader ("D://2.jpg", "d://3.jpg", 227, 163); }}
Article turned from: http://blog.sina.com.cn/s/blog_407a68fc0100nrbl.html
Java intercepts image part size