JAVA supports image cropping and java supports image cropping.

Source: Internet
Author: User

JAVA supports image cropping and java supports image cropping.
1/** 2 * crop an image 3 * @ param src Source image 4 * @ param dest the cropped image 5 * @ param x the X coordinate of the cropping range 6 * @ param y the Y coordinate of the range is 7 * @ param w the height of the crop range is 8 * @ param h the width of the crop range is 9 * @ param destW the width of the cropped image is 10 * @ param deth the cropped Image height of 11 * @ throws IOException12 */13 public static void cutImage (String src, string dest, int x, int y, int w, 14 int h, int destW, int deth) throws IOException {15 try {16 17 // unknown problem, I don't know why \ r \ n18 19 src = src will end up in the path. Replace ("\ r \ n", ""); 20 dest = dest. replace ("\ r \ n", ""); 21 System. out. println ("CutImage: src =" + src + "; dest =" + dest + "; x =" + x + "; y =" + y ); 22 File file = new File (src); 23 if (file. exists () {24 Iterator <ImageReader> iterator = ImageIO. getImageReadersByFormatName ("jpg"); 25 ImageReader reader = (ImageReader) iterator. next (); 26 InputStream in = new FileInputStream (src); 27 ImageInputStream iis = Imag EIO. createImageInputStream (in); 28 reader. setInput (iis, true); 29 ImageReadParam param = reader. getDefaultReadParam (); 30 Rectangle rect = new Rectangle (x, y, w, h); 31 param. setSourceRegion (rect); 32 BufferedImage bi = reader. read (0, param); 33 if (w! = DestW) {// stretched or reduced by 34 bi. flush (); 35 BufferedImage newImage = new BufferedImage (destW, deth, bi. getType (); 36 Graphics g = newImage. getGraphics (); 37g. drawImage (bi, 0, 0, destW, deth, null, null); 38g. dispose (); 39 ImageIO. write (newImage, "jpg", new File (dest); 40} else {41 ImageIO. write (bi, "jpg", new File (dest); 42} 43 iis. close (); 44 in. close (); 45 // Delete the temporary file 46 file. delete (); 47} else {48 System. out. println ("the file to be cropped cannot be found: FileName =" + src); 49} 50} catch (Exception ex) {51 ex. printStackTrace (); 52} 53}CutImage

 

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.