Thumbnails 处理图片

博客引用处(以下内容在原有博客基础上进行补充或更改,谢谢这些大牛的博客指导):
Thumbnails 处理图片
Java图片处理开源框架
java使用google开源工具实现图片压缩
Thumbnails

        //原图 1056  2272 &Am neil tu


        //原图宽高都小于3000 不设置keepAspectRatio(false) 那么只有高会改为指定高,宽等比例改变,反之设置keepAspectRatio(false)那么宽高都会按照指定大小改变
        Thumbnails.of("E:\\img3.jpg").size(3000,3000).keepAspectRatio(false).toFile("E:\\3.jpg");
        Thumbnails.of("E:\\img3.jpg").size(3000,3000).toFile("E:\\3.jpg");

        //原图宽都小于3000 ,高大于2000 不设置keepAspectRatio(false)那么高会减小到指定大小,宽会等比例改变,反之设置keepAspectRatio(false)那么宽高都会按照指定大小改变
        Thumbnails.of("E:\\img3.jpg").size(3000,2000).toFile("E:\\3.jpg");
        Thumbnails.of("E:\\img3.jpg").size(3000,2000).keepAspectRatio(false).toFile("E:\\3.jpg");

        //原图宽都大于500,高小于3000 不设置keepAspectRatio(false)那么宽会减小到指定大小,高会等比例改变,反之设置keepAspectRatio(false)那么宽高都会按照指定大小改变
        Thumbnails.of("E:\\img3.jpg").size(500,3000).toFile("E:\\3.jpg");
        Thumbnails.of("E:\\img3.jpg").size(500,3000).keepAspectRatio(false).toFile("E:\\3.jpg");

        //原图 宽高都大于500 不设置keepAspectRatio(false)那么只有高会改为指定高,宽等比例改变,反之设置keepAspectRatio(false)那么宽高都会按照指定大小改变
        Thumbnails.of("E:\\img3.jpg").size(500,500).toFile("E:\\3.jpg");

        //只指定高 高度变为指定高,宽等比例改变
        Thumbnails.of("E:\\img3.jpg").height(500).toFile("E:\\3.jpg");

        //只指定宽,宽变为指定宽,高等比例改变
        Thumbnails.of("E:\\img3.jpg").width(500).toFile("E\\3.jpg");

        //按照比例因子进行缩放,不指定高宽的具体大小 scale 比例 scale取值越大,比例越高,1f代表原比例,0.5f代表原比例的一半,以此类推
        Thumbnails.of("E:\\img3.jpg").scale(0.5f).toFile("E:\\3.jpg");

        //图片旋转 rotate 旋转 正数:顺时针, 负数:逆时针  必须有比例大小的设置,不能单独用rotate
        Thumbnails.of("E:\\img3.jpg").scale(1f).rotate(90).toFile("E:\\3.jpg");
        Thumbnails.of("E:\\img3.jpg").size(1000,2000).rotate(90).toFile("E:\\3.jpg");

        //水印 watermark(位置(指取水印图放置的位置),水印图,透明度)
        Thumbnails.of("E:\\img3.jpg").scale(1f).watermark(Positions.BOTTOM_LEFT,ImageIO.read(new File("E:\\img6.jpg")),0.5f).toFile("E:\\3.jpg");

        //转化图像格式 如jpg 转 png
        Thumbnails.of("E:\\img3.jpg").scale(1f).outputFormat("png").toFile("E:\\3.png");

        //裁剪 图片中心400*400的区域
        Thumbnails.of("E:\\img3.jpg").sourceRegion(Positions.CENTER,400,400).size(200,200) .keepAspectRatio(false).toFile("E:\\3.png");

       //裁剪 图片右下400*400的区域
        Thumbnails.of("E:\\img3.jpg").sourceRegion(Positions.BOTTOM_RIGHT,400,400).size(200,200) .keepAspectRatio(false).toFile("E:\\3.png");

        //裁剪 指定坐标
        Thumbnails.of("E:\\img3.jpg") .sourceRegion(600,500,400,400).size(200,200) .keepAspectRatio(false).toFile("E:\\3.png");

        //转成ByteArrayOutputStream 或者二进制数组
        MultipartFile file = new MultipartFile() {
            @Override
            public String getName() {
                return null;
            }

            @Override
            public String getOriginalFilename() {
                return null;
            }

            @Override
            public String getContentType() {
                return null;
            }

            @Override
            public boolean isEmpty() {
                return false;
            }

            @Override
            public long getSize() {
                return 0;
            }

            @Override
            public byte[] getBytes() throws IOException {
                return new byte[0];
            }

            @Override
            public InputStream getInputStream() throws IOException {
                return null;
            }

            @Override
            public void transferTo(File dest) throws IOException, IllegalStateException {

            }
        };
        byte [] buf = null;
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        Thumbnails.of(file.getInputStream()).scale(0.1f).toOutputStream(outputStream);
        //或者
        File file2 = new File("E:\\img3.jpg");
        FileInputStream fileInputStream = new FileInputStream(file2);
        Thumbnails.of(fileInputStream).scale(0.1f).toOutputStream(outputStream);
        //或者
        ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
        Thumbnails.of(file.getInputStream()).height(720).outputQuality(1f).toOutputStream(byteOutputStream);
        ByteArrayInputStream inputStream = new ByteArrayInputStream(byteOutputStream.toByteArray());
        byteOutputStream.flush();
        InputStream inputStreamParent = inputStream;

        buf = outputStream.toByteArray();

        //图片的压缩质量 outputQuality是图片的质量,值是在0到1,越接近于1质量越好,越接近于0质量越差。
        Thumbnails.of("原图文件的路径").scale(1f) .outputQuality(0.5f).toFile("压缩后文件的路径");

注意:scale、width|height、size三者不能同时共存,但必须要有一个

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值