Let’s take a look at the effect first:
Test one:
Original picture:
Rendering:
Test 2:
Original picture:
Rendering:
Code part:
Copy the 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 image path name such as: c:/1.jpg
*/
private String srcpath = "e:/poool.jpg";
/**
* Cut image storage path name. 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;
/**
* Y coordinate of cutting point
*/
private int y;
/**
* Cut point width
*/
private int width;
/**
* Height of cutting point
*/
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 an Iterator containing all currently registered ImageReaders that claim to be able to decode the specified format.
* Parameter: formatName - Contains the informal format name. (such as "jpeg" or "tiff"), etc.
*
* @param postFix
* File extension
* @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 picture and save the cropped new picture.
* @param srcpath source image path
* @param subpath Cut image storage path
* @throwsIOException
*/
public void cut(String srcpath, String subpath) throws IOException {
FileInputStream is = null;
ImageInputStream iis = null;
try {
//Read image file
is = new FileInputStream(srcpath);
// Get the suffix name of the file
String postFix = getPostfix(srcpath);
System.out.println("The picture format is: " + postFix);
/*
* Returns an Iterator containing all currently registered ImageReaders that claim to be able to decode the specified format.
* Parameter: formatName - Contains the informal format name. (such as "jpeg" or "tiff"), etc.
*/
Iterator<ImageReader> it = getImageReadersByFormatName(postFix);
ImageReader reader = it.next();
// Get image 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 contained in the input source will only be read sequentially, possibly allowing the reader to avoid caching those parts of the input that contain data associated with images that have been read previously.
*/
reader.setInput(iis, true);
/*
* <p>Class that describes how to decode the stream<p>. Used to specify how to decode the stream from Java Image I/O on input
* Stream transforms an image or set of images in the context of a frame. Plugins for specific image formats will be implemented from their ImageReader
* The getDefaultReadParam method returns an instance of ImageReadParam.
*/
ImageReadParam param = reader.getDefaultReadParam();
/*
* Image cropping area. Rectangle specifies an area in coordinate space, through the Rectangle object
* The coordinates (x, y), width and height of the upper left vertex can define this area.
*/
Rectangle rect = new Rectangle(x, y, width, height);
// Provide a BufferedImage to use as the target for decoding pixel data.
param.setSourceRegion(rect);
/*
* Use the provided ImageReadParam to read the object specified by index imageIndex and treat it as a complete
* BufferedImage returns.
*/
BufferedImage bi = reader.read(0, param);
//Save new image
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, for example: the suffix name of "e:/test.pptx" is: "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;
}
}