Java intercepts image part size

Source: Internet
Author: User

 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

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.