Advertisement
Guest User

Untitled

a guest
Aug 10th, 2019
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.47 KB | None | 0 0
  1. using libzkfpcsharp;
  2. using System;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Runtime.InteropServices;
  6. using System.Threading;
  7. using System.Windows.Forms;
  8.  
  9. namespace Futvalle.view.huellas
  10. {
  11.     public partial class FrmRegistrarHuella : Form
  12.     {
  13.         public static byte[] fingerprint = new byte[2048];
  14.  
  15.         IntPtr mDevHandle = IntPtr.Zero;
  16.         IntPtr mDBHandle = IntPtr.Zero;
  17.         IntPtr FormHandle = IntPtr.Zero;
  18.         bool bIsTimeToDie = false;
  19.         bool IsRegister = false;
  20.         bool bIdentify = true;
  21.         byte[] FPBuffer;
  22.         int RegisterCount = 0;
  23.         const int REGISTER_FINGER_COUNT = 3;
  24.  
  25.         byte[][] RegTmps = new byte[3][];
  26.         byte[] RegTmp = new byte[2048];
  27.         byte[] CapTmp = new byte[2048];
  28.  
  29.         int cbCapTmp = 2048;
  30.         int cbRegTmp = 0;
  31.         int iFid = 1;
  32.  
  33.         private int mfpWidth = 0;
  34.         private int mfpHeight = 0;
  35.         private int mfpDpi = 0;
  36.  
  37.         const int MESSAGE_CAPTURED_OK = 0x0400 + 6;
  38.  
  39.         [DllImport("user32.dll", EntryPoint = "SendMessageA")]
  40.         public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
  41.  
  42.         public FrmRegistrarHuella()
  43.         {
  44.             InitializeComponent();
  45.             fingerprint = new byte[2048];
  46.         }
  47.  
  48.         private void Init()
  49.         {
  50.             if (zkfp2.Init() == zkfperrdef.ZKFP_ERR_OK)
  51.             {
  52.                 int nCount = zkfp2.GetDeviceCount();
  53.  
  54.                 if (nCount > 0)
  55.                 {
  56.                     Console.WriteLine("Sistema de huellas correctamente inicializado.");
  57.                 }
  58.                 else
  59.                 {
  60.                     zkfp2.Terminate();
  61.                     MessageBox.Show("Ningún dispositivo conectado");
  62.                     Close();
  63.                 }
  64.             }
  65.             else
  66.             {
  67.                 MessageBox.Show("Inicialización fallida. Vuelve a conectar el lector y reinicia la aplicación.");
  68.                 Close();
  69.             }
  70.         }
  71.  
  72.         private void CloseDevice()
  73.         {
  74.             bIsTimeToDie = true;
  75.             RegisterCount = 0;
  76.             Thread.Sleep(1000);
  77.             zkfp2.CloseDevice(mDevHandle);
  78.             btnRegistrar.Enabled = false;
  79.         }
  80.  
  81.         private void OpenFingerprintReader()
  82.         {
  83.             int ret = zkfp.ZKFP_ERR_OK;
  84.             if (IntPtr.Zero == (mDevHandle = zkfp2.OpenDevice(0)))
  85.             {
  86.                 MessageBox.Show("Error al conectar con el lector de huellas. Vuelve a conectar el lector y reinicia la aplicación.");
  87.                 Close();
  88.                 return;
  89.             }
  90.  
  91.             if (IntPtr.Zero == (mDBHandle = zkfp2.DBInit()))
  92.             {
  93.                 MessageBox.Show("Error al iniciar la base de datos.");
  94.                 Close();
  95.                 zkfp2.CloseDevice(mDevHandle);
  96.                 mDevHandle = IntPtr.Zero;
  97.                 return;
  98.             }
  99.  
  100.             zkfp2.DBClear(mDBHandle);
  101.             btnRegistrar.Enabled = true;
  102.             RegisterCount = 0;
  103.             cbRegTmp = 0;
  104.             iFid = 1;
  105.             for (int i = 0; i < 3; i++)
  106.             {
  107.                 RegTmps[i] = new byte[2048];
  108.             }
  109.             byte[] paramValue = new byte[4];
  110.             int size = 4;
  111.             zkfp2.GetParameters(mDevHandle, 1, paramValue, ref size);
  112.             zkfp2.ByteArray2Int(paramValue, ref mfpWidth);
  113.  
  114.             size = 4;
  115.             zkfp2.GetParameters(mDevHandle, 2, paramValue, ref size);
  116.             zkfp2.ByteArray2Int(paramValue, ref mfpHeight);
  117.  
  118.             FPBuffer = new byte[mfpWidth * mfpHeight];
  119.  
  120.             size = 4;
  121.             zkfp2.GetParameters(mDevHandle, 3, paramValue, ref size);
  122.             zkfp2.ByteArray2Int(paramValue, ref mfpDpi);
  123.  
  124.             //textRes.AppendText("Parámetros del lector, ancho de la imagen:" + mfpWidth + ", alto de la imagen:" + mfpHeight + ", densidad de píxeles:" + mfpDpi + "\n");
  125.  
  126.             Thread captureThread = new Thread(new ThreadStart(DoCapture));
  127.             captureThread.IsBackground = true;
  128.             captureThread.Start();
  129.             bIsTimeToDie = false;
  130.             textRes.AppendText("Conexión exitosa\n");
  131.             Console.WriteLine("Lector de huellas reconocido");
  132.         }
  133.  
  134.         private void Free()
  135.         {
  136.             zkfp2.Terminate();
  137.             cbRegTmp = 0;
  138.             btnRegistrar.Enabled = false;
  139.         }
  140.  
  141.         private void BnIdentify_Click(object sender, EventArgs e)
  142.         {
  143.             if (!bIdentify)
  144.             {
  145.                 bIdentify = true;
  146.                 textRes.AppendText("Por favor, presiona tu dedo...\n");
  147.             }
  148.         }
  149.  
  150.         private void BnVerify_Click(object sender, EventArgs e)
  151.         {
  152.             if (bIdentify)
  153.             {
  154.                 bIdentify = false;
  155.                 textRes.AppendText("Por favor, presiona tu dedo...\n");
  156.             }
  157.         }
  158.  
  159.         private void DoCapture()
  160.         {
  161.             while (!bIsTimeToDie)
  162.             {
  163.                 cbCapTmp = 2048;
  164.                 int ret = zkfp2.AcquireFingerprint(mDevHandle, FPBuffer, CapTmp, ref cbCapTmp);
  165.                 if (ret == zkfp.ZKFP_ERR_OK)
  166.                 {
  167.                     SendMessage(FormHandle, MESSAGE_CAPTURED_OK, IntPtr.Zero, IntPtr.Zero);
  168.                     Console.WriteLine("Huella capturada, enviando mensaje...");
  169.                 }
  170.                 Thread.Sleep(200);
  171.             }
  172.         }
  173.  
  174.         protected override void DefWndProc(ref Message m)
  175.         {
  176.             switch (m.Msg)
  177.             {
  178.                 case MESSAGE_CAPTURED_OK:
  179.                     {
  180.                         MemoryStream ms = new MemoryStream();
  181.                         BitmapFormat.GetBitmap(FPBuffer, mfpWidth, mfpHeight, ref ms);
  182.                         Bitmap bmp = new Bitmap(ms);
  183.                         picFPImg.Image = bmp;
  184.  
  185.                         //textRes.AppendText("capture template data:" + strShow + "\n");
  186.  
  187.                         if (IsRegister)
  188.                         {
  189.                             int fid = 0, score = 0;
  190.                             int ret = zkfp2.DBIdentify(mDBHandle, CapTmp, ref fid, ref score);
  191.                             if (zkfp.ZKFP_ERR_OK == ret)
  192.                             {
  193.                                 textRes.AppendText("¡Esa huella ya fue registrada por el ID " + fid + "!\n");
  194.                                 return;
  195.                             }
  196.  
  197.                             if (RegisterCount > 0 && zkfp2.DBMatch(mDBHandle, CapTmp, RegTmps[RegisterCount - 1]) <= 0)
  198.                             {
  199.                                 textRes.AppendText("Por favor, presiona el mismo dedo 3 veces para realizar el registro...\n");
  200.                                 return;
  201.                             }
  202.  
  203.                             Array.Copy(CapTmp, RegTmps[RegisterCount], cbCapTmp);
  204.                             RegisterCount++;
  205.                             if (RegisterCount >= REGISTER_FINGER_COUNT)
  206.                             {
  207.                                 RegisterCount = 0;
  208.                                 if (zkfp.ZKFP_ERR_OK == (ret = zkfp2.DBMerge(mDBHandle, RegTmps[0], RegTmps[1], RegTmps[2], RegTmp, ref cbRegTmp)) &&
  209.                                        zkfp.ZKFP_ERR_OK == (ret = zkfp2.DBAdd(mDBHandle, iFid, RegTmp)))
  210.                                 {
  211.                                     iFid++;
  212.  
  213.                                     textRes.AppendText("¡Registro exitoso!\n");
  214.                                     btnGuardar.Enabled = true;
  215.                                     btnGuardar.BackColor = Color.FromArgb(67, 96, 176);
  216.                                     btnGuardar.ForeColor = Color.White;
  217.                                 }
  218.                                 else
  219.                                 {
  220.                                     textRes.AppendText("Registro fallido, código de error = " + ret + "\n");
  221.                                 }
  222.                                 IsRegister = false;
  223.                                 return;
  224.                             }
  225.                             else
  226.                             {
  227.                                 if (REGISTER_FINGER_COUNT - RegisterCount == 1)
  228.                                     textRes.AppendText("Presiona " + (REGISTER_FINGER_COUNT - RegisterCount) + " vez tu dedo...\n");
  229.                                 else
  230.                                     textRes.AppendText("Presiona " + (REGISTER_FINGER_COUNT - RegisterCount) + " veces tu dedo...\n");
  231.                             }
  232.                         }
  233.                         else
  234.                         {
  235.                             if (cbRegTmp <= 0)
  236.                             {
  237.                                 textRes.AppendText("Por favor, registra tu huella primero.\n");
  238.                                 return;
  239.                             }
  240.                             if (bIdentify)
  241.                             {
  242.                                 int fid = 0, score = 0;
  243.                                 int ret = zkfp2.DBIdentify(mDBHandle, CapTmp, ref fid, ref score);
  244.                                 if (zkfp.ZKFP_ERR_OK == ret)
  245.                                 {
  246.                                     textRes.AppendText("¡Identificación exitosa, fid= " + fid + ", puntuación = " + score + "!\n");
  247.                                     return;
  248.                                 }
  249.                                 else
  250.                                 {
  251.                                     textRes.AppendText("Identificación fallida, error = " + ret + "\n");
  252.                                     return;
  253.                                 }
  254.                             }
  255.                             else
  256.                             {
  257.                                 int ret = zkfp2.DBMatch(mDBHandle, CapTmp, RegTmp);
  258.                                 if (0 < ret)
  259.                                 {
  260.                                     textRes.AppendText("Match finger succ, score=" + ret + "!\n");
  261.                                     return;
  262.                                 }
  263.                                 else
  264.                                 {
  265.                                     textRes.AppendText("Match finger fail, ret= " + ret + "\n");
  266.                                     return;
  267.                                 }
  268.                             }
  269.                         }
  270.                     }
  271.                     break;
  272.  
  273.                 default:
  274.                     base.DefWndProc(ref m);
  275.                     break;
  276.             }
  277.         }
  278.  
  279.         private void FrmPruebaHuella_Load(object sender, EventArgs e)
  280.         {
  281.             FormHandle = Handle;
  282.  
  283.             Init();
  284.             OpenFingerprintReader();
  285.         }
  286.  
  287.         private void FrmPruebaHuella_FormClosing(object sender, FormClosingEventArgs e)
  288.         {
  289.             Console.WriteLine("Cerrando el lector de huellas.");
  290.             CloseDevice();
  291.             Free();
  292.         }
  293.  
  294.         private void BtnGuardar_Click(object sender, EventArgs e)
  295.         {
  296.             fingerprint = RegTmp;
  297.  
  298.             Close();
  299.         }
  300.  
  301.         private void BtnRegistrar_Click(object sender, EventArgs e)
  302.         {
  303.             if (!IsRegister)
  304.             {
  305.                 IsRegister = true;
  306.                 RegisterCount = 0;
  307.                 cbRegTmp = 0;
  308.                 textRes.AppendText("Por favor, presiona tu dedo 3 veces\n");
  309.             }
  310.         }
  311.     }
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement