yangys
2024-10-30 25db770e621f1259b8d5b7fd514207f7481c2d0f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.qianwen.smartman.common.utils;
 
import cn.hutool.core.codec.Base64Encoder;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import javax.imageio.ImageIO;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.lang3.StringUtils;
import com.qianwen.smartman.common.constant.DncConstant;
import org.springframework.web.multipart.MultipartFile;
 
public class PictureUtil {
    public static boolean isPicture(String imgName) {
        boolean flag = false;
        if (StringUtils.isBlank(imgName)) {
            return false;
        }
        String[] arr = {"bmp", "dib", "gif", "jfif", "jpe", "jpeg", "jpg", "png", "tif", "tiff", "ico"};
        int length = arr.length;
        int i = 0;
        while (true) {
            if (i >= length) {
                break;
            }
            String item = arr[i];
            if (!item.equals(imgName)) {
                i++;
            } else {
                flag = true;
                break;
            }
        }
        return flag;
    }
 
    public static String compressBase64(byte[] b, long desFileSize) throws IOException {
        double accuracy = 0.8d;
        if (104857.6d <= b.length && b.length <= 1048576) {
            accuracy = 0.3d;
        } else if (1048576 < b.length && b.length <= 2097152) {
            accuracy = 0.2d;
        } else if (2097152 < b.length) {
            accuracy = 0.1d;
        }
        byte[] bytes = commpressPicCycle(b, desFileSize, accuracy);
        if (bytes != null) {
            return Base64Encoder.encode(bytes);
        }
        return null;
    }
 
    public static String getSuffix(MultipartFile file) {
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(DncConstant.POINT) + 1);
        return suffix;
    }
 
    public static String commpressPicForScale(String srcPath, String desPath, long desFileSize, double accuracy, int desMaxWidth, int desMaxHeight) {
        if (StringUtils.isEmpty(srcPath) || StringUtils.isEmpty(srcPath) || !new File(srcPath).exists()) {
            return null;
        }
        try {
            File srcFile = new File(srcPath);
            long srcFileSize = srcFile.length();
            System.out.println("源图片:" + srcPath + ",大小:" + (srcFileSize / 1024) + "kb");
            BufferedImage bim = ImageIO.read(srcFile);
            int srcWidth = bim.getWidth();
            int srcHeight = bim.getHeight();
            Thumbnails.Builder builder = Thumbnails.of(new File[]{srcFile}).outputFormat("jpg");
            if (srcWidth > desMaxWidth || srcHeight > desMaxHeight) {
                builder.size(desMaxWidth, desMaxHeight);
            } else {
                builder.size(srcWidth, srcHeight);
            }
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            builder.toOutputStream(baos);
            byte[] bytes = commpressPicCycle(baos.toByteArray(), desFileSize, accuracy);
            File desFile = new File(desPath);
            FileOutputStream fos = new FileOutputStream(desFile);
            fos.write(bytes);
            fos.close();
            System.out.println("目标图片:" + desPath + ",大小" + (desFile.length() / 1024) + "kb");
            System.out.println("图片压缩完成!");
            return desPath;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    private static byte[] commpressPicCycle(byte[] bytes, long desFileSize, double accuracy) throws IOException {
        long srcFileSizeJPG = bytes.length;
        if (srcFileSizeJPG <= desFileSize * 1024) {
            return bytes;
        }
        BufferedImage bim = ImageIO.read(new ByteArrayInputStream(bytes));
        int srcWdith = bim.getWidth();
        int srcHeigth = bim.getHeight();
        int desWidth = new BigDecimal(srcWdith).multiply(new BigDecimal(accuracy)).intValue();
        int desHeight = new BigDecimal(srcHeigth).multiply(new BigDecimal(accuracy)).intValue();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Thumbnails.of(new InputStream[]{new ByteArrayInputStream(bytes)}).size(desWidth, desHeight).outputQuality(accuracy).toOutputStream(baos);
        return commpressPicCycle(baos.toByteArray(), desFileSize, accuracy);
    }
}