Java Picture Crop Processing tool class code

Source: Internet
Author: User

before cutting:  

Original: Java Picture Crop processing tool class code

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

After cut:

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 picture path name such as: \ C 1.jpgprivate String srcpath;//= = = Cut Picture holds the path name. Example: c:\2.jpgprivate String subpath;//= = = Cut point x coordinate private int x;private int y;//= = Clipping point width private int width;private int height;p Ublic 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 picture and save the cropped new picture. */public void Cut () throws IOException {FileInputStream is = Null;imageinputstream IIS = null;try {//Read picture file is = new Filei Nputstream (Srcpath);/** * Returns Iterator containing all currently registered ImageReader, which claim to be able to decode the specified format. Parameter: FormatName-contains the informal format name. *(such as "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> * class describing how the stream is decoded * <p> * is used to specify how to change the flow of an image or a group of images from the context of the Java Image I/O framework at input. Like. 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 complete bufferedimage. */bufferedimage bi = reader.read (0, param);//Save new picture 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 Oper Ateimage (5, +), O.setsrcpath (name), O.setsubpath ("f:\\zuidaima\\3.jpg"); O.cut ();}}

Java Picture Crop Processing tool class code

Related Article

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.