Search code examples
vb.netbackgroundfingerprintreader

vb.net app does not listen Fingerprint reader when is inactive (not in focus)


I made an application in visual basic using the DigitalPersona SDK OneTouch for .NET that compares the fingerprint scanned in a fingerprint reader "U.are.U 4500" with the fingerprints stored in a folder. The app works fine as long as it is the active window (among all the open apps) and the thing is I need it to always listen to the reader even when it is minimized or is not the active app. I've tried using a backgroundworker, setting the form property TopMost True, making a service and even setting the form active when the event deactivate is called but none of this alternatives have worked. I would appreciate any help and I thank you in advance for your suggestions.

Here is the code of my vb.net app.

Public Class CaptureForm
    Implements DPFP.Capture.EventHandler

    Private Template As DPFP.Template
    Private Verificator As DPFP.Verification.Verification
    Private Capturer As DPFP.Capture.Capture

    Private Sub CaptureForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If BackgroundWorker1.IsBusy <> True Then
            BackgroundWorker1.RunWorkerAsync()
        End If
    End Sub

    Private Sub CaptureForm_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        StopCapture()
    End Sub

    'Private Sub CaptureForm_Deactivate(ByVal sender As System.Object, ByVal e As EventArgs) Handles MyBase.Deactivate
    '    Me.Activate()
    'End Sub
    'Private Sub CaptureForm_Activate(ByVal sender As System.Object, ByVal e As EventArgs) Handles MyBase.Activated

    'End Sub

    Sub Init()
        Try
            Capturer = New DPFP.Capture.Capture()
            If (Not Capturer Is Nothing) Then
                Capturer.EventHandler = Me
            End If
        Catch ex As Exception
            MessageBox.Show("No se pudo iniciar la operación de captura!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        Verificator = New DPFP.Verification.Verification()
        StartCapture()
    End Sub

    Protected Overridable Sub Process(ByVal Sample As DPFP.Sample)

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

        ' Check quality of the sample and start verification if it's good
        If Not features Is Nothing Then
            Dim result As DPFP.Verification.Verification.Result = New DPFP.Verification.Verification.Result()

            Dim foundFile As String

            Dim fileReader As String
            fileReader = My.Computer.FileSystem.ReadAllText("templates.txt")
            Dim flag As Boolean

            flag = False

            For Each foundFile In My.Computer.FileSystem.GetFiles(fileReader, FileIO.SearchOption.SearchTopLevelOnly, "*.fpt")
                Using fs As IO.FileStream = IO.File.OpenRead(foundFile)
                    Dim template1 As New DPFP.Template(fs)
                    Me.Template = template1
                End Using
                Verificator.Verify(features, Template, result)
                If result.Verified Then

                    flag = True
                    Dim file As System.IO.StreamWriter
                    Dim file2 As String
                    Dim nombre As String = My.Computer.FileSystem.GetName(foundFile)
                    file2 = nombre.Remove(nombre.Length - 4, 4)
                    'MsgBox(file2)
                    file = My.Computer.FileSystem.OpenTextFileWriter("ingreso.txt", False)
                    file.WriteLine(file2)
                    file.Close()
                    Exit For
                End If
            Next
            If flag = False Then

            End If
        End If
    End Sub
    Protected Function ExtractFeatures(ByVal Sample As DPFP.Sample, ByVal Purpose As DPFP.Processing.DataPurpose) As DPFP.FeatureSet

        Dim extractor As New DPFP.Processing.FeatureExtraction()    ' Create a feature extractor
        Dim feedback As DPFP.Capture.CaptureFeedback = DPFP.Capture.CaptureFeedback.None
        Dim features As New DPFP.FeatureSet()
        extractor.CreateFeatureSet(Sample, Purpose, feedback, features) ' TODO: return features as a result?
        If (feedback = DPFP.Capture.CaptureFeedback.Good) Then
            Return features
        Else
            Return Nothing
        End If

    End Function

    Protected Sub StartCapture()
        If (Not Capturer Is Nothing) Then
            Try
                Capturer.StartCapture()
            Catch ex As Exception

            End Try
        End If
    End Sub

    Protected Sub StopCapture()

        If (Not Capturer Is Nothing) Then
            Try
                Capturer.StopCapture()
            Catch ex As Exception

            End Try
        End If
    End Sub

    Sub OnComplete(ByVal Capture As Object, ByVal ReaderSerialNumber As String, ByVal Sample As DPFP.Sample) Implements DPFP.Capture.EventHandler.OnComplete
        Process(Sample)
    End Sub

    Sub OnFingerGone(ByVal Capture As Object, ByVal ReaderSerialNumber As String) Implements DPFP.Capture.EventHandler.OnFingerGone

    End Sub

    Sub OnFingerTouch(ByVal Capture As Object, ByVal ReaderSerialNumber As String) Implements DPFP.Capture.EventHandler.OnFingerTouch

    End Sub

    Sub OnReaderConnect(ByVal Capture As Object, ByVal ReaderSerialNumber As String) Implements DPFP.Capture.EventHandler.OnReaderConnect

    End Sub

    Sub OnReaderDisconnect(ByVal Capture As Object, ByVal ReaderSerialNumber As String) Implements DPFP.Capture.EventHandler.OnReaderDisconnect

    End Sub

    Sub OnSampleQuality(ByVal Capture As Object, ByVal ReaderSerialNumber As String, ByVal CaptureFeedback As DPFP.Capture.CaptureFeedback) Implements DPFP.Capture.EventHandler.OnSampleQuality

    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Init()
    End Sub
End Class

Solution

  • you done the hard part, only missing this one

    Verificator = New DPFP.Verification.Verification()
    Capturer.Priority = DPFPDevXLib.DPFPCapturePriorityEnum.CapturePriorityHigh
    StartCapture()
    

    Also, the application must be running under Admin for the high priority to work. ** High priority means your app receives all events, regardles if its minimized.