package com.ibeetl.admin.core.util;

import sun.misc.BASE64Encoder;

import java.io.FileInputStream;
import java.io.InputStream;

/**图片工具类
 * Created by Skipper on 2018/12/10.
 */
public class ImageUtil {
    /**
     * 图片路径组装
     * @param localPath
     * @param imgFile
     * @return
     */
    public static String getImageStr(String localPath, String imgFile) {
        InputStream inputStream = null;
        byte[] data = null;
        String imgtitle="";
        try {
            if (imgFile.matches("^.+\\.(?i)(jpg)$")){
                imgtitle="data:image/jpg;base64,";
            }else if(imgFile.matches("^.+\\.(?i)(png)$")){
                imgtitle="data:image/png;base64,";
            }else if(imgFile.matches("^.+\\.(?i)(gif)$")){
                imgtitle="data:image/gif;base64,";
            }
            inputStream = new FileInputStream(localPath+"/"+imgFile);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        // 加密
        BASE64Encoder encoder = new BASE64Encoder();
        return imgtitle+encoder.encode(data);
    }
}
