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

import com.hst.hwface.model.IDCardInfoTpm;
import com.hst.hwface.sdk.IDCardRcgSDK;
import com.hst.hwface.utils.ImageUtils;
import com.hst.hwface.utils.TheApp;
import com.sun.jna.ptr.IntByReference;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Arrays;
import javax.imageio.ImageIO;

public class IDCardRcgUtils {
    public static IDCardInfoTpm IDCardRecognize(String imageFilePath) throws Exception {
        File file = new File(imageFilePath);
        BufferedImage bufferedImage = ImageIO.read(file);
        return IDCardRcgUtils.IDCardRecognize(bufferedImage);
    }

    public static IDCardInfoTpm IDCardRecognize(BufferedImage bufferedImage) throws Exception {
        try {
            int width = bufferedImage.getWidth();
            int height = bufferedImage.getHeight();
            byte[] rgb24 = ImageUtils.convertToRGB24(bufferedImage);
            System.out.println("ID Card Image width:" + width + ", height:" + height);
            String dbPath = TheApp.getHWIdCardRcgDatabasePath();
            System.out.println(dbPath);
            int result = IDCardRcgSDK.INSTANCE.HWOCR_IDCardRcgInit(dbPath);
            if (result != 0) {
                throw new Exception("\u521d\u59cb\u5316\u5b57\u5178\u5931\u8d25\uff01result:" + result);
            }
            IntByReference pnType = new IntByReference(0);
            int aColor = 2;
            IDCardRcgSDK.TOCRRECT.ByValue rcCard = new IDCardRcgSDK.TOCRRECT.ByValue();
            rcCard.left = 0;
            rcCard.top = 0;
            rcCard.right = width - 1;
            rcCard.bottom = height - 1;
            IDCardRcgSDK.IDCARDRCGRST.ByReference pIdCardRcg = new IDCardRcgSDK.IDCARDRCGRST.ByReference();
            result = IDCardRcgSDK.INSTANCE.HWOCR_IDCardRcgBoth(rgb24, width, height, aColor, rcCard, pnType, pIdCardRcg);
            if (result != 0) {
                throw new Exception("\u8bc6\u522b\u8eab\u4efd\u8bc1\u4fe1\u606f\u5931\u8d25\uff01result:" + result);
            }
            String name = IDCardRcgUtils.converJNABytesToString(pIdCardRcg.sName, pIdCardRcg.iNamLen);
            String gender = IDCardRcgUtils.converJNABytesToString(pIdCardRcg.sGender, pIdCardRcg.iGenLen);
            String people = IDCardRcgUtils.converJNABytesToString(pIdCardRcg.sPeople, pIdCardRcg.iPeoLen);
            String birYear = IDCardRcgUtils.converJNABytesToString(pIdCardRcg.sBirYear, pIdCardRcg.iBirYLen);
            String birMonth = IDCardRcgUtils.converJNABytesToString(pIdCardRcg.sBirMonth, pIdCardRcg.iBirMLen);
            String birDay = IDCardRcgUtils.converJNABytesToString(pIdCardRcg.sBirDay, pIdCardRcg.iBirDLen);
            String address = IDCardRcgUtils.converJNABytesToString(pIdCardRcg.sAddress, pIdCardRcg.iAddLen);
            String cardId = IDCardRcgUtils.converJNABytesToString(pIdCardRcg.sIDNumber, pIdCardRcg.iIdNumLen);
            String validDate = IDCardRcgUtils.converJNABytesToString(pIdCardRcg.sValidDate, pIdCardRcg.iVDateLen);
            String authority = IDCardRcgUtils.converJNABytesToString(pIdCardRcg.sAuthority, pIdCardRcg.iAutLen);
            IDCardInfoTpm IDCardInfo = new IDCardInfoTpm();
            IDCardInfo.setName(name);
            IDCardInfo.setGender(gender);
            IDCardInfo.setNation(people);
            IDCardInfo.setBirthday(String.valueOf(birYear) + "-" + birMonth + "-" + birDay);
            IDCardInfo.setAddress(address);
            IDCardInfo.setCardId(cardId);
            IDCardInfo.setValidDate(validDate);
            IDCardInfo.setAuthority(authority);
            IDCardInfoTpm iDCardInfoTpm = IDCardInfo;
            return iDCardInfoTpm;
        }
        catch (Exception e) {
            System.out.println(e.getLocalizedMessage());
            throw e;
        }
        finally {
            IDCardRcgSDK.INSTANCE.HWOCR_IDCardRcgExit();
        }
    }

    public static String converJNABytesToString(byte[] jnaBytes, int length) {
        try {
            byte[] after = Arrays.copyOf(jnaBytes, length * 2);
            return new String(after, "UTF-16LE");
        }
        catch (Exception exception) {
            return null;
        }
    }
}
