/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.sun.jna.Pointer
 *  com.sun.jna.ptr.IntByReference
 *  com.sun.jna.ptr.PointerByReference
 */
package com.hst.hwface.utils;

import com.hst.hwface.sdk.HWFaceRecMultiSDK;
import com.hst.hwface.utils.ImageUtils;
import com.hst.hwface.utils.TheApp;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.PointerByReference;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import javax.imageio.ImageIO;

public class HWFaceRecUtils {
    public static float MIN_VALUE = -999.0f;
    private static Queue<PointerByReference> faceQueue = new ConcurrentLinkedQueue<PointerByReference>();
    private static int queueSize = 0;
    private static int backlog = 50;

    public static void initHWCoreDict() {
        try {
            String faceRecLibDir = TheApp.getHWFaceRecLibDir();
            int result = HWFaceRecMultiSDK.INSTANCE.HWInitialD(faceRecLibDir);
            if (result != 0) {
                System.out.println("\u521d\u59cb\u5316\u8bc6\u522b\u6838\u5fc3\u5b57\u5178\u5931\u8d25\uff01");
            } else {
                System.out.println("\u8bc6\u522b\u6838\u5fc3\u5b57\u5178\u521d\u59cb\u5316\u5b8c\u6bd5\uff01");
            }
        }
        catch (Exception e) {
            System.out.println("\u521d\u59cb\u5316\u8bc6\u522b\u6838\u5fc3\u5b57\u5178\u9519\u8bef\uff1a" + e.getLocalizedMessage());
        }
    }

    public static void releaseHWCoreDict() {
        try {
            HWFaceRecMultiSDK.INSTANCE.HWReleaseD();
            System.out.println("\u8bc6\u522b\u6838\u5fc3\u5b57\u5178\u91ca\u653e\u5b8c\u6bd5\uff01");
        }
        catch (Exception e) {
            System.out.println("\u91ca\u653e\u5b57\u5178\u9519\u8bef\uff1a" + e.getMessage());
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public static PointerByReference initHWCore() throws Exception {
        int result;
        PointerByReference pHandle;
        Queue<PointerByReference> queue = faceQueue;
        synchronized (queue) {
            if (faceQueue.isEmpty()) {
                pHandle = new PointerByReference(Pointer.NULL);
            } else {
                PointerByReference handle = faceQueue.poll();
                if (handle == null) {
                    pHandle = new PointerByReference(Pointer.NULL);
                } else {
                    --queueSize;
                    return handle;
                }
            }
        }
        try {
            result = HWFaceRecMultiSDK.INSTANCE.HWInitial(pHandle, null);
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
        if (result != 0) {
            throw new Exception("\u521d\u59cb\u5316\u8bc6\u522b\u6838\u5fc3\u5931\u8d25\uff01");
        }
        System.out.println("\u521d\u59cb\u5316\u6838\u5fc3Handle\u5b8c\u6210\uff01");
        return pHandle;
    }

    public static void releaseHWCore(PointerByReference pHandle) {
        try {
            if (pHandle != null && pHandle.getValue() != Pointer.NULL) {
                if (queueSize > backlog) {
                    HWFaceRecMultiSDK.INSTANCE.HWRelease(pHandle);
                    System.out.println("\u91ca\u653e\u6838\u5fc3Handle\u5b8c\u6210.");
                    return;
                }
                ++queueSize;
                faceQueue.add(pHandle);
            } else {
                System.out.println("\u65e0\u9700\u91ca\u653e\u6838\u5fc3Handle.");
            }
        }
        catch (Exception e) {
            System.out.println("\u91ca\u653e\u6838\u5fc3\u9519\u8bef\uff1a" + e.getMessage());
        }
    }

    public static byte[] extractFeature(PointerByReference pHandle, File imgFile) throws Exception {
        BufferedImage image = ImageIO.read(imgFile);
        int width = image.getWidth();
        int height = image.getHeight();
        byte[] pImg = ImageUtils.convertRGB32ToGray(image);
        byte[] featureBytes = HWFaceRecUtils.extractFeature(pHandle, pImg, width, height);
        return featureBytes;
    }

    public static byte[] extractFeature(PointerByReference pHandle, byte[] imgBytes) throws Exception {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(imgBytes);
        BufferedImage image = ImageIO.read(inputStream);
        int width = image.getWidth();
        int height = image.getHeight();
        byte[] pImg = ImageUtils.convertRGB32ToGray(image);
        byte[] featureBytes = HWFaceRecUtils.extractFeature(pHandle, pImg, width, height);
        return featureBytes;
    }

    public static byte[] extractFeatureWithoutLocation(PointerByReference pHandle, byte[] pImgGray, int width, int height, HWFaceRecMultiSDK.HWFaceInfo.ByReference pFaceInfo) {
        byte[] featureBytes = null;
        try {
            Pointer handle = pHandle.getValue();
            IntByReference pFeatureLen = new IntByReference(0);
            int result = HWFaceRecMultiSDK.INSTANCE.HWGetFeatureSize(handle, pFeatureLen);
            if (result != 0) {
                throw new Exception("\u83b7\u53d6\u7279\u5f81\u4e32\u5927\u5c0f\u5931\u8d25\uff01");
            }
            featureBytes = new byte[pFeatureLen.getValue()];
            result = HWFaceRecMultiSDK.INSTANCE.HWExtractFeature(handle, pImgGray, width, height, pFaceInfo, featureBytes);
            if (result != 0) {
                featureBytes = null;
                throw new Exception("\u63d0\u53d6\u6a21\u677f\u5931\u8d25\uff01");
            }
        }
        catch (Exception e) {
            System.out.println("\u53d1\u751f\u9519\u8bef\uff1a" + e.getMessage());
        }
        return featureBytes;
    }

    private static byte[] extractFeature(PointerByReference pHandle, byte[] pImgGray, int width, int height) throws Exception {
        byte[] featureBytes = null;
        try {
            Pointer handle = pHandle.getValue();
            int faceCount = 1;
            IntByReference pnMaxFace = new IntByReference(faceCount);
            HWFaceRecMultiSDK.HWFaceInfo[] pFaceInfoArr = (HWFaceRecMultiSDK.HWFaceInfo[])new HWFaceRecMultiSDK.HWFaceInfo().toArray(faceCount);
            int result = HWFaceRecMultiSDK.INSTANCE.HWDetectFaceKeyPoints(handle, pImgGray, width, height, pnMaxFace, pFaceInfoArr);
            if (result != 0 || pnMaxFace.getValue() < 1 || pFaceInfoArr.length < 1) {
                throw new Exception("\u4eba\u8138\u5b9a\u4f4d\u5931\u8d25\uff0c\u8bf7\u68c0\u67e5\u56fe\u7247\u4e2d\u4eba\u8138\u662f\u5426\u6e05\u6670\u3001\u660e\u4eae\u3001\u7aef\u6b63\uff01");
            }
            IntByReference pFeatureLen = new IntByReference(0);
            result = HWFaceRecMultiSDK.INSTANCE.HWGetFeatureSize(handle, pFeatureLen);
            if (result != 0) {
                throw new Exception("\u83b7\u53d6\u7279\u5f81\u4e32\u5927\u5c0f\u5931\u8d25\uff01");
            }
            HWFaceRecMultiSDK.HWFaceInfo.ByReference pFaceInfo = new HWFaceRecMultiSDK.HWFaceInfo.ByReference();
            pFaceInfo.m_FaceRect = pFaceInfoArr[0].m_FaceRect;
            pFaceInfo.m_afKeyPoint = pFaceInfoArr[0].m_afKeyPoint;
            pFaceInfo.m_KeyPos = pFaceInfoArr[0].m_KeyPos;
            featureBytes = new byte[pFeatureLen.getValue()];
            result = HWFaceRecMultiSDK.INSTANCE.HWExtractFeature(handle, pImgGray, width, height, pFaceInfo, featureBytes);
            if (result != 0) {
                featureBytes = null;
                throw new Exception("\u63d0\u53d6\u4eba\u8138\u7279\u5f81\u5931\u8d25\uff01");
            }
        }
        catch (Exception e) {
            throw new Exception(e.getLocalizedMessage());
        }
        return featureBytes;
    }

    public static int locationFace(PointerByReference pHandle, byte[] pImgGray, int width, int height, int maxFace, HWFaceRecMultiSDK.HWFaceInfo[] pFaceInfoArr) {
        if (maxFace < 1 || pFaceInfoArr == null || pFaceInfoArr.length == 0) {
            return 0;
        }
        Pointer handle = pHandle.getValue();
        int faceCount = 0;
        IntByReference pnMaxFace = new IntByReference(maxFace);
        int result = HWFaceRecMultiSDK.INSTANCE.HWDetectFaceKeyPoints(handle, pImgGray, width, height, pnMaxFace, pFaceInfoArr);
        if (result == 0 && pnMaxFace.getValue() > 0 || pFaceInfoArr.length > 0) {
            faceCount = pnMaxFace.getValue();
        }
        return faceCount;
    }
}
