Java Intercepting pictures Sample _java

Source: Internet
Author: User

Copy Code code as follows:

/**
*
*/
Package com.b510;

Import Java.awt.Rectangle;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.util.Date;
Import Java.util.Iterator;

Import Javax.imageio.ImageIO;
Import Javax.imageio.ImageReadParam;
Import Javax.imageio.ImageReader;
Import Javax.imageio.stream.ImageInputStream;

/**
* @date 2012-11-26
* @author XHW
*
*/
public class Imagecut {

/**
* Source picture path name such as: c:\1.jpg
*/
Private String Srcpath = "E:/poool.jpg";
/**
* Cut picture to store path name. such as: c:\2.jpg
*/
Private String subpath = "E:/pool_end";
/**
* JPG picture format
*/
private static final String image_form_of_jpg = "JPG";
/**
* PNG picture format
*/
private static final String image_form_of_png = "PNG";
/**
* Cut point x coordinates
*/
private int x;

/**
* Shear Point y-coordinate
*/
private int y;

/**
* Cut Point width
*/
private int width;

/**
* Shear Point Height
*/
private int height;

Public Imagecut () {

}

public imagecut (int x, int y, int width, int height) {
this.x = x;
This.y = y;
This.width = width;
This.height = height;
}

public static void Main (string[] args) throws Exception {
Imagecut imagecut = new Imagecut (134, 0, 366, 366);
Imagecut.cut (Imagecut.getsrcpath (), Imagecut.getsubpath ());
}

/**
* Returns the iterator containing all currently registered ImageReader, which claim to be able to decode the specified format.
* Parameters: FormatName-Contains informal format names (e.g. "JPEG" or "TIFF").
*
* @param PostFix
* Suffix name of file
* @return
*/
Public iterator<imagereader> getimagereadersbyformatname (String postFix) {
Switch (PostFix) {
Case Image_form_of_jpg:
Return Imageio.getimagereadersbyformatname (image_form_of_jpg);
Case Image_form_of_png:
Return Imageio.getimagereadersbyformatname (image_form_of_png);
Default
Return Imageio.getimagereadersbyformatname (image_form_of_jpg);
}
}

/**
* Cut the picture and save the new picture by cutting it.
* @param srcpath Source picture path
* @param subpath cut picture Storage path
* @throws IOException
*/
public void Cut (String srcpath, String subpath) throws IOException {
FileInputStream is = null;
Imageinputstream IIS = NULL;
try {
Reading picture files
is = new FileInputStream (Srcpath);

           //Get file suffix name
             String PostFix = Getpostfix (Srcpath);
            System.out.println ("Picture format is:" + PostFix);
           /*
              * Returns iterator containing all currently registered ImageReader, which claim to be able to decode the specified format.
             * Parameters: FormatName-contains informal format names. (For example, "JPEG" Or "TIFF"), and so on.
             */
             iterator<imagereader> it = getimagereadersbyformatname (PostFix);

ImageReader reader = It.next ();
Get picture Stream
IIS = Imageio.createimageinputstream (IS);

/*
* <p>iis: Read source. True: Forward-only search </p> mark it as ' forward only '.
* This setting means that the images contained in the input source will be read in sequential order 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 to decode the convection class <p> used to specify how to enter from Java Image I/o
              * Frame in the context of the flow of a picture or a group of images. A plug-in for a particular image format will be implemented from its ImageReader
             * Returns an instance of Imagereadparam in the Getdefaultreadparam method.
             */
             imagereadparam param = Reader.getdefaultreadparam ();

           /*
              * Picture clipping area. Rectangle specifies an area in the coordinate space, through the Rectangle object
            The coordinates (x,y), width, and height of the top left vertex of   can define this area.
             */
             Rectangle rect = new Rectangle (x, y, width, height);

           //provides a bufferedimage that is used as a target for decoding pixel data.
            param.setsourceregion (rect);
           /*
              * Use the provided Imagereadparam to read the specified object through the index ImageIndex and take it as a complete
              * BufferedImage return.
             */
             bufferedimage bi = reader.read (0, param);

Save a new picture
Imageio.write (BI, PostFix, new File (subpath + "_" + New Date (). GetTime () + "." + PostFix));
finally {
if (is!= null)
Is.close ();
if (IIS!= null)
Iis.close ();
}

}

/**
* Get inputfilepath suffix name, such as: "E:/test.pptx" suffix named: "Pptx" <br>
*
* @param Inputfilepath
* @return
*/
public string Getpostfix (string inputfilepath) {
Return inputfilepath.substring (Inputfilepath.lastindexof (".") + 1);
}

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;
}

}

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.