当前位置: 首页 > 工具软件 > Java Base64 > 使用案例 >

java压缩base64位图

仲皓君
2023-12-01

前端调用接口的时候获取base64过大,响应变慢,这时候需要对base64进行压缩,提高渲染速度

<!-- 图片压缩 -->
        <dependency>
            <groupId>net.coobird</groupId>
            <artifactId>thumbnailator</artifactId>
            <version>0.4.8</version>
        </dependency>
package com.ekkcole.controlle.压缩图片;

import com.ekkcole.utils.UrlToBase64Util;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Decoder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.apache.commons.codec.binary.Base64;

import java.io.*;

/*
   图片压缩
 */

public class Imageys {

    public static void main(String[] args) {
        String s = "iVBORw0KGgoAAAANSUhEUgAAAJYAAACWCAYAAAA8AXHiAAAAAXNSR0IArs4c6QAAFEdJREFUeF7tnXncTtUWx/drVkKZivJSxihU6uU1NEqiDEVRGkQiIVN1u93PrW5RmRqolGaapElzeSs0XGPJTEUa8ArJFPb9fLeOe97HOc85z3me8zz7sH0+/vGcs/faa/322mv91tpHljB/jAZC0EBWCGOaIY0GhAGWAUEoGjDACkWtZlADLIOBUDRggBWKWs2gBlgGA6FowAArFLWaQQ2wDAZC0YABVihqNYMaYBkMhKIBA6xQ1GoGNcAyGAhFAwZYoajVDGqAZTAQigYMsEJRqxnUAMtgIBQNGGCFolYzqAGWwUAoGjDACkWtZlADLIOBUDRggBWKWs2gBlgGA6FowAArFLWaQQ2wDAZC0YABVihqNYMaYBkMhKIBA6xQ1GoGNcAyGAhFAwZYoajVDGqAZTAQigYMsEJRqxnUAMtgIBQNGGCFolYzqAHWQYaBf94yRLKku4bfn1HbZnTyg8ymGV/O0IE3yYULF4rdu3eLypUri6dfeDFj9s3YxBm3wkEowMAbb5C//fabKFmypPj+++9FtWrVxMTnJmXExhmZ9CC0qRZLGnRTX1mqVCnx73tGZF3d7TL5008/icaNG4t7HxiddjunfUItLH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IGVhgL4AikbcPehtKBL8tUqpTUR2ETlZVjkpKS203oRMc7lJ/POLCu7d5NtdLEBv94LBj5MPgrN4Nz15DAPpmWlkMZTPa1ZwxYxGoE4FTdY1N3jkayQzosk/miXqJGhmCkEG1irEQ1d+DzGQNWvHt0AAuij6ww0x9pTV7Fh+YIGQOWl7q56QzXlKquUa/5zO+p1YC2wOJSAuyw023p1KrAjBaGBrQFFiQoBeh0dI6GodhDfUxtgXWoGybq6zfAiroFNZXfAEtTw0RdLAOsqFtQU/kNsDQ1TNTFMsCKugU1ld8AS1PDRF0sA6yoW1BT+Q2wNDVM1MUywIq6BTWV3wBLU8NEXSwDrKhbUFP5DbA0NUzUxTLAiroFNZXfAEtTw0RdLAOsqFtQU/kNsDQ1TNTFMsCKugU1ld8AS1PDRF0sA6yoW1BT+Q2wNDVM1MUywIq6BTWV3wBLU8NEXSwDrKhbUFP5DbA0NUzUxTLAiroFNZXfAEtTw0RdLAOsqFtQU/kNsDQ1TNTFMsCKugU1ld8AS1PDRF0sA6yoW1BT+Q2wNDVM1MUywIq6BTWV3wBLU8NEXSwDrKhbUFP5DbA0NUzUxTLAiroFNZXfAEtTw0RdLAOsqFtQU/kNsDQ1TNTFMsCKugU1ld8AS1PDRF0sA6yoW1BT+Q2wNDVM1MUywIq6BTWV3wBLU8NEXSwDrKhbUFP5DbA0NUzUxTLAiroFNZXfAEtTw0RdLAOsqFtQU/kNsDQ1TNTFMsCKugU1ld8AS1PDRF0sA6yoW1BT+Q2wNDVM1MUywIq6BTWV3wBLU8NEXSwDrKhbUFP5DbA0NUzUxfofGmYyHujLQh0AAAAASUVORK5CYII=

摘自: https://imgtobase64.d777.com/";
		System.out.println( resizeImageTo40K(s,"2"));
    }

    /**
     * 压缩base64编码至40K以内
     *
     * @param base64Img
     * @return
     */
    public static String resizeImageTo40K(String base64Img) {
        try {
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] bytes1 = decoder.decodeBuffer(base64Img);
            InputStream stream = new ByteArrayInputStream(bytes1);
            BufferedImage src = ImageIO.read(stream);
            //BufferedImage output = Thumbnails.of(src).size(src.getWidth() / 3, src.getHeight() / 3).asBufferedImage();
            // 压缩的尺寸
            BufferedImage output = output=Thumbnails.of(src).size(120, 80).asBufferedImage();
            String base64 = imageToBase64(output);
            if (base64.length() - base64.length() / 8 * 2 > 40000) {
                output = Thumbnails.of(output).scale(1 / (base64.length() / 40000)).asBufferedImage();
                base64 = imageToBase64(output);
            }
            return base64;
        } catch (Exception e) {
            return base64Img;
        }
    }

    // BufferedImage转换成base64,在这里需要设置图片格式,如下是jpg格式图片:
    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())));
    }


}
 类似资料: