This cutting graph processing class works with the front-end js to save the cut graph when uploading images.
Package COM. util. 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;/*** specifies the Java class used to cut images. ** @ author **/public class operateimage {/*** Source image path */private string srcpath;/*** cut image storage path name */private string subpath;/*** cut point X coordinate. */private int X;/*** y coordinate of the cut point. */private int y;/*** the width of the image to be cut. */private int width;/*** the height of the image to be cut. */private int height;/*** No parameter constructor. */Public operateimage () {}/ *** constructor: input the coordinates in the upper left corner of the image to be cut and the width and height of the image to be cut. ** @ Param x *. * @ Param y *. * @ Param width *. * @ Param height *. */Public operateimage (int x, int y, int wi DTH, int height) {This. X = x; this. y = y; this. width = width; this. height = height;}/*** main method of the program. ** @ Param ARGs *. * @ throws exception. */public static void main (string [] ARGs) throws exception {// specify the path of the image to be cut: string name = "C: \ srcimage \ test. BMP "; // set the coordinates in the upper left corner of the area to be cut and the width and height of the image to be cut. operateimage o = new operateimage (160, 32, 16, 16); // (X, Y, width, height) // you can specify the source image path. o. setsrcpath (name); // you can specify the path and new image of the image to be output. Title. o. setsubpath ("C: \ subimage \ 50.bmp"); // call the graph cutting method. o. cut ();}/*** crop the image and save the cropped image. */Public void cut () throws ioexception {// file input stream object is. fileinputstream is = NULL; // image file input stream object IIS. imageinputstream IIS = NULL; try {// read the image file path. is = new fileinputstream (srcpath);/** returns iterator containing all currently registered imagereader, which claims to be able to decode the specified format. * Parameter: formatname-contains informal format names (for example, "Jpeg" or "tiff. */Iterator <imagereader> it = ImageIO. getimagereadersbyformatname ("BMP"); imagereader reader = it. next (); // transfers a file to an image stream. IIS = ImageIO. createimageinputstream (is);/** <p> IIS: reads the source. true: Only search forward </P>. mark it as 'only forward search '. * This setting means that the images contained in the input source will only be read in order. Reader may be allowed to avoid caching the input parts that contain data associated with previously read images. */Reader. setinput (IIS, true);/** <p> describes the classes for stream decoding. <p>. it 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 will return the 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, "BMP", new file (subpath);} // The program finally closes the stream file stream and image stream. finally {If (is! = NULL) is. Close (); If (IIS! = NULL) IIS. close () ;}/ ***** @ return height. */Public int getheight () {return height;}/***** @ Param height. */Public void setheight (INT height) {This. height = height;}/***** @ return srcpath. */Public String getsrcpath () {return srcpath;}/***** @ Param srcpath. */Public void setsrcpath (string srcpath) {This. srcpath = srcpath;}/***** @ return subpath. */Public String getsubpath () {return subpath;}/***** @ Param subpath. */Public void setsubpath (string subpath) {This. subpath = subpath;}/***** @ return width. */Public int getwidth () {return width;}/***** @ Param width. */Public void setwidth (INT width) {This. width = width;}/***** @ return X. */Public int getx () {return X;}/***** @ Param X. */Public void setx (int x) {This. X = x;}/***** @ return. */Public int Gety () {return y;}/***** @ Param y. */Public void sety (INT y) {This. y = y ;}}