/**
* ¶ÔͼƬ²Ã¼ô£¬²¢°Ñ²Ã¼ôÍêµ°ÐÂͼƬ±£´æ ¡£
*
* @param srcpath: ±»²Ã¼õµÄͼƬÍêÕû·¾¶
* @param tarpath: ÒѲüõµÄÐÂͼƬ±£´æÍêÕûµÄ·¾¶
* @param x: ²Ã¼õ¾ØÐÎÇøÓò×óÉ϶¥µãË®Æ½Î»ÒÆ
* @param y: ²Ã¼õ¾ØÐÎÇøÓò×óÉ϶¥µã´¹Ö±Î»ÒÆ
* @param width: ²Ã¼õ¾ØÐÎÇøÓòµÄ¿í¶È
* @param height: ²Ã¼õ¾ØÐÎÇøÓòµÄ¸ß¶È
*/
public static void cut(String srcpath, String tarpath, int x, int y, int width, int height) {
try {
// ¶ÁȡͼƬÎļþ
FileInputStream is = new FileInputStream(srcpath);
// »ñȡͼƬÁ÷
ImageInputStream iis = ImageIO.createImageInputStream(is);
// ת»¯³ÉÊä³öÁ÷
BufferedImage outputImage = getSubimage(ImageIO.read(iis), x, y, width, height);
// ±£´æÐÂͼƬ
if (null != outputImage) {
ImageIO.write(outputImage, "jpg", new File(tarpath));
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static BufferedImage getSubimage(BufferedImage inputImage, int x, int y, int width, int height) {
return getSubimage(inputImage, new Rectangle(x, y, width, height));
}
public static BufferedImage getSubimage(BufferedImage inputImage, Rectangle rect) {
if (rect.x < 1 || rect.y < 1 || rect.x > inputImage.getWidth() || rect.y > inputImage.getHeight()) {
return null;
}
int x = Math.min(inputImage.getWidth(), rect.x);
int y = Math.min(inputImage.getHeight(), rect.y);
int width = Math.min(inputImage.getWidth() - x, rect.width);
int height = Math.min(inputImage.getHeight() - y, rect.height);
return inputImage.getSubimage(x, y, width, height);
}