简单的java图像裁减

/** * 对图片裁剪,并把裁剪完蛋新图片保存 。 * * @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); }

转载于:https://www.cnblogs.com/xingxiudong/archive/2010/02/23/3987038.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值