南昌市物联网技防平台-公安版
zengh
2021-06-04 c926acaadc3d98fd8ba8926466b842f1edb3aee3
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
package org.springblade.jfpt.animalheat.util;
 
import jodd.util.StringPool;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.FileUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.beans.factory.annotation.Value;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
 
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
import static java.time.LocalDate.now;
 
/**
 * base64原始数据转换为图片工具类
 */
public class ImageUtil {
    static BASE64Encoder encoder = new sun.misc.BASE64Encoder();
    static BASE64Decoder decoder = new sun.misc.BASE64Decoder();
 
    private static String PRE_URL = "software/minio/data/jfpt/animalHeat";
 
    private static String PRF_MINIO_URL = "minio/jfpt/animalHeat";
 
    /**
     * base64原始数据转换为图片
     * @param base64String
     */
    public static String base64StringToImage(String base64String){
        try {
            byte[] bytes = decoder.decodeBuffer(base64String);
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            BufferedImage bi = ImageIO.read(bais);
            String UUID = DateUtil.today()+StringPool.SLASH+ StringUtil.randomUUID();
            //可以是jpg,png,gif格式
            File w2 = new File("D://"+PRE_URL+StringPool.SLASH+ UUID+".jpg");
            //判断父文件夹存在与否
            File parentFile = w2.getParentFile();
            if (!parentFile.exists()){
                //如果不存在,则创建
                parentFile.mkdirs();
            }
            //不管输出什么格式图片,此处不需改动
            ImageIO.write(bi, "jpg", w2);
            //返回图片路径
            return PRF_MINIO_URL+ StringPool.SLASH+ UUID +StringPool.DOT+ "jpg";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
 
 
    /**
     * 获取base64原始数据图片的长宽
     * @param base64String
     */
    public static Map<String,Object> getBase64ImageWidthAndHeight(String base64String){
        Map<String, Object> map = new HashMap<>();
        try {
            //将base64字符串转换成二进制字节数组
            byte[] bytes = decoder.decodeBuffer(base64String);
            //二进制输入流
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            //io读取图片输入流
            BufferedImage bi = ImageIO.read(bais);
            //封装数据
            map.put("imgWidth",bi.getWidth());
            map.put("imgHeight",bi.getHeight());
            //返回
            return map;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}