Java 使用Thumbnails 等比例 base64压缩、旋转等处理图片

 1、添加依赖

<dependency>
    <groupId>net.coobird</groupId>
    <artifactId>thumbnailator</artifactId>
    <version>0.4.11</version>
</dependency>

 主要代码:

size(width,height) 图片长宽
rotate(角度),正数:顺时针 负数:逆时针
scale(比例)
keepAspectRatio(false) 默认是按照比例缩放的
watermark(位置,水印图,透明度)
outputFormat(图像格式)

Thumbnails.of(in).size(600,400).rotate(90).toOutputStream(out);
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;

@Slf4j
public class ImageUtils {
    private static final Integer KB = 1024;
    /**
     * 图片压缩指定长宽处理
     * @param origPicContent
     * @return
     */
    public static String compressPic(String origPicContent) {
        ByteArrayInputStream in = null;
        ByteArrayOutputStream out = null;
        try {
            byte[] bytes = new BASE64Decoder().decodeBuffer(origPicContent);
            in = new ByteArrayInputStream(bytes);
            out = new ByteArrayOutputStream();
            Thumbnails.of(in).size(600,400).toOutputStream(out);
            return new BASE64Encoder().encode(bytes).replaceAll("\\r\\n","");
        } catch (IOException e) {
            log.error ("decode buffer fail, message:{}", e.getMessage (), e);
        } finally {
            if (in != null) {
                try {
                    in.close ();
                } catch (IOException e) {
                    log.error ("ByteArrayInputStream close fail, message:{}", e.getMessage (), e);
                }
            }
            if (out != null) {
                try {
                    out.close ();
                } catch (IOException e) {
                    log.error ("ByteArrayOutputStream close fail, message:{}", e.getMessage (), e);
                }
            }
        }
        return origPicContent;
    }

    /**
     * 图片压缩到指定大小
     * @param origPicContent
     * @param desFileSize
     * @return
     */
    public static String compressPic(String origPicContent, Integer desFileSize) {
        ByteArrayInputStream in = null;
        ByteArrayOutputStream out = null;
        try {
            byte[] bytes = new BASE64Decoder().decodeBuffer(origPicContent);
            while (bytes.length > desFileSize * KB) {
                in = new ByteArrayInputStream(bytes);
                out = new ByteArrayOutputStream();
                Thumbnails.of(in)
                        .scale(0.8f)
//                        .outputQuality(accuracy)
                        .toOutputStream(out);
                bytes = out.toByteArray();
            }
            return new BASE64Encoder().encode(bytes).replaceAll("\\r\\n","");
        } catch (IOException e) {
            log.error ("decode buffer fail, message:{}", e.getMessage (), e);
        } finally {
            if (in != null) {
                try {
                    in.close ();
                } catch (IOException e) {
                    log.error ("ByteArrayInputStream close fail, message:{}", e.getMessage (), e);
                }
            }
            if (out != null) {
                try {
                    out.close ();
                } catch (IOException e) {
                    log.error ("ByteArrayOutputStream close fail, message:{}", e.getMessage (), e);
                }
            }
        }
        return origPicContent;
    }

    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer();
        sb.append(testText());
        String img = sb.toString().split(",")[1];
        System.out.println("length:"+img.length());
        String image = ImageUtils.compressPic(img, 200);
        System.out.println("length:"+image.length());
        System.out.println(image);
    }

    private static String testText(){
        StringBuilder result = new StringBuilder();
        try {
            File file = new File("D:\\imgFile.txt");
            //构造一个BufferedReader类来读取文件
            BufferedReader br = new BufferedReader(new FileReader(file));
            String s = null;
            //使用readLine方法,一次读一行
            while((s = br.readLine())!=null){
                result.append(s);
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result.toString();
    }
}

参考文章:

https://blog.csdn.net/qiaqia609/article/details/53171149

https://blog.csdn.net/paul200345/article/details/94402283

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值