【Java】将Base64格式的图片等比伸缩至目标尺寸代码实现

需求

前端页面上传的图片是Base64字符串,需要根据目标尺寸进行伸缩,不能改变图片的比例

代码实现

使用图片处理工具:thumbnailator

引入Maven依赖:

        
            net.coobird
            thumbnailator
            0.4.8
        

核心代码:

import net.coobird.thumbnailator.Thumbnails;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Base64;
import java.util.Objects;

public class Test {
    public static final String IMAGE_BASE64_PREFIX_REG = "data:image/[^;]+;base64,";
  	// 你的测试数据
    private static final String IMG_BASE64 = "";
    private static final String FILE_PATH = "/Users/xdf/Desktop/Temp/test.png";
    private static Integer targetHeight = 400;
    private static Integer targetWidth = 400;

    public static void main(String[] args) throws IOException {
        DecimalFormat df = new DecimalFormat("#.##");
        df.setRoundingMode(RoundingMode.FLOOR);
        String image = IMG_BASE64.replaceAll(IMAGE_BASE64_PREFIX_REG, "");
        BufferedImage bufferedImage = convertBase64ToImage(image);
        if (Objects.isNull(bufferedImage)) {
            System.err.println("covert base64 image error.");
            return;
        }
        int height = bufferedImage.getHeight();
        int width = bufferedImage.getWidth();
        // 计算缩放比例,目标尺寸/当前尺寸,大于0则需要放大(目标大),小于0则需要缩放(目标小)
        double heightScale = Double.parseDouble(df.format((double) targetHeight / height));
        double widthScale = Double.parseDouble(df.format((double) targetWidth / width));
        System.out.printf("[current height]: %d, [current width]: %d \n[target height]: %d, [target width]: %d\n", height, width, targetHeight, targetWidth);
        // 放大时,需要以目标较长的一边作为放大基准;缩小时,需要以当前尺寸较短的一边为基准(当目标尺寸大时,分子越大,差距越大,当目标尺寸小时,分母越小,差距越大,两种情况都会导致商更大)
        double scale = Math.max(heightScale, widthScale);
        System.out.printf("[heightScale]: %s, [widthScale]: %s, [final scale]: %s \n", heightScale, widthScale, scale);
        BufferedImage targetImage = Thumbnails.of(bufferedImage).scale(scale).asBufferedImage();
        ImageIO.write(targetImage, "png", new File(FILE_PATH));
    }

    public static BufferedImage convertBase64ToImage(String base64String) {
        try {
            // 解码Base64字符串为字节数组
            byte[] imageBytes = Base64.getDecoder().decode(base64String);
            // 创建ByteArrayInputStream对象
            ByteArrayInputStream bis = new ByteArrayInputStream(imageBytes);
            // 将ByteArrayInputStream对象转换为BufferedImage对象
            BufferedImage bufferedImage = ImageIO.read(bis);
            // 关闭ByteArrayInputStream
            bis.close();
            // 返回BufferedImage对象
            return bufferedImage;
        } catch (Exception e) {
            System.err.println(e);
        }
        return null;
    }
}

这个方案是保证图片完整的情况下,尽量接近目标尺寸,如果要精确等于目标尺寸,还需要用到裁剪功能。

你可能感兴趣的:(Java,java,开发语言,图片处理,thumbnailator)