Java image cropping tool code and java image cropping tool class

Source: Internet
Author: User

Java image cropping tool code and java image cropping tool class

Before cutting:

Original article: java image cropping tool code

Source code: http://www.zuidaima.com/share/1550463351786496.htm

After cutting:

Package com. zuidaima. zhangjun. image; 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;/*** @ author www.zuidaima.com ***/public class OperateImage {// === Source image path name, for example, c: \ 1.jpg private String srcpath; // === cut the image storage path name. For example: c: \ 2.jpg private String subpath; // === cut point x coordinate private int x; private int y; // === cut point width private int width; private int height; public OperateImage () {} public OperateImage (int x, int y, int width, int height) {this. x = x; this. y = y; this. width = width; this. height = height;}/*** crop the image and save the cropped new image. */Public void cut () throws IOException {FileInputStream is = null; ImageInputStream iis = null; try {// read the image file is = new FileInputStream (srcpath ); /*** return all iterators that have registered ImageReader. These ImageReader * claim to be able to decode the specified format. Parameter: formatName-including name in informal format * (for example, "jpeg" or "tiff. */Iterator <ImageReader> it = ImageIO. getImageReadersByFormatName ("jpg"); ImageReader reader = it. next (); // obtain the image stream iis = ImageIO. createImageInputStream (is);/*** iis: reads the source. True: Only search forward * mark it as 'only search forward '. * This setting means that the images contained in the input source will only be read in order, and may allow reader * to avoid caching those input parts that contain data associated with previously read images. */Reader. setInput (iis, true ); /*** <p> * class that describes how to decode streaming * <p> * specifies how to convert stream 1 from the context of the Java Image I/O * framework during input. an image or a group of images. The plug-in for specific image formats * returns an * ImageReadParam instance from the getDefaultReadParam method implemented by its ImageReader. */ImageReadParam param = reader. getDefaultReadParam ();/*** crop the image area. Rectangle specifies an area in the coordinate space, which can be defined by the coordinates (x, y), width, and height of the top left vertex of the Rectangle object. */Rectangle rect = new Rectangle (x, y, width, height); // provides a BufferedImage to decode pixel data. Param. setSourceRegion (rect);/*** use the provided ImageReadParam to read the object specified by the imageIndex index and return * it as a complete BufferedImage. */BufferedImage bi = reader. read (0, param); // Save the new image ImageIO. write (bi, "jpg", new File (subpath);} finally {if (is! = Null) is. close (); if (iis! = Null) iis. close () ;}} public int getHeight () {return height;} public void setHeight (int height) {this. height = height;} public String getSrcpath () {return srcpath;} public void setSrcpath (String srcpath) {this. srcpath = srcpath;} public String getSubpath () {return subpath;} public void setSubpath (String subpath) {this. subpath = subpath;} public int getWidth () {return width;} public void setWidth (int width) {this. width = width;} public int getX () {return x;} public void setX (int x) {this. x = x;} public int getY () {return y;} public void setY (int y) {this. y = y;} public static void main (String [] args) throws Exception {String name = "D: \ zuidaima \ image.jpg"; OperateImage o = new OperateImage (60, 5, 80,100); o. setSrcpath (name); o. setSubpath ("F :\\ zuidaima \ 3.jpg"); o. cut ();}}

 


How can I use java code to crop a blank area with a signature image?

Use imageIO to read the BufferedImage, check the non-blank size of the image, create a new BufferedImage, copy the non-blank area to the new BufferedImage, and save it again with imageIO.
 
Java cropping GIF images

This does not seem to work !! Gif is a combination of several images!

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.