java 图片剪辑

不废话,直接贴代码

/**
     * @Title: ImageUtils   
     * @Description: 截取图片
     * @author TimBrian
     * @since Jan 15, 2019 4:19:16 PM   
     * @param imgPath
     * @param subPath
     * @param fileType
     * @param x Rectangle左上角的X Rectangle 
     * @param y Rectangle左上角的Y Rectangle
     * @param height Rectangle的高度
     * @param width 宽度为 Rectangle
     * @throws IOException
     */
    public static void cutImage(String imgPath, String subPath, String fileType, int x, int y, int height ,int width) throws IOException {
        
        File f = new File(imgPath);
        File t = new File(subPath);
        if (t.exists()) {
            t.delete();
        }
        //图片输入流
        ImageInputStream iis = ImageIO.createImageInputStream(f);
        //图片读取器
        Iterator<ImageReader> it = ImageIO.getImageReaders(iis);
        if (it.hasNext()) {
            ImageReader r = it.next();
            //设置输入流
            r.setInput(iis, true);
            System.out.println("格式=" + r.getFormatName());
            System.out.println("宽=" + r.getWidth(0));
            System.out.println("高=" + r.getHeight(0));
            //读取参数
            ImageReadParam param = r.getDefaultReadParam();
            //创建要截取的矩形范围
            Rectangle rect = new Rectangle(x, y, width, height);
            //设置截取范围参数
            param.setSourceRegion(rect);
            //读取截图数据
            BufferedImage bi = r.read(0, param);
            //保存图片
            ImageIO.write(bi, fileType, t);
        }
    }

当然,也可以以流的形式来操作文件

https://blog.csdn.net/wang20y8/article/details/86507306图片缩放

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值