Java image center Cropping
The Code is as follows:
Package com. model; 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 Images {// === source image path name private String srcpath; // === cut the image storage path Name private String subpath; // ==== cut point x coordinate private int x; private int y; // === cut point width private int width; private int height; public Images () {} public Images (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
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 );/***
* Class that describes how to perform stream decoding *
* This parameter is used to specify how to convert an Image or a group of images from a stream in the context of the Java Image I/O * framework during input. 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 = "E: \ java.jpg"; Images o = new Images (180,180,180,180); o. setSrcpath (name); o. setSubpath ("F: \ ios.jpg"); o. cut ();}}
Note: For more information, see java tutorial network.