Simple Java image Cutting

Source: Internet
Author: User

/** <Br/> * crop the image and save the cropped image. <Br/> * @ Param srcpath: full path of the cropped image <br/> * @ Param tarpath: save the complete path of the cropped new image <br/> * @ Param X: the horizontal displacement of the top left vertex of the rectangular area. <br/> * @ Param Y: trim the vertical displacement of the top left vertex of the rectangle area <br/> * @ Param width: trim the width of the rectangle area <br/> * @ Param height: cut the height of the rectangular area <br/> */<br/> Public static void cut (string srcpath, string tarpath, int X, int y, int width, int height) {<br/> try {<br/> // read the image file <br/> fileinputstream is = new fileinputstream (srcpath ); </P> <p> // obtain the image stream <br/> imageinputstream IIS = ImageIO. createimageinputstream (is); </P> <p> // convert to output stream <br/> bufferedimage outputimage = getsubimage (ImageIO. read (IIS), X, Y, width, height); </P> <p> // Save the new image <br/> If (null! = Outputimage) {<br/> ImageIO. write (outputimage, "jpg", new file (tarpath); <br/>}< br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}</P> <p> Public static bufferedimage getsubimage (bufferedimage inputimage, int X, int y, int width, int height) {<br/> return getsubimage (inputimage, new rectangle (X, Y, width, height )); <br/>}</P> <p> Public static bufferedimage getsubimage (bufferedimage inputimage, rectangle rect) {<br/> If (rect. x <1 | rect. Y <1 | rect. x> inputimage. getwidth () | rect. y> inputimage. getheight () {<br/> return NULL; <br/>}< br/> int x = math. min (inputimage. getwidth (), rect. x); <br/> int y = math. min (inputimage. getheight (), rect. y); <br/> int width = math. min (inputimage. getwidth ()-X, rect. width); <br/> int Height = math. min (inputimage. getheight ()-y, rect. height); <br/> return inputimage. getsubimage (X, Y, width, height); <br/>}

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.