// DemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Demo.h"
#include "DemoDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDemoDlg dialog

CDemoDlg::CDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDemoDlg)
	m_Used = _T("");
	m_SN = _T("");
	m_COUNT = _T("");
	m_Regname = _T("");
	m_Hint = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	s_zkfp = _T("");
}

void CDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDemoDlg)
	DDX_Control(pDX, IDC_ZKFPENGX2, m_zkfp);
	DDX_Text(pDX, IDC_EDUsed, m_Used);
	DDX_Text(pDX, IDC_EDSN, m_SN);
	DDX_Text(pDX, IDC_EDCount, m_COUNT);
	DDX_Text(pDX, IDC_EDReg, m_Regname);
	DDX_Text(pDX, IDC_EDHint, m_Hint);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDemoDlg, CDialog)
	//{{AFX_MSG_MAP(CDemoDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTNInit, OnBTNInit)
	ON_BN_CLICKED(IDC_BTNSave, OnBTNSave)
	ON_BN_CLICKED(IDC_BTNReg, OnBTNReg)
	ON_BN_CLICKED(IDC_BTNIdentify, OnBTNIdentify)
	ON_BN_CLICKED(IDC_BTNVer, OnBTNVer)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDemoDlg message handlers

BOOL CDemoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here

	FingerCount = 0;
    fpcHandle = m_zkfp.CreateFPCacheDB();
	VariantInit(&FRegTemplate);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CDemoDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CDemoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDemoDlg::OnBTNInit() 
{
	char buffer[10];

	CheckRadioButton(IDC_RADIOBMP, IDC_RADIOJPG, IDC_RADIOBMP);
	// TODO: Add your control notification handler code here
	if (m_zkfp.InitEngine() == 0){
		m_SN = m_zkfp.GetSensorSN();
		ltoa(m_zkfp.GetSensorIndex(), buffer, 10);
		m_Used = buffer;
		ltoa(m_zkfp.GetSensorCount(), buffer, 10);
		m_COUNT = buffer;
		SetDlgItemText(IDC_EDUsed, m_Used);
		SetDlgItemText(IDC_EDCount, m_COUNT);
		SetDlgItemText(IDC_EDSN, m_SN);		
		MessageBox("初始化成功!");
	} else
		MessageBox("初始化失败!");
	FMatchType = 0;
}

BEGIN_EVENTSINK_MAP(CDemoDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CDemoDlg)
	ON_EVENT(CDemoDlg, IDC_ZKFPENGX2, 8 /* OnImageReceived */, OnOnImageReceivedZkfpengx2, VTS_PBOOL)
	ON_EVENT(CDemoDlg, IDC_ZKFPENGX2, 10 /* OnCapture */, OnOnCaptureZkfpengx2, VTS_BOOL VTS_VARIANT)
	ON_EVENT(CDemoDlg, IDC_ZKFPENGX2, 9 /* OnEnroll */, OnOnEnrollZkfpengx2, VTS_BOOL VTS_VARIANT)
	ON_EVENT(CDemoDlg, IDC_ZKFPENGX2, 5 /* OnFeatureInfo */, OnOnFeatureInfoZkfpengx2, VTS_I4)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CDemoDlg::OnOnImageReceivedZkfpengx2(BOOL FAR* AImageValid) 
{
	// TODO: Add your control notification handler code here
	HDC hdc;

	hdc = this->GetDC()->m_hDC;

	m_zkfp.PrintImageAt(long(hdc), 330, 2, m_zkfp.GetImageWidth(), m_zkfp.GetImageHeight());
}

void CDemoDlg::OnBTNSave() 
{
	// TODO: Add your control notification handler code here
	if (IsDlgButtonChecked(IDC_RADIOBMP) != 0)
		m_zkfp.SaveBitmap("c:\\fingerprint.bmp");
	else
		m_zkfp.SaveJPG("c:\fingerprint.jpg");
	MessageBox("图像保存到C:\\FingerPrint");
}

void CDemoDlg::OnBTNReg() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if (m_Regname.Compare("") == 0){
		MessageBox("请输入指纹名称标识!");
		return;
	}
	m_zkfp.BeginEnroll();
	SetDlgItemText(IDC_EDHint, "开始登记");		
}

void CDemoDlg::OnBTNIdentify() 
{
	// TODO: Add your control notification handler code here
    if (m_zkfp.GetIsRegister())
        m_zkfp.CancelEnroll();
	SetDlgItemText(IDC_EDHint, "开始比对(1:N)");		
    FMatchType = 2;	
}

void CDemoDlg::OnBTNVer() 
{
	// TODO: Add your control notification handler code here
    if (m_zkfp.GetIsRegister())
        m_zkfp.CancelEnroll();
	SetDlgItemText(IDC_EDHint, "开始比对(1:1)");		
    FMatchType = 1;		
}

BOOL CDemoDlg::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
    m_zkfp.FreeFPCacheDB(fpcHandle);	
	return CDialog::DestroyWindow();
}

void CDemoDlg::OnOnCaptureZkfpengx2(BOOL ActionResult, const VARIANT FAR& ATemplate) 
{
	// TODO: Add your control notification handler code here
  long fi;
  long Score, ProcessNum;
  BOOL RegChanged;
  char buffer[80];

  SetDlgItemText(IDC_EDHint, "取得指纹特征模板");		
  if (FMatchType == 1){
     if (m_zkfp.VerFinger(&FRegTemplate, ATemplate, FALSE, &RegChanged))
        MessageBox("指纹比对成功！");
     else
        MessageBox("指纹比对失败！");

  }	else if (FMatchType == 2){  //1:N
		Score = 8;
		fi = m_zkfp.IdentificationInFPCacheDB(fpcHandle, ATemplate, &Score, &ProcessNum);
	    if (fi == -1)
	       MessageBox("指纹比对失败！");
		else{
		   sprintf(buffer, "指纹比对成功！Name = %s Score = %d  Processed Number = %d", FFingerNames[fi], Score, ProcessNum);
    	   MessageBox(buffer);
		}
	}   
}

void CDemoDlg::OnOnEnrollZkfpengx2(BOOL ActionResult, const VARIANT FAR& ATemplate) 
{
	// TODO: Add your control notification handler code here
  VARIANT pTemplate;
  
  if (!ActionResult)
    MessageBox("指纹登记失败！");
  else{
    MessageBox("指纹登记成功！");

	VariantClear(&FRegTemplate);
	pTemplate = m_zkfp.GetTemplate();

	s_zkfp = m_zkfp.EncodeTemplate1(pTemplate);

    VariantCopy(&FRegTemplate, &pTemplate);
    
    m_zkfp.SaveTemplate("c:\\fingerprint.tpl", ATemplate);
    m_zkfp.AddRegTemplateFileToFPCacheDB(fpcHandle, FingerCount, "c:\\fingerprint.tpl");
	UpdateData(TRUE);
	strcpy(FFingerNames[FingerCount], m_Regname);
    FingerCount = FingerCount + 1;
  }
}

void CDemoDlg::OnOnFeatureInfoZkfpengx2(long AQuality) 
{
	// TODO: Add your control notification handler code here
  CString sTemp;
  char buffer[10];

  sTemp = "";
  if (m_zkfp.GetIsRegister()){
	 ltoa(m_zkfp.GetEnrollIndex(), buffer, 10);
     sTemp = "登记状态: 还需要按压";
	 sTemp = sTemp + buffer + "次手指!";
  }
  sTemp = sTemp + " 指纹质量";
  ltoa(AQuality, buffer, 10);
  if (AQuality != 0)
     sTemp = sTemp + "不合格: "  + buffer;
  else
     sTemp = sTemp +  "合格";
  SetDlgItemText(IDC_EDHint, sTemp);
}
