package com.ktg.common.utils.face;

import com.ktg.common.config.RuoYiConfig;
import com.ktg.common.utils.StringUtils;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.imgcodecs.Imgcodecs;

import java.util.Base64;

/**
 *
 *
 * @author cgj
 */
public class MatChangeUtil
{

    // 加载OpenCV本地库
    static {
        String prodApi = RuoYiConfig.getProd();
        String loadPath = "/guoruan/app/opencv/build/java/x64/opencv_java470.dll";
        if (StringUtils.isBlank(prodApi)) {
            loadPath = "C:/work/app/install/opencv/build/java/x64/opencv_java470.dll";
        }
        System.load(loadPath);
    }
    public static String matToStr(Mat mat) {
        // 将MAT对象编码为图像格式的字节数组
        MatOfByte matOfByte = new MatOfByte();
        Imgcodecs.imencode(".jpg", mat, matOfByte);
        byte[] byteArray = matOfByte.toArray();
        // 将字节数组转换为Base64编码的字符串
        return Base64.getEncoder().encodeToString(byteArray);
    }


    public static Mat strToMat(String base64String, int rows, int cols, int type) {
        // 将Base64编码的字符串解码为字节数组
        byte[] byteArray = Base64.getDecoder().decode(base64String);
        // 将字节数组转换为MAT对象
        MatOfByte matOfByte = new MatOfByte(byteArray);
        return Imgcodecs.imdecode(matOfByte, Imgcodecs.IMREAD_UNCHANGED);
    }

}
