package com.bootdo.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import javax.xml.bind.DatatypeConverter;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Base64;

public class Base64Util {
    private static final Logger logger = LoggerFactory.getLogger(Base64Util.class);

    /**
     * base64转图片
     *
     * @param base64Code base64码
     */
    public static void convertBase64ToImage(String base64Code, String picname) {

        BufferedImage image = null;
        byte[] imageByte = null;
        try {
            imageByte = DatatypeConverter.parseBase64Binary(base64Code);
            ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
            image = ImageIO.read(new ByteArrayInputStream(imageByte));
            bis.close();
            File outputfile = new File("d:\\" + picname);
            ImageIO.write(image, "jpg", outputfile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 将图片转换成Base64编码
     *
     * @param imgFile 待处理图片
     * @return
     */
    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 (Exception e) {
            logger.info("base64转换异常,url：" + imgFile);
            return "";
        }
        return java.util.Base64.getEncoder().encodeToString(data);
    }


    /**
     * 处理base64串  将+变成空格
     *
     * @param urlBase64
     * @return
     */
    public static String encodeStr(String urlBase64) {
        try {
            urlBase64 = URLEncoder.encode(urlBase64, "UTF-8");
        } catch (Exception e) {
            logger.info(StringKit.getTrace(e));
        }
        return urlBase64;
    }


    /**
     *
     * 功能描述: 获取网络地址的base64
     *
     */
    public static String getBase64ByUrl(String imageUrl) {
        try {
            // 图片 URL 地址
//            String imageUrl = "http://221.229.107.118:30521/box-im/image/20241117/1731789048895.png";

            // 创建 URL 对象
            URL url = new URL(imageUrl);

            // 打开 HTTP 连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            // 获取输入流
            InputStream inputStream = connection.getInputStream();

            // 转换图片 InputStream 为字节数组
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, bytesRead);
            }

            // 获取字节数组
            byte[] imageBytes = byteArrayOutputStream.toByteArray();

            // 将字节数组转换为 Base64 字符串
            String base64String = Base64.getEncoder().encodeToString(imageBytes);

            // 输出 Base64 字符串
//            System.out.println("Base64 String: " + base64String);

            // 关闭流
            inputStream.close();
            byteArrayOutputStream.close();
            return base64String;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

}
