Skip to content
Permalink
1 contributor

Users who have contributed to this file

598 lines (452 sloc) 19.3 KB
using libzkfpcsharp;
using System;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using ZKTecoFingerPrintScanner_Implementation;
using ZKTecoFingerPrintScanner_Implementation.Helpers;
namespace Dofe_Re_Entry.UserControls.DeviceController
{
public partial class FingerPrintControl : UserControl
{
const string VerifyButtonDefault = "Verify";
const string VerifyButtonToggle = "Stop Verification";
const string Disconnected = "Disconnected";
Thread captureThread = null;
public Master parentForm = null;
#region -------- FIELDS --------
const int REGISTER_FINGER_COUNT = 3;
zkfp fpInstance = new zkfp();
IntPtr FormHandle = IntPtr.Zero; // To hold the handle of the form
bool bIsTimeToDie = false;
bool IsRegister = false;
bool bIdentify = true;
byte[] FPBuffer; // Image Buffer
int RegisterCount = 0;
byte[][] RegTmps = new byte[REGISTER_FINGER_COUNT][];
byte[] RegTmp = new byte[2048];
byte[] CapTmp = new byte[2048];
int cbCapTmp = 2048;
int regTempLen = 0;
int iFid = 1;
const int MESSAGE_CAPTURED_OK = 0x0400 + 6;
private int mfpWidth = 0;
private int mfpHeight = 0;
#endregion
// [ CONSTRUCTOR ]
public FingerPrintControl()
{
InitializeComponent();
ReInitializeInstance();
}
// [ INITALIZE DEVICE ]
private void bnInit_Click(object sender, EventArgs e)
{
parentForm.statusBar.Visible = false;
cmbIdx.Items.Clear();
int initializeCallBackCode = fpInstance.Initialize();
if (zkfp.ZKFP_ERR_OK == initializeCallBackCode)
{
int nCount = fpInstance.GetDeviceCount();
if (nCount > 0)
{
for (int i = 1; i <= nCount; i++) cmbIdx.Items.Add(i.ToString());
cmbIdx.SelectedIndex = 0;
btnInit.Enabled = false;
DisplayMessage(MessageManager.msg_FP_InitComplete, true);
}
else
{
int finalizeCount = fpInstance.Finalize();
DisplayMessage(MessageManager.msg_FP_NotConnected, false);
}
// CONNECT DEVICE
#region -------- CONNECT DEVICE --------
int openDeviceCallBackCode = fpInstance.OpenDevice(cmbIdx.SelectedIndex);
if (zkfp.ZKFP_ERR_OK != openDeviceCallBackCode)
{
DisplayMessage($"Uable to connect with the device! (Return Code: {openDeviceCallBackCode} )", false);
return;
}
Utilities.EnableControls(false, btnInit);
Utilities.EnableControls(true, btnClose, btnEnroll, btnVerify, btnIdentify, btnFree);
RegisterCount = 0;
regTempLen = 0;
iFid = 1;
//for (int i = 0; i < 3; i++)
for (int i = 0; i < REGISTER_FINGER_COUNT; i++)
{
RegTmps[i] = new byte[2048];
}
byte[] paramValue = new byte[4];
int size = 4;
//fpInstance.GetParameters
fpInstance.GetParameters(1, paramValue, ref size);
zkfp2.ByteArray2Int(paramValue, ref mfpWidth);
size = 4;
fpInstance.GetParameters(2, paramValue, ref size);
zkfp2.ByteArray2Int(paramValue, ref mfpHeight);
FPBuffer = new byte[mfpWidth * mfpHeight];
//FPBuffer = new byte[fpInstance.imageWidth * fpInstance.imageHeight];
captureThread = new Thread(new ThreadStart(DoCapture));
captureThread.IsBackground = true;
captureThread.Start();
bIsTimeToDie = false;
string devSN = fpInstance.devSn;
lblDeviceStatus.Text = "Connected \nDevice S.No: " + devSN;
DisplayMessage("You are now connected to the device.", true);
#endregion
}
else
DisplayMessage("Unable to initailize the device. " + FingerPrintDeviceUtilities.DisplayDeviceErrorByCode(initializeCallBackCode) + " !! ", false);
}
// [ CAPTURE FINGERPRINT ]
private void DoCapture()
{
try
{
while (!bIsTimeToDie)
{
cbCapTmp = 2048;
int ret = fpInstance.AcquireFingerprint(FPBuffer, CapTmp, ref cbCapTmp);
if (ret == zkfp.ZKFP_ERR_OK)
{
//if (RegisterCount == 0)
// btnEnroll.Invoke((Action)delegate
// {
// btnEnroll.Enabled = true;
// });
SendMessage(FormHandle, MESSAGE_CAPTURED_OK, IntPtr.Zero, IntPtr.Zero);
}
Thread.Sleep(100);
}
}
catch { }
}
[DllImport("user32.dll", EntryPoint = "SendMessageA")]
public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
private void bnIdentify_Click(object sender, EventArgs e)
{
if (!bIdentify)
{
bIdentify = true;
DisplayMessage(MessageManager.msg_FP_PressForIdentification, true);
}
}
private void bnVerify_Click(object sender, EventArgs e)
{
if (bIdentify)
{
bIdentify = false;
btnVerify.Text = VerifyButtonToggle;
DisplayMessage(MessageManager.msg_FP_PressForVerification, true);
}
else
{
bIdentify = true;
btnVerify.Text = VerifyButtonDefault;
}
}
protected override void DefWndProc(ref Message m)
{
switch (m.Msg)
{
case MESSAGE_CAPTURED_OK:
{
parentForm.statusBar.Visible = false;
DisplayFingerPrintImage();
if (IsRegister)
{
#region -------- IF REGISTERED FINGERPRINT --------
int ret = zkfp.ZKFP_ERR_OK;
int fid = 0, score = 0;
ret = fpInstance.Identify(CapTmp, ref fid, ref score);
if (zkfp.ZKFP_ERR_OK == ret)
{
int deleteCode = fpInstance.DelRegTemplate(fid); // <---- REMOVE FINGERPRINT
if (deleteCode != zkfp.ZKFP_ERR_OK)
{
DisplayMessage(MessageManager.msg_FP_CurrentFingerAlreadyRegistered + fid, false);
return;
}
}
if (RegisterCount > 0 && fpInstance.Match(CapTmp, RegTmps[RegisterCount - 1]) <= 0)
{
DisplayMessage("Please press the same finger " + REGISTER_FINGER_COUNT + " times for enrollment", true);
return;
}
Array.Copy(CapTmp, RegTmps[RegisterCount], cbCapTmp);
if (RegisterCount == 0) btnEnroll.Enabled = false;
RegisterCount++;
if (RegisterCount >= REGISTER_FINGER_COUNT)
{
RegisterCount = 0;
ret = GenerateRegisteredFingerPrint(); // <--- GENERATE FINGERPRINT TEMPLATE
if (zkfp.ZKFP_ERR_OK == ret)
{
ret = AddTemplateToMemory(); // <--- LOAD TEMPLATE TO MEMORY
if (zkfp.ZKFP_ERR_OK == ret) // <--- ENROLL SUCCESSFULL
{
string fingerPrintTemplate = string.Empty;
zkfp.Blob2Base64String(RegTmp, regTempLen, ref fingerPrintTemplate);
Utilities.EnableControls(true, btnVerify, btnIdentify);
Utilities.EnableControls(false, btnEnroll);
// GET THE TEMPLATE HERE : fingerPrintTemplate
DisplayMessage(MessageManager.msg_FP_EnrollSuccessfull, true);
DisconnectFingerPrintCounter();
}
else
DisplayMessage(MessageManager.msg_FP_FailedToAddTemplate, false);
}
else
DisplayMessage(MessageManager.msg_FP_UnableToEnrollCurrentUser + ret, false);
IsRegister = false;
return;
}
else
{
int remainingCont = REGISTER_FINGER_COUNT - RegisterCount;
lblFingerPrintCount.Text = remainingCont.ToString();
string message = "Please provide your fingerprint " + remainingCont + " more time(s)";
DisplayMessage(message, true);
}
#endregion
}
else
{
#region ------- IF RANDOM FINGERPRINT -------
// If unidentified random fingerprint is applied
if (regTempLen <= 0)
{
DisplayMessage(MessageManager.msg_FP_UnidentifiedFingerPrint, false);
return;
}
if (bIdentify)
{
int ret = zkfp.ZKFP_ERR_OK;
int fid = 0, score = 0;
ret = fpInstance.Identify(CapTmp, ref fid, ref score);
if (zkfp.ZKFP_ERR_OK == ret)
{
DisplayMessage(MessageManager.msg_FP_IdentificationSuccess + ret, true);
return;
}
else
{
DisplayMessage(MessageManager.msg_FP_IdentificationFailed + ret, false);
return;
}
}
else
{
int ret = fpInstance.Match(CapTmp, RegTmp);
if (0 < ret)
{
DisplayMessage(MessageManager.msg_FP_MatchSuccess + ret, true);
return;
}
else
{
DisplayMessage(MessageManager.msg_FP_MatchFailed + ret, false);
return;
}
}
#endregion
}
}
break;
default:
base.DefWndProc(ref m);
break;
}
}
/// <summary>
/// FREE RESOURCES
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void bnFree_Click(object sender, EventArgs e)
{
int result = fpInstance.Finalize();
if (result == zkfp.ZKFP_ERR_OK)
{
DisconnectFingerPrintCounter();
IsRegister = false;
RegisterCount = 0;
regTempLen = 0;
ClearImage();
cmbIdx.Items.Clear();
Utilities.EnableControls(true, btnInit);
Utilities.EnableControls(false, btnFree, btnClose, btnEnroll, btnVerify, btnIdentify);
DisplayMessage("Resources were successfully released from the memory !!", true);
}
else
DisplayMessage("Failed to release the resources !!", false);
}
private void ClearImage()
{
picFPImg.Image = null;
//pbxImage2.Image = null;
}
private void bnEnroll_Click(object sender, EventArgs e)
{
if (!IsRegister)
{
ClearImage();
IsRegister = true;
RegisterCount = 0;
regTempLen = 0;
Utilities.EnableControls(false, btnEnroll, btnVerify, btnIdentify);
DisplayMessage("Please press your finger " + REGISTER_FINGER_COUNT + " times to register", true);
lblFingerPrintCount.Visible = true;
lblFingerPrintCount.Text = REGISTER_FINGER_COUNT.ToString();
}
}
public object PushToDevice(object args)
{
DisplayMessage("Pushed to fingerprint !", true);
return null;
}
public void ReEnrollUser(bool enableEnroll, bool clearDeviceUser = true)
{
ClearImage();
if (clearDeviceUser && !btnInit.Enabled) ClearDeviceUser();
if (enableEnroll) btnEnroll.Enabled = true;
}
public void ClearDeviceUser()
{
try
{
int deleteCode = fpInstance.DelRegTemplate(iFid); // <---- REMOVE FINGERPRINT
if (deleteCode != zkfp.ZKFP_ERR_OK)
{
DisplayMessage(MessageManager.msg_FP_UnableToDeleteFingerPrint + iFid, false);
}
iFid = 1;
}
catch { }
}
public bool ReleaseResources()
{
try
{
ReEnrollUser(true, true);
bnClose_Click(null, null);
return true;
}
catch
{
return false;
}
}
#region -------- CONNECT/DISCONNECT DEVICE --------
/// <summary>
/// DISCONNECT DEVICE
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void bnClose_Click(object sender, EventArgs e)
{
OnDisconnect();
}
public void OnDisconnect()
{
bIsTimeToDie = true;
RegisterCount = 0;
DisconnectFingerPrintCounter();
ClearImage();
Thread.Sleep(1000);
int result = fpInstance.CloseDevice();
captureThread.Abort();
if (result == zkfp.ZKFP_ERR_OK)
{
Utilities.EnableControls(false, btnInit, btnClose, btnEnroll, btnVerify, btnIdentify);
lblDeviceStatus.Text = Disconnected;
Thread.Sleep(1000);
result = fpInstance.Finalize(); // CLEAR RESOURCES
if (result == zkfp.ZKFP_ERR_OK)
{
regTempLen = 0;
IsRegister = false;
cmbIdx.Items.Clear();
Utilities.EnableControls(true, btnInit);
Utilities.EnableControls(false, btnClose, btnEnroll, btnVerify, btnIdentify);
ReInitializeInstance();
DisplayMessage(MessageManager.msg_FP_Disconnected, true);
}
else
DisplayMessage(MessageManager.msg_FP_FailedToReleaseResources, false);
}
else
{
string errorMessage = FingerPrintDeviceUtilities.DisplayDeviceErrorByCode(result);
DisplayMessage(errorMessage, false);
}
}
#endregion
#region ------- COMMON --------
private void FingerPrintControl_Load(object sender, EventArgs e) { FormHandle = this.Handle; }
private void ReInitializeInstance()
{
Utilities.EnableControls(true, btnInit);
Utilities.EnableControls(false, btnClose, btnEnroll, btnVerify, btnIdentify);
DisconnectFingerPrintCounter();
bIdentify = true;
btnVerify.Text = VerifyButtonDefault;
}
private void DisconnectFingerPrintCounter()
{
lblFingerPrintCount.Text = REGISTER_FINGER_COUNT.ToString();
lblFingerPrintCount.Visible = false;
}
#endregion
#region -------- UTILITIES --------
/// <summary>
/// Combines Three Pre-Registered Fingerprint Templates as One Registered Fingerprint Template
/// </summary>
/// <returns></returns>
private int GenerateRegisteredFingerPrint()
{
return fpInstance.GenerateRegTemplate(RegTmps[0], RegTmps[1], RegTmps[2], RegTmp, ref regTempLen);
}
/// <summary>
/// Add A Registered Fingerprint Template To Memory | params: (FingerPrint ID, Registered Template)
/// </summary>
/// <returns></returns>
private int AddTemplateToMemory()
{
return fpInstance.AddRegTemplate(iFid, RegTmp);
}
private void DisplayFingerPrintImage()
{
// NORMAL METHOD >>>
//Bitmap fingerPrintImage = Utilities.GetImage(FPBuffer, fpInstance.imageWidth, fpInstance.imageHeight);
//Rectangle cropRect = new Rectangle(0, 0, pbxImage2.Width / 2, pbxImage2.Height / 2);
//Bitmap target = new Bitmap(cropRect.Width, cropRect.Height);
//using (Graphics g = Graphics.FromImage(target))
//{
// g.DrawImage(fingerPrintImage, new Rectangle(0, 0, target.Width, target.Height), cropRect, GraphicsUnit.Pixel);
//}
//this.pbxImage2.Image = target;
// OPTIMIZED METHOD
MemoryStream ms = new MemoryStream();
BitmapFormat.GetBitmap(FPBuffer, mfpWidth, mfpHeight, ref ms);
Bitmap bmp = new Bitmap(ms);
this.picFPImg.Image = bmp;
}
private void DisplayMessage(string message, bool normalMessage)
{
try
{
Utilities.ShowStatusBar(message, parentForm.statusBar, normalMessage);
}
catch (Exception ex)
{
Utilities.ShowStatusBar(ex.Message, parentForm.statusBar, false);
}
}
#endregion
}
}
You can’t perform that action at this time.