java中的图片裁剪

除了根据网络上的一些代码实现了图片的裁剪功能,初次之外也在作者的基础上进一步更加简洁地实现图片的裁剪的功能。

/**
 * 裁剪图片
 * @param srcImageString
 * @param detImageString
 * @param x
 * @param y
 * @param w
 * @param h
 * @throws Exception
 */
public void imageCut(String srcImageString, String detImageString, int x, int y, int w, int h) throws Exception{
	File imagePutFile = new File(srcImageString);
	if (!imagePutFile.exists()) {
		throw new Exception("imagefile is not exited!");
	}
	//图片流读入缓存
	ImageInputStream llStream = ImageIO.createImageInputStream(new FileInputStream(srcImageString));
	Iterator<ImageReader> iterator =ImageIO.getImageReaders(llStream);
	ImageReader iamgeradeReader = (ImageReader) iterator.next();
	
	iamgeradeReader.setInput(llStream);
	ImageReadParam param = iamgeradeReader.getDefaultReadParam();
	param.setSourceRegion(new Rectangle(x, y, w, h));
	BufferedImage bImage = iamgeradeReader.read(0, param);
	ImageIO.write(bImage, "png", new File(detImageString));
}

上述代码地址为:https://blog.csdn.net/qq_40162735/article/details/88029484
以下为自己的实现代码:

public void testImage(String imageString, String detImageString) throws Exception{
	File file = new File(imageString);
	if (!file.exists()) {
		throw new Exception("file is not exited!");
	}
	BufferedImage image = ImageIO.read(new FileInputStream(file));
	BufferedImage bImage = image.getSubimage(0, 0, 200, 200);
	ImageIO.write(bImage, "png", new File(detImageString));
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

绛洞花主敏明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值