base64图片字符串获取指定压缩图片后的文件流

/**
 * @Author mxf
 * @Description 获取指定压缩图片后的文件流
 * @Date 2021/7/29 14:39
 * @Param [baseImg64, scale, outputQuality] base64字节,scale:指定图片的大小,值在0到1之间,1f就是原图大小,0.5就是原图的一半大小,这里的大小是指图片的长宽。
 *                                           outputQuality:图片的质量,值也是在0到1,越接近于1质量越好,越接近于0质量越差。
 * @return java.io.InputStream
 **/
public static InputStream getScaleInputStreamImg(String baseImg64,double scale,double outputQuality) throws java.io.IOException
{
    InputStream  fileInputStream = baseToInputStream(baseImg64);
    String path =  AttachServerConfig.AttachServerTempPath + CommonUtil.getUUID() + ".jpg";
    File file = new File(path);
    Thumbnails.of(fileInputStream).scale(scale).outputQuality(outputQuality).toFile(file);
    InputStream inputStream =  new FileInputStream(file);
    file.delete();
    return inputStream;
}

/**
 * base64转inputStream
 * @param base64string base64
 * @return InputStream
 */
public static InputStream baseToInputStream(String base64string)
{
    ByteArrayInputStream stream = null;
    try {
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] bytes1 = decoder.decodeBuffer(base64string);
        stream = new ByteArrayInputStream(bytes1);
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
    return stream;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值