package com.ssj.kmt.utils;

import com.ssj.framework.core.util.ImageTools;
import com.ssj.framework.core.util.PropertiesUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Base64.Encoder;


public class ImageUtils {
    protected static Logger logger = LoggerFactory.getLogger(ImageUtils.class);
    public static String rotatePhoto(String src, int angle) {
        logger.info("图片路径={}", src);
        src = PropertiesUtil.getValue("file_save_path") + src;
        String fhz = "cg";
        BufferedImage bufferedimage = null;
        OutputStream out = null;
        try {//从服务器读取照片

            int h = 0;
            int w = 0;
            bufferedimage = ImageIO.read(new File(src));//从服务器中读取照片，用new URL(),读取本地磁盘中照片用new File()
            if (angle != 0) {//旋转角度不等于零
                if (angle / 90 % 2 == 1) {//旋转角度为90或270...时，图像的长款对换
                    h = bufferedimage.getWidth();
                    w = bufferedimage.getHeight();
                } else {
                    w = bufferedimage.getWidth();
                    h = bufferedimage.getHeight();
                }
            } else {//旋转角度为0时，返回
                return fhz;
            }
            int type = bufferedimage.getColorModel().getTransparency();//将像素值转换为颜色分量和 alpha 分量的方法,返回此 ColorModel 的转换类型。
            BufferedImage img;
            img = new BufferedImage(w, h, type);
            Graphics2D graphics2d = img.createGraphics();//创建一个 Graphics2D，可以将它绘制到此 BufferedImage 中。
            graphics2d.translate((w - bufferedimage.getWidth()) / 2, (h - bufferedimage.getHeight()) / 2);//将当前 Graphics2D Transform 与平移转换连接。后续呈现相对于前一位置平移指定的距离。
            graphics2d.setRenderingHint(
                    RenderingHints.KEY_INTERPOLATION,//插值提示键
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);//插值提示值——图像中最接近整数坐标样本的 4 种颜色样本被线性地插入，以生成一个颜色样本
            graphics2d.rotate(Math.toRadians(angle), bufferedimage.getWidth() / 2, bufferedimage.getHeight() / 2);//注意此处是员图片的宽和长，不是计算后的，用计算后的宽和长，保存后又黑边，因为此处错误找了三四个小时
            graphics2d.drawImage(bufferedimage, null, null);
            graphics2d.dispose();

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            ImageIO.write(img, "jpg", outputStream);
            Encoder encoder = java.util.Base64.getEncoder();
            String   result = encoder.encodeToString(outputStream.toByteArray());
            String baseStr =  "data:image/png;base64," + result.replaceAll("\r\n", "");
           // logger.info("64位编码：" + baseStr);
            fhz = ImageTools.savePic4Base64(baseStr);
        } catch (IOException e) {
            fhz = src;
            logger.error("图片旋转异常", e);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        return fhz;
    }


    /**
     * 图片知道对象编程base64
     * @param img
     * @return
     */
    public static String Img2Base64 (BufferedImage img) {
        String result = null;
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            ImageIO.write(img, "png", outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Encoder encoder = java.util.Base64.getEncoder();
        result = encoder.encodeToString(outputStream.toByteArray());
        
        return result;
    }
}
