java图片裁剪

/**
	 * 图片裁剪
	 * @param srcImageFile				源文件路径
	 * @param x							剪裁起始位置x坐标
	 * @param y							剪裁起始位置y坐标
	 * @param width						剪裁宽度
	 * @param heigh						剪裁高度
	 * @throws IOException
	 */
	public void cut(String srcImageFile, int x, int y, int width, int heigh)
			throws IOException {
		Image img;
		ImageFilter cropFilter;
		String dir = null;
		// 读取源图像
		BufferedImage src = ImageIO.read(new File(srcImageFile));
		cropFilter = new CropImageFilter(x, y, width, heigh);
		img = Toolkit.getDefaultToolkit().createImage(
				new FilteredImageSource(src.getSource(), cropFilter));
		BufferedImage tag = new BufferedImage(width, heigh,
				BufferedImage.TYPE_INT_RGB);
		Graphics g = tag.getGraphics();
		g.drawImage(img, 0, 0, null); 
		g.dispose();
		// 输出为文件
		dir = "d:/work/test/cut_image.jpg";
		File f = new File(dir);
		ImageIO.write(tag, "PNG", f);
	}

 

你可能感兴趣的:(java)