Biometric finger scanner deserializing template from database C#

3.7k views Asked by At

good day to all I just need some help. I was wondering if my code is correct so please guide if it is not. I have a Digital Personal Biometric Model U.ar.U 4500 using DPFP .NET (C#) SDK I was able to save the serialize template to Database ms-sql and I was wondering if the way a serialize is correct. If it is correct how do I deserialize and compare the data to the machine template. here is my code.

          public void EnrollmentControl_OnEnroll(Object Control, int Finger, DPFP.Template       
              Template, ref DPFP.Gui.EventHandlerStatus Status) {
          if (Data.IsEventHandlerSucceeds) {
               byte[] by = new byte[0];
               foreach (DPFP.Template template in Data.Templates)
               {
                    if (template != null) 
               {
                  by = new byte[template.Bytes.Length];
                       template.Serialize(ref by);
                       string PP;
                       string CR;
              PP = iSQL.STORED_PROCEDURE_FNG_IMG; // <- Stored Procedure INSERT INTO Employee_Img
              CR = "AB-0001-R12";
              iEmp emp = new iEmp(PP);                          // <- Class that insert to MS SQL 
              int rt = emp.Insert_Finger(CR, template.Bytes);   // <- return 1 if Successfully 
              if (rt == 1) { iCon.success_mgs(1, "Successfully Inserted", iSQL.EMPLOYEE_REGISTRN); } //<- Popup message
              else { iCon.success_mgs(1, "Successfully Inserted", iSQL.EMPLOYEE_REGISTRN); } 
            }
         }
          Data.Templates[Finger - 1] = Template;            // store a finger template
          ExchangeData(true);                               // update other data
          ListEvents.Items.Insert(0, String.Format("OnEnroll: finger {0}", Finger));
     }else
        Status = DPFP.Gui.EventHandlerStatus.Failure;   // force a "failure" status
   }  
2

There are 2 answers

1
user1417068 On BEST ANSWER

I have faced with this problem, then I have fixed it already by following steps:

Example: pgAdmin:

Table Name:

-tbl_fptable

DB Fields:

fp_id (int) fp_data (text)

Insert Data to Database;

    protected override void Process(DPFP.Sample Sample)
    {
        base.Process(Sample);

        // Process the sample and create a feature set for the enrollment purpose.
        DPFP.FeatureSet features = ExtractFeatures(Sample, DPFP.Processing.DataPurpose.Enrollment);

        // Check quality of the sample and add to enroller if it's good
        if (features != null) try
        {
            MakeReport("The fingerprint feature set was created.");
            Enroller.AddFeatures(features);     // Add feature set to template.
        }
        finally {
            UpdateStatus();

                // Check if template has been created.

                switch (Enroller.TemplateStatus)
            {

                case DPFP.Processing.Enrollment.Status.Ready:
                        // report success and stop capturing

                    byte[] serializedTemplate = null;
                    string str_temp = null;
                    DateTime cur_date = DateTime.Now;
                    Enroller.Template.Serialize(ref serializedTemplate);
                    //Enroller.Template.Serialize(ref str_temp);

                        if (serializedTemplate != null)
                    {
                            string result = System.Text.Encoding.UTF8.GetString(serializedTemplate);
                            Console.Write(result);

                            try
                        {
                                using (NpgsqlConnection conn = new NpgsqlConnection("Host=127.0.0.1;Port=5432;User Id=UserName;Password=*****;Database=finger_print_db;"))
                                {
                                    conn.Open();


                                    NpgsqlCommand dbcmd = conn.CreateCommand();
                                    try
                                    {
                                        // Store TemplateSerialize as string data

                                        dbcmd.CommandText= "INSERT INTO tbl_fptable( fp_data ) VALUES ( @serializedTemplate )";
dbcmd.Parameters.AddWithValue("@serializedTemplate ", serializedTemplate);                                                                                                   
                                        dbcmd.ExecuteNonQuery();
                                    }
                                    finally
                                    {
                                        conn.Close();
                                    }
                                }

                            }
                        catch (Exception ex)
                        {
                            throw ex;
                        }

                    }

                    OnTemplate(Enroller.Template);
                    SetPrompt("Click Close, and then click Fingerprint Verification.");
                    Stop();
                    break;

                case DPFP.Processing.Enrollment.Status.Failed:  // report failure and restart capturing
                    Enroller.Clear();
                    Stop();
                    UpdateStatus();
                    OnTemplate(null);
                    Start();
                    break;
            }
        }
    }

Get Data from Database to verified

protected override void Process(DPFP.Sample Sample)
    {

        base.Process(Sample);

        // Process the sample and create a feature set for the enrollment purpose.
        DPFP.FeatureSet features = ExtractFeatures(Sample, DPFP.Processing.DataPurpose.Verification);

        // Check quality of the sample and start verification if it's good
        // TODO: move to a separate task
        if (features != null)
        {

            try
            {
                using (NpgsqlConnection conn = new NpgsqlConnection("Host=127.0.0.1;Port=5432;User Id=UserName;Password=*****;Database=finger_print_db;"))
                {
                    conn.Open();
                    try
                    {
                        NpgsqlCommand cmd = new NpgsqlCommand("SELECT * FROM tbl_fptable ORDER BY enr_id DESC LIMIT 1", conn);
                        NpgsqlDataReader dr = cmd.ExecuteReader();
                        while ((dr.Read()))
                        {

                            long fpid = Convert.ToInt64(dr["id"]);
                            byte[] fpbyte = (byte[])dr["finger_data"];
                            Stream stream = new MemoryStream(fpbyte );
                            DPFP.Template tmpObj = new DPFP.Template(stream);


                            DPFP.Verification.Verification.Result result = new DPFP.Verification.Verification.Result();
                            // Compare the feature set with our template     
                            Verificator.Verify(features, tmpObj, ref result);
                            UpdateStatus(result.FARAchieved);
                            if (result.Verified)
                                MakeReport("The fingerprint was VERIFIED.");
                            else
                                MakeReport("The fingerprint was NOT VERIFIED.");
                        }
                    }
                    finally
                    {
                        conn.Close();
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

I hope this example can solve this stuck.

0
Lucky Boy Cordero Tubang On
public class VerificationForm : CaptureForm
{
    public void Verify(DPFP.Template template)
    {
        Template = template;

        ShowDialog();
    }

    protected override void Init()
    {
        base.Init();
        base.Text = "Fingerprint Verification";
        Verificator = new DPFP.Verification.Verification();     // Create a fingerprint template verificator
        UpdateStatus(0);
    }
    bool found = false; string fname = "";
    protected override void Process(DPFP.Sample Sample)
    {
        base.Process(Sample);

        // Process the sample and create a feature set for the enrollment purpose.
        DPFP.FeatureSet features = ExtractFeatures(Sample, DPFP.Processing.DataPurpose.Verification);

        // Check quality of the sample and start verification if it's good
        // TODO: move to a separate task
        if (features != null)
        {
            // Compare the feature set with our template
            DPFP.Verification.Verification.Result result = new DPFP.Verification.Verification.Result();

            string[] flist = Directory.GetFiles("c:/finger");
            int mx = flist.Length;
            int cnt = 0;
            bool found = false;

             DPFP.Template templates;
            while (cnt < mx && !found)
            {
               // MessageBox.Show(flist[cnt]);
                using (FileStream fs = File.OpenRead(flist[cnt]))
                {
                     templates = new DPFP.Template(fs);
                    Verificator.Verify(features, templates, ref result);
                    UpdateStatus(result.FARAchieved);
                }
                if (result.Verified)
                {
                    found = true;
                    FileInfo nfo = new FileInfo(flist[cnt]);
                    fname = nfo.Name;                        
                    break;
                }
                else
                {
                    found = false;
                    cnt++;
                }
            }


                if (found)
                {
                    MakeReport("The fingerprint was VERIFIED. "+fname);
                    MessageBox.Show("Verified!!");



                }
                else
                {
                    MakeReport("The fingerprint was NOT VERIFIED.");
                    MessageBox.Show("Not Verified!!");

                }

        }
    }

    private void UpdateStatus(int FAR)
    {
        // Show "False accept rate" value
        SetStatus(String.Format("False Accept Rate (FAR) = {0}", FAR));
    }

    private DPFP.Template Template;
    private DPFP.Verification.Verification Verificator;
    private void InitializeComponent()
    {
        this.SuspendLayout();
        // 
        // VerificationForm
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.ClientSize = new System.Drawing.Size(693, 373);
        this.Name = "VerificationForm";
        this.Load += new System.EventHandler(this.VerificationForm_Load);
        this.ResumeLayout(false);
        this.PerformLayout();

    }