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

import com.hst.framework.util.HstHwFaceUtil;
import com.hst.hwface.sdk.HWFaceRecMultiSDK;
import com.hst.hwface.utils.HWFaceRecUtils;
import com.hst.hwface.utils.ImageUtils;
import com.sun.jna.ptr.FloatByReference;
import com.sun.jna.ptr.PointerByReference;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileSystemView;

public class FrmMain
extends JFrame {
    private JLabel lblPath1;
    private JTextField txtPath1;
    private JLabel lblPath2;
    private JTextField txtPath2;
    private JFileChooser fchPath;
    private JButton btnPath1;
    private JButton btnPath2;
    private JButton btnCompare;
    private JLabel lblTitle;
    PointerByReference pHandle = null;

    private void initLayoutAndProperties() {
        this.setTitle("\u56fe\u7247\u5bf9\u6bd4Demo");
        this.setSize(460, 220);
        this.setDefaultCloseOperation(3);
        this.setLayout(null);
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        int screenWidth = screenSize.width;
        int screenHeight = screenSize.height;
        int windowWidth = this.getWidth();
        int windowHeight = this.getHeight();
        this.setLocation(screenWidth / 2 - windowWidth / 2, screenHeight / 2 - windowHeight / 2);
    }

    private void initComponents() {
        this.fchPath = new JFileChooser();
        this.fchPath.setFileSelectionMode(0);
        this.fchPath.setApproveButtonText("\u786e\u5b9a");
        this.fchPath.setCurrentDirectory(FileSystemView.getFileSystemView().getHomeDirectory());
        this.fchPath.setAcceptAllFileFilterUsed(false);
        this.fchPath.setFileFilter(new FileNameExtensionFilter("*.gif|*.jpg|*.jpeg|*.bmp|*.png", "gif", "jpg", "jpeg", "bmp", "png"));
        this.lblPath1 = new JLabel();
        this.lblPath1.setSize(60, 22);
        this.lblPath1.setLocation(10, 15);
        this.lblPath1.setText("\u56fe\u7247\u4e00");
        this.txtPath1 = new JTextField();
        this.txtPath1.setSize(300, 28);
        this.txtPath1.setLocation(60, 13);
        this.txtPath1.setEditable(false);
        this.btnPath1 = new JButton();
        this.btnPath1.setText("\u9009\u62e9");
        this.btnPath1.setSize(55, 28);
        this.btnPath1.setLocation(370, 13);
        this.btnPath1.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                FrmMain.this.btnPath_Click(1);
            }
        });
        this.lblPath2 = new JLabel();
        this.lblPath2.setSize(60, 22);
        this.lblPath2.setLocation(10, 42);
        this.lblPath2.setText("\u56fe\u7247\u4e8c");
        this.txtPath2 = new JTextField();
        this.txtPath2.setSize(300, 28);
        this.txtPath2.setLocation(60, 40);
        this.txtPath2.setEditable(false);
        this.btnPath2 = new JButton();
        this.btnPath2.setText("\u9009\u62e9");
        this.btnPath2.setSize(55, 28);
        this.btnPath2.setLocation(370, 40);
        this.btnPath2.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                FrmMain.this.btnPath_Click(2);
            }
        });
        this.btnCompare = new JButton();
        this.btnCompare.setText("\u5bf9\u6bd4");
        this.btnCompare.setSize(120, 40);
        this.btnCompare.setLocation(150, 120);
        this.btnCompare.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                FrmMain.this.comparePhoto(new File(FrmMain.this.txtPath1.getText()), new File(FrmMain.this.txtPath2.getText()));
            }
        });
        this.lblTitle = new JLabel();
        this.lblTitle.setSize(200, 30);
        this.lblTitle.setLocation(155, 90);
        this.lblTitle.setText("\u521d\u59cb\u5316\u6838\u5fc3\u4e2d...");
        this.add(this.lblPath1);
        this.add(this.txtPath1);
        this.add(this.btnPath1);
        this.add(this.lblPath2);
        this.add(this.txtPath2);
        this.add(this.btnPath2);
        this.add(this.btnCompare);
        this.add(this.lblTitle);
    }

    private void btnPath_Click(int value) {
        int result = this.fchPath.showOpenDialog(this);
        if (result == 0) {
            if (value == 1) {
                this.txtPath1.setText(this.fchPath.getSelectedFile().getPath());
            } else {
                this.txtPath2.setText(this.fchPath.getSelectedFile().getPath());
            }
        }
    }

    public FrmMain() {
        this.initLayoutAndProperties();
        this.initComponents();
        new HstHwFaceUtil.init(new HstHwFaceUtil()).start();
    }

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        new FrmMain().setVisible(true);
    }

    public void comparePhoto(File file1, File file2) {
        block11: {
            try {
                try {
                    if (file1 == null || file2 == null || !this.isImageFile(file1.getAbsolutePath()) || !this.isImageFile(file2.getAbsolutePath())) {
                        throw new Exception("\u8bf7\u9009\u62e9\u7167\u7247\u6587\u4ef6\uff01");
                    }
                    BufferedImage image1 = ImageIO.read(file1);
                    int width1 = image1.getWidth();
                    int height1 = image1.getHeight();
                    byte[] pImgGray1 = ImageUtils.convertRGB32ToGray(image1);
                    BufferedImage image2 = ImageIO.read(file2);
                    int width2 = image2.getWidth();
                    int height2 = image2.getHeight();
                    byte[] pImgGray2 = ImageUtils.convertRGB32ToGray(image2);
                    HWFaceRecMultiSDK.HWFaceInfo[] pFaceInfos1 = (HWFaceRecMultiSDK.HWFaceInfo[])new HWFaceRecMultiSDK.HWFaceInfo().toArray(2);
                    HWFaceRecMultiSDK.HWFaceInfo[] pFaceInfos2 = (HWFaceRecMultiSDK.HWFaceInfo[])new HWFaceRecMultiSDK.HWFaceInfo().toArray(2);
                    int faceCount1 = HWFaceRecUtils.locationFace(this.pHandle, pImgGray1, width1, height1, 2, pFaceInfos1);
                    if (faceCount1 < 1) {
                        throw new Exception("\u672a\u80fd\u5728\u7167\u7247\u4e2d\u5b9a\u4f4d\u5230\u4eba\u8138\uff01\u7167\u7247\u540d\u79f0\uff1a" + file1.getAbsolutePath());
                    }
                    if (faceCount1 > 1) {
                        throw new Exception("\u7167\u7247\u5b9a\u4f4d\u5230\u591a\u4e2a\u4eba\u8138\uff0c\u4e0d\u7b26\u5408\u8981\u6c42\uff0c\u8981\u6c42\u53ea\u80fd\u6709\u4e00\u4e2a\u4eba\u8138\uff01\u7167\u7247\u540d\u79f0\uff1a" + file1.getAbsolutePath());
                    }
                    int faceCount2 = HWFaceRecUtils.locationFace(this.pHandle, pImgGray2, width2, height2, 2, pFaceInfos2);
                    if (faceCount2 < 1) {
                        throw new Exception("\u672a\u80fd\u5728\u7167\u7247\u4e2d\u5b9a\u4f4d\u5230\u4eba\u8138\uff01\u7167\u7247\u540d\u79f0\uff1a" + file2.getAbsolutePath());
                    }
                    if (faceCount2 > 1) {
                        throw new Exception("\u7167\u7247\u5b9a\u4f4d\u5230\u591a\u4e2a\u4eba\u8138\uff0c\u4e0d\u7b26\u5408\u8981\u6c42\uff0c\u8981\u6c42\u53ea\u80fd\u6709\u4e00\u4e2a\u4eba\u8138\uff01\u7167\u7247\u540d\u79f0\uff1a" + file2.getAbsolutePath());
                    }
                    HWFaceRecMultiSDK.HWFaceInfo.ByReference pFaceInfo1 = new HWFaceRecMultiSDK.HWFaceInfo.ByReference();
                    pFaceInfo1.m_FaceRect = pFaceInfos1[0].m_FaceRect;
                    pFaceInfo1.m_afKeyPoint = pFaceInfos1[0].m_afKeyPoint;
                    pFaceInfo1.m_KeyPos = pFaceInfos1[0].m_KeyPos;
                    byte[] feature1 = HWFaceRecUtils.extractFeatureWithoutLocation(this.pHandle, pImgGray1, width1, height1, pFaceInfo1);
                    HWFaceRecMultiSDK.HWFaceInfo.ByReference pFaceInfo2 = new HWFaceRecMultiSDK.HWFaceInfo.ByReference();
                    pFaceInfo2.m_FaceRect = pFaceInfos2[0].m_FaceRect;
                    pFaceInfo2.m_afKeyPoint = pFaceInfos2[0].m_afKeyPoint;
                    pFaceInfo2.m_KeyPos = pFaceInfos2[0].m_KeyPos;
                    byte[] feature2 = HWFaceRecUtils.extractFeatureWithoutLocation(this.pHandle, pImgGray2, width2, height2, pFaceInfo2);
                    FloatByReference fScore = new FloatByReference(0.0f);
                    int result = HWFaceRecMultiSDK.INSTANCE.HWCompareFeature(this.pHandle.getValue(), feature1, feature2, fScore);
                    if (result == 0) {
                        this.lblTitle.setText("\u5bf9\u6bd4\u7ed3\u679c\uff1a" + fScore.getValue());
                        System.out.println(fScore.getValue());
                        break block11;
                    }
                    throw new Exception("\u5bf9\u6bd4\u5931\u8d25\uff01");
                }
                catch (Exception e) {
                    String cfr_ignored_0 = "\u53d1\u751f\u9519\u8bef\uff1a" + e.getLocalizedMessage();
                    e.printStackTrace();
                    HWFaceRecUtils.releaseHWCore(this.pHandle);
                }
            }
            finally {
                HWFaceRecUtils.releaseHWCore(this.pHandle);
            }
        }
    }

    protected boolean isImageFile(String fileName) {
        String[] imgExts;
        boolean isImage = false;
        String[] stringArray = imgExts = new String[]{".gif", ".jpg", ".jpeg", ".bmp", ".png"};
        int n = imgExts.length;
        int n2 = 0;
        while (n2 < n) {
            String ext = stringArray[n2];
            if (fileName.toLowerCase().endsWith(ext)) {
                isImage = true;
            }
            ++n2;
        }
        return isImage;
    }

    public class init
    extends Thread {
        @Override
        public void run() {
            try {
                System.out.println("-------------\u521d\u59cb\u5316-----------");
                FrmMain.this.pHandle = HWFaceRecUtils.initHWCore();
                if (FrmMain.this.pHandle != null) {
                    FrmMain.this.lblTitle.setText("\u521d\u59cb\u5316\u6838\u5fc3\u6210\u529f\uff01");
                } else {
                    FrmMain.this.lblTitle.setText("\u521d\u59cb\u5316\u6838\u5fc3\u5931\u8d25\uff01");
                }
            }
            catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }
}
