HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcsharpModerate

How to match a fingerprint and retrieve data from the database? In simpler way

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thematchwaysimplerfingerprintdatabaseretrievehowandfrom

Problem

I am using Digital Persona 4500 Fingerprint Scanner for my project. I used U.are.U SDK 2.3 for Windows as the SDK and I am able to insert the serialized fingerprint minutiae data to database using this code:

```
void reader_On_Captured(CaptureResult capResult)
{
try
{
if (!_sender.CheckCaptureResult(capResult)) return;

fingerindex++;
DataResult fmd = FeatureExtraction.CreateFmdFromFid(capResult.Data, Constants.Formats.Fmd.ANSI);

SendMessage(Action.SendMessage, "A fingerprint was captured. \r\nCount: " + fingerindex);
// Get view bytes to create bitmap.
foreach (Fid.Fiv fiv in capResult.Data.Views)
{
//Ask user to press finger to get fingerprint
pbFingerprint1.Image = _sender.CreateBitmap(fiv.RawImage, fiv.Width, fiv.Height);
SendMessage(Action.SendMessage, "Now place the same finger on the reader.");
}
preEnrollmentFmd.Add(fmd.Data);

if (fingerindex >= 4)
{
enrollmentFmd = Enrollment.CreateEnrollmentFmd(Constants.Formats.Fmd.ANSI, preEnrollmentFmd);

if (enrollmentFmd.ResultCode == Constants.ResultCode.DP_SUCCESS)
{
SendMessage(Action.SendMessage, "An enrollment fmd has successfully created.");
SendMessage(Action.SendMessage, "Click save to save your data..");

fmd1 = enrollmentFmd;

string empID = txtEmpID.Text.ToString();
Biometrics.updateFMDUserIDList(fmd1.Data, empID);

if (!CheckIfUserExists())
{
passCapturedFinger(Fmd.SerializeXml(fmd1.Data));
SaveEnrolledFmd(Convert.ToInt32(txtEmpID.Text), Fmd.SerializeXml(fmd1.Data));
}
fingerindex = 0;
return;

Solution

You only need three switch blocks here, with 11 cases:

case 1:
    if (compareResult.Score == 0 || compareResult.Score <= 100)
    {
        passCapturedFinger(countOf.ToString());
        selectFingerXml(Convert.ToInt32(txtXml.Text));
        if (txtString.Text != null)
        {
            fmd1 = Fmd.DeserializeXml(txtString.Text);
        }
        CompareResult compare = Comparison.Compare(anyFinger, 0, fmd1, 0);
        if (compare.ResultCode != Constants.ResultCode.DP_SUCCESS)
        {
            _sender.Reset = true;
            throw new Exception(compare.ResultCode.ToString());
        }
        else
        {
            selectFingerCredentials(Convert.ToInt32(txtXml.Text));
            SendMessage(Action.SendMessage, "Fingerprint Matched on the count of " + countOf);
        }
    }
    break;
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
    if (compareResult.Score == 0 || compareResult.Score <= 100)
    {
        passCapturedFinger(countOf.ToString());
        selectFingerXml(Convert.ToInt32(txtXml.Text));
        fmd1 = Fmd.DeserializeXml(txtString.Text);
        CompareResult compare = Comparison.Compare(anyFinger, 0, fmd1, 0);
        if (compare.ResultCode != Constants.ResultCode.DP_SUCCESS)
        {
            _sender.Reset = true;
            throw new Exception(compare.ResultCode.ToString());
        }
        else
        {
            selectFingerCredentials(Convert.ToInt32(txtXml.Text));
            SendMessage(Action.SendMessage, "Fingerprint Matched on the count of " + countOf);
        }
    }
    break;
default:
    _sender.Reset = true;
    throw new Exception(compareResult.ResultCode.ToString());


You're allowed to have case-fallthrough in C# if that case has no code associated with it.

Even better: the only difference between case 1 and the rest is that you check txtString.Text != null in case 1, apply that to the other cases and you can make it an if/else:

if (countOf >= 1 && countOf <= 10)
{
    // Code for case 1-10
}
else
{
    // Code for default
}


Why make it overly complex?

Besides that, why do you violate SRP so drastically? You should be breaking your code into methods with a single purpose/responsibility each, not creating one massive super-method.

Code Snippets

case 1:
    if (compareResult.Score == 0 || compareResult.Score <= 100)
    {
        passCapturedFinger(countOf.ToString());
        selectFingerXml(Convert.ToInt32(txtXml.Text));
        if (txtString.Text != null)
        {
            fmd1 = Fmd.DeserializeXml(txtString.Text);
        }
        CompareResult compare = Comparison.Compare(anyFinger, 0, fmd1, 0);
        if (compare.ResultCode != Constants.ResultCode.DP_SUCCESS)
        {
            _sender.Reset = true;
            throw new Exception(compare.ResultCode.ToString());
        }
        else
        {
            selectFingerCredentials(Convert.ToInt32(txtXml.Text));
            SendMessage(Action.SendMessage, "Fingerprint Matched on the count of " + countOf);
        }
    }
    break;
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
    if (compareResult.Score == 0 || compareResult.Score <= 100)
    {
        passCapturedFinger(countOf.ToString());
        selectFingerXml(Convert.ToInt32(txtXml.Text));
        fmd1 = Fmd.DeserializeXml(txtString.Text);
        CompareResult compare = Comparison.Compare(anyFinger, 0, fmd1, 0);
        if (compare.ResultCode != Constants.ResultCode.DP_SUCCESS)
        {
            _sender.Reset = true;
            throw new Exception(compare.ResultCode.ToString());
        }
        else
        {
            selectFingerCredentials(Convert.ToInt32(txtXml.Text));
            SendMessage(Action.SendMessage, "Fingerprint Matched on the count of " + countOf);
        }
    }
    break;
default:
    _sender.Reset = true;
    throw new Exception(compareResult.ResultCode.ToString());
if (countOf >= 1 && countOf <= 10)
{
    // Code for case 1-10
}
else
{
    // Code for default
}

Context

StackExchange Code Review Q#154394, answer score: 10

Revisions (0)

No revisions yet.