`
yangwei0915
  • 浏览: 460336 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

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

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics