生物识别手指扫描仪从数据库C#反序列化模板

时间:2014-11-14 02:17:16

标签: c# serialization biometrics

美好的一天,我只需要一些帮助。我想知道我的代码是否正确所以请指导,如果不是。我有一个使用DPFP .NET(C#)SDK的数字个人生物识别模型U.ar.U 4500我能够将序列化模板保存到Database ms-sql,我想知道序列化的方式是否正确。如果正确,我如何反序列化并将数据与机器模板进行比较。这是我的代码。

          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 个答案:

答案 0 :(得分:0)

我遇到过这个问题,然后我已经通过以下步骤修复了它:

示例: pgAdmin的:

表名:

-tbl_fptable

数据库字段:

fp_id(int) fp_data(文字)

将数据插入数据库;

import javax.swing.*;

public class CaseStudyHW {

    public static void main(String[] args) {
        int guess;
        int magic = (int) (Math.random() * 10 + 1);
        int attempt1 = 1;
        int attempt2 = 1;
        int attempt3 = 1;
        int attempt4 = 1;
        int attempt5 = 1;
        int players;

        players = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter Number of Players. \n2-5 Only"));
        if (players == 2) {
            do {
                guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Player 1, Enter A Number from 1-10", "Guessing Game", 1));
                if (guess == magic) {
                    JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!"
                            + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt1 + " attempt(s)", "Guessing Game", 1);
                } else {
                    attempt1++;
                    if (guess > magic) {
                        JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!", "Guessing Game", 0);
                    } else {
                        JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!", "Guessing Game", 0);
                    }
                }
            } while (guess != magic);

            do {
                guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Player 2, Enter A Number from 1-10", "Guessing Game", 1));

                if (guess == magic) {
                    JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!"
                            + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt2 + " attempt(s)", "Guessing Game", 1);
                } else {
                    attempt2++;
                    if (guess > magic) {
                        JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!", "Guessing Game", 0
                        );
                    } else {
                        JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!", "Guessing Game", 0);
                    }
                }
            } while (guess != magic);
            if (attempt1 > attempt2) {
                JOptionPane.showMessageDialog(null, "Player 1 Won!", "Guessing Game", 0);
            } else {
                JOptionPane.showMessageDialog(null, "Player 2 Won!", "Guessing Game", 0);
            }
        } else if (players == 3) {
            do {
                guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Player 1, Enter A Number from 1-10", "Guessing Game", 1));

                if (guess == magic) {
                    JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!"
                            + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt1 + " attempt(s)", "Guessing Game", 1);
                } else {
                    attempt1++;
                    if (guess > magic) {
                        JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!", "Guessing Game", 0                        );
                    } else {
                        JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!", "Guessing Game", 0);
                    }
                }
            } while (guess != magic);

            do {
                guess = Integer.parseInt(JOptionPane.showInputDialog(null,                        "Player 2, Enter A Number from 1-10", "Guessing Game", 1));

                if (guess == magic) {
                    JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!"
                            + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt2 + " attempt(s)", "Guessing Game", 1);
                } else {
                    attempt2++;
                    if (guess > magic) {
                        JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!", "Guessing Game", 0
                        );
                    } else {
                        JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!", "Guessing Game", 0);
                    }
                }
            } while (guess != magic);

            do {
                guess = Integer.parseInt(JOptionPane.showInputDialog(null,
                        "Player 3, Enter A Number from 1-10", "Guessing Game", 1));

                if (guess == magic) {
                    JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!"
                            + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt3 + " attempt(s)", "Guessing Game", 1);
                } else {
                    attempt3++;
                    if (guess > magic) {
                        JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!", "Guessing Game", 0
                        );
                    } else {
                        JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!", "Guessing Game", 0);
                    }
                }
            } while (guess != magic);
            if (attempt1 > attempt2) {
                JOptionPane.showMessageDialog(null, "Player 1 Won!", "Guessing Game", 0);
            } else if (attempt2 > attempt3) {
                JOptionPane.showMessageDialog(null, "Player 2 Won!", "Guessing Game", 0);
            } else if (attempt3 > attempt1) {
                JOptionPane.showMessageDialog(null, "Player 3 Won!", "Guessing Game", 0);
            } else if (players == 4) {
                do {
                    guess = Integer.parseInt(JOptionPane.showInputDialog(null,
                            "Player 1, Enter A Number from 1-10", "Guessing Game", 1));

                    if (guess == magic) {
                        JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!"
                                + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt1 + " attempt(s)", "Guessing Game", 1);
                    } else {
                        attempt1++;
                        if (guess > magic) {
                            JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!", "Guessing Game", 0
                            );
                        } else {
                            JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!", "Guessing Game", 0);
                        }
                    }
                } while (guess != magic);

                do {
                    guess = Integer.parseInt(JOptionPane.showInputDialog(null,
                            "Player 2, Enter A Number from 1-10", "Guessing Game", 1));

                    if (guess == magic) {
                        JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!"
                                + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt2 + " attempt(s)", "Guessing Game", 1);
                    } else {
                        attempt2++;
                        if (guess > magic) {
                            JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!", "Guessing Game", 0
                            );
                        } else {
                            JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!", "Guessing Game", 0);
                        }
                    }
                } while (guess != magic);

                do {
                    guess = Integer.parseInt(JOptionPane.showInputDialog(null,
                            "Player 3, Enter A Number from 1-10", "Guessing Game", 1));

                    if (guess == magic) {
                        JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!"
                                + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt3 + " attempt(s)", "Guessing Game", 1);
                    } else {
                        attempt3++;
                        if (guess > magic) {
                            JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!", "Guessing Game", 0
                            );
                        } else {
                            JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!", "Guessing Game", 0);
                        }
                    }
                } while (guess != magic);

                do {
                    guess = Integer.parseInt(JOptionPane.showInputDialog(null,
                            "Player 4, Enter A Number from 1-10", "Guessing Game", 1));

                    if (guess == magic) {
                        JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!"
                                + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt1 + " attempt(s)", "Guessing Game", 1);
                    } else {
                        attempt4++;
                        if (guess > magic) {
                            JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!", "Guessing Game", 0
                            );
                        } else {
                            JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!", "Guessing Game", 0);
                        }
                    }
                } while (guess != magic);
                if (attempt1 > attempt2) {
                    JOptionPane.showMessageDialog(null, "Player 1 Won!", "Guessing Game", 0);
                } else if (attempt2 > attempt3) {
                    JOptionPane.showMessageDialog(null, "Player 2 Won!", "Guessing Game", 0);
                } else if (attempt3 > attempt1) {
                    JOptionPane.showMessageDialog(null, "Player 3 Won!", "Guessing Game", 0);
                } else if (attempt4 > attempt3) {
                    JOptionPane.showMessageDialog(null, "Player 4 Won!", "Guessing Game", 0);
                } else if (players == 5) {
                    do {
                        guess = Integer.parseInt(JOptionPane.showInputDialog(null,
                                "Player 1, Enter A Number from 1-10", "Guessing Game", 1));

                        if (guess == magic) {
                            JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!"
                                    + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt1 + " attempt(s)", "Guessing Game", 1);
                        } else {
                            attempt1++;
                            if (guess > magic) {
                                JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!", "Guessing Game", 0
                                );
                            } else {
                                JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!", "Guessing Game", 0);
                            }
                        }
                    } while (guess != magic);

                    do {
                        guess = Integer.parseInt(JOptionPane.showInputDialog(null,
                                "Player 2, Enter A Number from 1-10", "Guessing Game", 1));

                        if (guess == magic) {
                            JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!"
                                    + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt2 + " attempt(s)", "Guessing Game", 1);
                        } else {
                            attempt2++;
                            if (guess > magic) {
                                JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!", "Guessing Game", 0
                                );
                            } else {
                                JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!", "Guessing Game", 0);
                            }
                        }
                    } while (guess != magic);

                    do {
                        guess = Integer.parseInt(JOptionPane.showInputDialog(null,
                                "Player 3, Enter A Number from 1-10", "Guessing Game", 1));

                        if (guess == magic) {
                            JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!"
                                    + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt3 + " attempt(s)", "Guessing Game", 1);
                        } else {
                            attempt3++;
                            if (guess > magic) {
                                JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!", "Guessing Game", 0
                                );
                            } else {
                                JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!", "Guessing Game", 0);
                            }
                        }
                    } while (guess != magic);

                    do {
                        guess = Integer.parseInt(JOptionPane.showInputDialog(null,
                                "Player 4, Enter A Number from 1-10", "Guessing Game", 1));

                        if (guess == magic) {
                            JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!"
                                    + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt1 + " attempt(s)", "Guessing Game", 1);
                        } else {
                            attempt4++;
                            if (guess > magic) {
                                JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!", "Guessing Game", 0
                                );
                            } else {
                                JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!", "Guessing Game", 0);
                            }
                        }
                    } while (guess != magic);

                    do {
                        guess = Integer.parseInt(JOptionPane.showInputDialog(null,
                                "Player 5, Enter A Number from 1-10", "Guessing Game", 1));

                        if (guess == magic) {
                            JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!"
                                    + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt5 + " attempt(s)", "Guessing Game", 1);
                        } else {
                            attempt4++;
                            if (guess > magic) {
                                JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!", "Guessing Game", 0
                                );
                            } else {
                                JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!", "Guessing Game", 0);
                            }
                        }
                    } while (guess != magic);
                    if (attempt1 > attempt2) {
                        JOptionPane.showMessageDialog(null, "Player 1 Won!", "Guessing Game", 0);
                    } else if (attempt2 > attempt3) {
                        JOptionPane.showMessageDialog(null, "Player 2 Won!", "Guessing Game", 0);
                    } else if (attempt3 > attempt1) {
                        JOptionPane.showMessageDialog(null, "Player 3 Won!", "Guessing Game", 0);
                    } else if (attempt4 > attempt3) {
                        JOptionPane.showMessageDialog(null, "Player 4 Won!", "Guessing Game", 0);
                    } else if (attempt5 > attempt4) {
                        JOptionPane.showMessageDialog(null, "Player 4 Won!", "Guessing Game", 0);
                    } else {
                        JOptionPane.showMessageDialog(null, "No Such Gamemode", "Guessing Game", 0);
                    }
                }
            }
        }
    }
}

从数据库获取数据到已验证

    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;
            }
        }
    }

我希望这个例子可以解决这个问题。

答案 1 :(得分:0)

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();

    }