package com.bootdo.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.codec.binary.Base64;
import sun.misc.BASE64Decoder;

import javax.imageio.ImageIO;
import javax.xml.bind.DatatypeConverter;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * 生成图片工具类
 */
public class PicFile {

    private static BASE64Decoder decoder = new BASE64Decoder();

    /**
     * 生成图片
     */
    public static boolean generateImage1(String filePath, String imgBase64Str, String fileName) {
        if (StringUtils.isBlank(filePath)) {
            return false;
        }
        if (StringUtils.isBlank(imgBase64Str)) {
            return false;
        }
        if (StringUtils.isBlank(fileName)) {
            return false;
        }
        try {
            byte[] b = decoder.decodeBuffer(imgBase64Str);
            ByteArrayInputStream inputStream = new ByteArrayInputStream(b);
            File file = new File(filePath);
            if (!file.exists()) {
                file.mkdirs();
            }
            if (!filePath.endsWith("/")) {
                filePath = filePath + "/";
            }
            File f = new File(filePath + fileName);
            BufferedImage bi = ImageIO.read(inputStream);
            ImageIO.write(bi, "jpg", f);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * base64转图片
     */
    public static boolean generateImage(String filePath, String imgBase64Str, String fileName) {
        if (StringUtils.isBlank(filePath)) {
            return false;
        }
        if (StringUtils.isBlank(imgBase64Str)) {
            return false;
        }
        if (StringUtils.isBlank(fileName)) {
            return false;
        }
        BufferedImage image = null;
        byte[] imageByte = null;
        try {
            imageByte = DatatypeConverter.parseBase64Binary(imgBase64Str);
            ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
            image = ImageIO.read(new ByteArrayInputStream(imageByte));
            bis.close();
            File outPutFile = new File(filePath + fileName);
            ImageIO.write(image, "jpg", outPutFile);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * base64转图片
     */
    public static boolean generateImage(String filePathName, String imgBase64Str) {
        if (StringUtils.isBlank(filePathName)) {
            return false;
        }
        if (StringUtils.isBlank(imgBase64Str)) {
            return false;
        }
        BufferedImage image = null;
        byte[] imageByte = null;
        try {
            imageByte = DatatypeConverter.parseBase64Binary(imgBase64Str);
            ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
            image = ImageIO.read(new ByteArrayInputStream(imageByte));
            bis.close();
            File outPutFile = new File(filePathName + ".tmp");
            String fileFolder = outPutFile.getParent();
            File folderFile = new File(fileFolder);
            if(!folderFile.exists()){
                folderFile.mkdirs();
            }

            ImageIO.write(image, "jpg", outPutFile);
            outPutFile.renameTo(new File(filePathName));
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 将图片转换成Base64编码
     */
    public static String getImgStr(String imgFile) {
        //将图片文件转化为字节数组字符串，并对其进行Base64编码处理
        InputStream in = null;
        byte[] data = null;
        //读取图片字节数组
        try {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
        return new String(Base64.encodeBase64(data));
    }

}
