Java image cropping Tool

Source: Internet
Author: User
Package COM. yanek. util; import Java. AWT. rectangle; import Java. AWT. image. bufferedimage; import Java. io. file; import Java. io. fileinputstream; import Java. io. ioexception; import Java. util. iterator; import javax. imageIO. imageIO; import javax. imageIO. imagereadparam; import javax. imageIO. imagereader; import javax. imageIO. stream. imageinputstream; public class imgcututil {/*** @ Param ARGs */public static void main (String [] ARGs) {imgcututil. cut (30, 50,300,400, "d:/1.jpg"," d:/100.jpg ");} /*** image cropping ** @ Param X1 select the X coordinate in the upper left corner of the area * @ Param Y1 select the Y coordinate in the upper left corner of the area * @ Param width select the area width * @ Param height select the area * @ Param sourcepath source image path * @ Param descpath: Save the cropped image path */public static void cut (INT X1, int Y1, int width, int height, string sourcepath, string descpath) {fileinputstream is = NULL; imageinputstream IIS = NULL; try {is = New fileinputstream (sourcepath); string filesuffix = sourcepath. substring (sourcepath. lastindexof (". ") + 1); iterator <imagereader> it = ImageIO. getimagereadersbyformatname (filesuffix); imagereader reader = it. next (); IIS = ImageIO. createimageinputstream (is); reader. setinput (IIS, true); imagereadparam Param = reader. getdefaultreadparam (); rectangle rect = new rectangle (x1, Y1, width, height); Param. setsour Ceregion (rect); bufferedimage Bi = reader. read (0, Param); ImageIO. write (Bi, filesuffix, new file (descpath);} catch (exception ex) {ex. printstacktrace ();} finally {If (is! = NULL) {try {is. Close () ;}catch (ioexception e) {e. printstacktrace () ;}is = NULL ;}if (IIS! = NULL) {try {IIS. Close () ;}catch (ioexception e) {e. printstacktrace () ;}iis = NULL ;}}}}

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.