The code is as follows
<span style= "FONT-SIZE:18PX;" >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 Picture path name Private String Srcpath; = = = Cut Picture store path name Private String subpath; = = = Cut point x coordinate private int x; private int y; = = = Shear 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 picture and save the cropped new picture. */public void cut () throws IOException {FileInputStream is = null; Imageinputstream IIS = NULL; try {//Read picture file is = new FileInputStream (srcpath); /** * Returns Iterator that contain all currently registered ImageReader, which ImageReader * claim to decode the specified format. Parameter: FormatName-contains the informal format name. * (for example, "JPEG" or "TIFF"). */iterator<imagereader> it = ImageIO. Getimagereadersbyformatname ("JPG"); ImageReader reader = It.next (); Get Picture Stream IIS = Imageio.createimageinputstream (IS); /** * IIS: Read source. True: Search forward only * to mark it as ' search forward only '. * This setting means that images contained in the input source will be read only sequentially and may allow reader * to avoid caching those input parts that contain data associated with previously read images. */Reader.setinput (IIS, True); /** * <p> * Description of how the stream is decoded by class * <p> * for specifying how to input from Java Image I/O * Flow in the context of a frame in exchange for an image or a set of images. The plug-in for a particular image format * will return an instance of * Imagereadparam from the Getdefaultreadparam method implemented by its imagereader. */Imagereadparam param = Reader.getdefaultreadparam (); /** * Picture clipping area. Rectangle specifies an area in the coordinate space that can be defined by the coordinates (x, y), width, and height of the upper-left vertex of the Rectangle object *. */Rectangle rect = new Rectangle (x, y, width, height); Provides a bufferedimage that is used as the target for decoding pixel data. Param.setsourceregion (rect); /** * Reads the specified object by index ImageIndex using the provided imagereadparam and returns * it as a full bufferedimage. */BufferedImage bi = reader.read (0, param); Save the new Image Imageio.write (bi, "JPG", "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 (); }}</span>
Note: Detailed information is in the Java Tutorial Web
Java Picture Center cropping