package com.jeeplus.common.utils;

import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class ImageUtil {
    /**
     * 将路径参数的文件转为Base64字符串
     * @return
     */
    public static String getImageStr(String path){
        // 1、校验是否为空
        if (path == null || path.trim().length() <= 0) {
            return "";
        }

        // 2、校验文件是否为目录或者是否存在
        File picFile = new File(path);
        if (picFile.isDirectory() || (!picFile.exists()))
            return "";

        // 3、校验是否为图片
        try {
            BufferedImage image = ImageIO.read(picFile);
            if (image == null) {
                return "";
            }
        } catch (IOException ex) {
            ex.printStackTrace();
            return "";
        }

        // 4、转换成base64编码
        String imageStr = "   ";
        InputStream in = null;
        try {
            byte[] data = null;
            in = new FileInputStream(path);
            data = new byte[in.available()];
            in.read(data);
            BASE64Encoder encoder = new BASE64Encoder();
            imageStr = encoder.encode(data);
        } catch (Exception e) {
            imageStr = "";
            e.printStackTrace();
        }finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return imageStr;
    }
}
