侧边栏壁纸
博主头像
suringYu

走走停停

  • 累计撰写 35 篇文章
  • 累计创建 11 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

java中base64图片的压缩

suringYu
2022-04-14 / 0 评论 / 0 点赞 / 414 阅读 / 19,566 字

核心思想:

使用Thumbnailator实现图片的压缩功能,Thumbnailator也提供给图片缩放、旋转与加水印等功能。项目中前端上传数据是base64格式传入,也就使用了base64压缩

使用方式

引入依赖


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

调用工具类

import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;

public class Base64CompressUtils {

    /**
     * 在线图片转换成base64字符串
     *
     * @param imgURL 图片线上路径
     * @return
     */
    public static String ImageToBase64ByOnline(String imgURL) {
        ByteArrayOutputStream data = new ByteArrayOutputStream();
        try {
            // 创建URL
            URL url = new URL(imgURL);
            byte[] by = new byte[1024];
            // 创建链接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);
            InputStream is = conn.getInputStream();

            // 将内容读取内存中
            int len = -1;
            while ((len = is.read(by)) != -1) {
                data.write(by, 0, len);
            }
            // 关闭流
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        String str=encoder.encode(data.toByteArray());
        //压缩Base64编码并输出
        return resizeImageTo50K(str);
    }

    /**
     * 压缩base64编码至50K以内
     * @param base64Img
     * @return
     */
    public static String resizeImageTo50K(String base64Img) {
        return resizeImage(base64Img, 50);
    }

    /**
     * 压缩base64编码至目标大小以内,单位/kb
     * @param base64Img 目标base64
     * @param targetSize 目标大小,单位kb
     * @return
     */
    public static String resizeImage(String base64Img, int targetSize) {
        try{
            return compressPicCycle(base64Img, targetSize, 0.8);
        } catch (Exception e) {
            e.printStackTrace();
            return base64Img;
        }
    }

    /**
     *
     * @param base64 目标base64
     * @param targetSize 目标大小,单位kb
     * @param accuracy 精度,递归压缩的比率,建议小于0.9
     * @return
     * @throws IOException
     */
    private static String compressPicCycle(String base64, long targetSize, double accuracy) throws IOException {
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] bytes1 = decoder.decodeBuffer(base64);
        InputStream stream = new ByteArrayInputStream(bytes1);
        BufferedImage bim = ImageIO.read(stream);
        //判断大小,如果小于50kb,不用压缩,如果大于等于50kb,需要压缩
        if (bytes1.length <= targetSize * 1024) {
            return base64;
        }
        //计算宽高
        int srcWidth = bim.getWidth();
        int srcHeight = bim.getHeight();
        int destWidth = new BigDecimal(srcWidth).multiply(new BigDecimal(accuracy)).intValue();
        int destHeight = new BigDecimal(srcHeight).multiply(new BigDecimal(accuracy)).intValue();
        BufferedImage output = Thumbnails.of(bim)
                .size(destWidth,destHeight)
                .outputQuality(accuracy).asBufferedImage();
        String base64Out = imageToBase64(output);
        return compressPicCycle(base64Out,targetSize,accuracy);
    }

    /**
     * BufferedImage转换成base64,默认是png类型的图片
     * @param bufferedImage 目标图片
     * @return
     */
    public static String imageToBase64(BufferedImage bufferedImage) {
        Base64 encoder = new Base64();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ImageIO.write(bufferedImage, "png", baos);
        } catch (IOException e) {
        }
        return new String(encoder.encode((baos.toByteArray())));
    }

    /**
     * BufferedImage转换成base64
     * @param bufferedImage 目标图片
     * @param imageType 图片类型
     * @return
     */
    public static String imageToBase64(BufferedImage bufferedImage, String imageType) {
        Base64 encoder = new Base64();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ImageIO.write(bufferedImage, imageType, baos);
        } catch (IOException e) {
        }
        return new String(encoder.encode((baos.toByteArray())));
    }

    /**
     * 本地图片转换成base64字符串
     *
     * @param imgFile 图片本地路径
     * @return
     */
    public static String ImageToBase64ByLocal(String imgFile) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理


        InputStream in = null;
        byte[] data = null;

        // 读取图片字节数组
        try {
            in = new FileInputStream(imgFile);

            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();

        return encoder.encode(data);// 返回Base64编码过的字节数组字符串
    }


    /**
     * base64字符串转换成图片
     *
     * @param imgStr      base64字符串
     * @param imgFilePath 图片存放路径
     * @return
     */
    public static boolean Base64ToImage(String imgStr, String imgFilePath) { // 对字节数组字符串进行Base64解码并生成图片

        if (StringUtils.isEmpty(imgStr)) // 图像数据为空
            return false;

        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64解码
            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}

调用示例


    public static void main(String[] args) {
        String bareheadedBase64 = "iVBORw0KGgoAAAANSUhEUgAAAW8AAADoCAIAAACvnPKXAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAEXRFWHRTb2Z0d2FyZQBTbmlwYXN0ZV0Xzt0AACAASURBVHic7d15eBRVvjfwU72ms3b2lex7SAIkYQcT9l0WwQUcRFHGeUevjnMRxxkv3juvr+Oo131BR8AFEB0UUNaIiIEkEMhCgOwJ2Tt70knv1fX+UdCG7lPdne7qhfTv88wzT6eq+tQZx3xzzqlT5xAURSEAALAax9EVAACME5AmAAB2QJoAANgBaQIA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MUNyeZP3MxP3Wh46o5F4awweqRGVyZ9kP6RFzFb01pA/7fe9eOSa6XJ+CB+6cNBXCPrsZy0T9cvtH99HEOrUZ7fqSo1MR9fkLFVmIufnAJY51o9nXGgtmcAGyXeQsGf5rrSih4cnnDO3z02FTO9SYgQIjyCzB8VBtaDNLnLHKqswx5/ds5kkwO04w/HL0G04gvRygOcAMyQlnDq84SHrVaxBIagp3M3+ffVuvu++MHw+JKk6OOPrbZ/fZyKquxjVdErunEWbtQ893u/dWyVXA20Te4apW1dT36HWaPMV+S2f+NS+9fH2QgmbfP4XYkgcxv9oxBe2LE7aJvcBWQq9WtnS/75y2Xs/uc/PbGOra38xgey45LmxlfCeW85uiIuB9LEuew8VRji7SF2E4pFQqlSfa2zp7FferWjuxS3VzlC6PUVc5+ba+7MVwBsyrXe+rsrPHnojJlX7t6wyPgq+QDYE6TJ3ergpuXrM8ayuhoANgajsHefCB8viBLghKBtcjdJCfLbkpP2SFZqoKfpxZkAsDMYhXU6PSPyzy5d+/5afcuAtHVwONbPJ9bfJ9bPZ0p40CPZqSZX2AfAUSBNAADsgHETAAA7IE0AAOyANAEAsAPSBADADkgTAAA7IE0AAOyANAEAsAPSBADADkgTAAA7IE0AAOyANAEAsAPSBADADkgTAAA7IE0AAOyANAEAsAPSBADADkgTAAA7IE0AAOyANAEAsAPSBADADkgTAAA7IE0AAOyANAEAsAPSBADADkgTAAA7IE0AAOyANAEAsAPSBADADkgTAAA7IE0AAOyANAEAsAPSBADADkgTAAA7IE0AAOyANAEAsAPSBADADkgTAAA7IE0AAOyANAEAsAPSBADADkgTAAA7IE0AAOyANAEAsOP/A3gRkwmzi+a8AAAAAElFTkSuQmCC";
        bareheadedBase64 = Base64CompressUtils.resizeImage(bareheadedBase64, 20);
        System.out.println(bareheadedBase64);
    }

注意

  • 本文只是提供一个简单压缩图片的工具类
  • 使用过程中需要注意图片类型,示例中提供的图片类型为PNG,其他类型的需要在imageToBase64这个方法上做入参调整
0

评论区