The code copy is 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 {
/**
* The source image path name is: c:/1.jpg
*/
private String srcpath = "e:/poool.jpg";
/**
* Name of the clipping image storage path. For example: 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";
/**
* x coordinate of shear point
*/
private int x;
/**
* Cut point y coordinate
*/
private int y;
/**
* Shear 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 Iterator containing all currently registered ImageReaders that claim to be able to decode the specified format.
* Parameters: formatName - Contains informal format name. (such as "jpeg" or "tiff"), etc.
*
* @param postFix
* File suffix name
* @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);
}
}
/**
* Crop the image and save the new image after cropping.
* @param srcpath Source image path
* @param subpath Cut image storage path
* @throws IOException
*/
public void cut(String srcpath, String subpath) throws IOException {
FileInputStream is = null;
ImageInputStream iis = null;
try {
// Read image files
is = new FileInputStream(srcpath);
// Get the suffix name of the file
String postFix = getPostfix(srcpath);
System.out.println("Image format is:" + postFix);
/*
* Returns Iterator containing all currently registered ImageReaders that claim to be able to decode the specified format.
* Parameters: formatName - Contains informal format name. (such as "jpeg" or "tiff"), etc.
*/
Iterator<ImageReader> it = getImageReadersByFormatName(postFix);
ImageReader reader = it.next();
// Get the picture stream
iis = ImageIO.createImageInputStream(is);
/*
* <p>iis: read source.true: search forward only</p>. Mark it as 'Search forward only'.
* This setting means that images included in the input source will be read only in order, which may allow the reader to avoid cached those input portions of data that contain previously read images.
*/
reader.setInput(iis, true);
/*
* <p> Class describing how to decode streams<p>. Used to specify how to decode from Java Image I/O at input
* Streams in the context of the frame convert an image or a group of images. The plugin for a specific image format will be implemented from its ImageReader
* The getDefaultReadParam method returns an instance of ImageReadParam.
*/
ImageReadParam param = reader.getDefaultReadParam();
/*
* Image crop area. Rectangle specifies an area in the coordinate space, through the Rectangle object
The coordinates (x, y), width and height of the upper left vertex of * can define this area.
*/
Rectangle rect = new Rectangle(x, y, width, height);
// Provide a BufferedImage to use it as the target for decoding pixel data.
param.setSourceRegion(rect);
/*
* Use the provided ImageReadParam to read the object specified by indexing imageIndex and use it as a complete
* BufferedImage Return.
*/
BufferedImage bi = reader.read(0, param);
// Save 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 the suffix name of inputFilePath, such as: "e:/test.pptx" with the suffix name: "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;
}
}