祝大家美好的一天,我只需要一些帮助。我想知道我的代码是否正确,所以如果不正确,请指导。我有一个使用 DPFP .NET (C#) SDK 的数字个人生物识别模型 U.ar.U 4500,我能够将序列化模板保存到数据库 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
}
我遇到了这个问题,那么我已经按照以下步骤修复了它:
例: pg管理员:
表名:
-tbl_fptable
数据库字段:
fp_id(国际)fp_data(文本)
将数据插入数据库;
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;
}
}
}
从数据库获取数据以进行验证
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);
}
}
}
我希望这个例子可以解决这个卡住的问题。
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();
}