HIP: FingerPrint Scanner ด้วย VB 6.0 PDF Print E-mail

w

 

การเขียนโปรแกรม VB 6.0 ติดต่อ HIP: FingerPrint Scanner รุ่น CM 5000 

ขั้นตอนแรกให้เราทำการ design Form ตามรูปด้ารนล่างนี้ครับ ....

w

จากนั้นให้เราทำการเขียน Code ดังนี้ ...

 

'---------------------------------------------------------------------------------------

Option Explicit
Dim FTempLen As Integer
Dim FRegTemplate As String
Dim FingerCount As Long
Dim FFingerNames() As String
Dim FMatchType As Integer
Dim ImgPath As String
Private Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long
Private Sub cmdEnroll_Click()
  Dim strsql1 As String
  Dim maxNum As Long
 
  Set recFinger = New ADODB.Recordset
  If TextFingerName.Text = "" Then
     MessageBox 0, "Please enter ID", "Error", 0
     Exit Sub
  End If
  ZKFPEngX1.BeginEnroll
 
  StatusBar.Caption = "Begin Register"
  strsql1 = "select max(ID) as maxnum from template "
  'conn.Execute strsql1, recFinger, -1
  recFinger.Open strsql1, conn, adOpenKeyset, adLockOptimistic
  If recFinger.Fields( "maxnum") <> 0 Then
      txtuserID.Text = CStr(recFinger.Fields( "maxnum") + 1)
      recFinger.Close
  Else
      txtuserID.Text = "1"
  End If
End Sub

Private Sub cmdIdentify_Click()
    If ZKFPEngX1.IsRegister Then
        ZKFPEngX1.CancelEnroll
    End If
    StatusBar.Caption = "Start Identification(1:N)"
    FMatchType = 2
End Sub

Private Sub cmdInit_Click()
  ZKFPEngX1.SensorIndex = 0
  If ZKFPEngX1.InitEngine = 0 Then
     MessageBox 0, "init success", "Hint", 0
    
     StatusBar.Caption = "Sensor Connected"
     TextSensorCount.Text = ZKFPEngX1.SensorCount & ""
     TextSensorIndex.Text = ZKFPEngX1.SensorIndex & ""
     TextSensorSN.Text = ZKFPEngX1.SensorSN
    
     cmdInit.Enabled = False
     FMatchType = 0
  End If
End Sub

Private Sub cmdLoad_Click()
    Dim rectemp As ADODB.Recordset
    Dim sqlstrLoad As String
    Dim i As Long
    'Dim tempFp(2048)
   
    Set rectemp = New ADODB.Recordset
    sqlstrLoad = "select id,FingerTmplate from template"
   
    rectemp.CursorLocation = adUseServer
    rectemp.Open sqlstrLoad, conn 'adOpenKeyset, adLockOptimistic
   
    'conn.Execute sqlstrLoad, rectemp, -1
    'rectemp.MoveFirst
   
    'For i = 1 To rectemp.RecordCount *** this sentence have some error for mysql,I think Microsoftware could modify they tool.haha.
        Do While Not rectemp.EOF
            ZKFPEngX1.AddRegTemplateStrToFPCacheDB FpcHandle, rectemp( "ID"), rectemp("FingerTmplate")
            rectemp.MoveNext
        Loop
       
        'of course you can use binary system to store finger template.
        'Do While Not rectemp.EOF
            'tempFp(0) = rectemp( "Template")
            'ZKFPEngX1.AddRegTemplateToFPCacheDB FpcHandle, rectemp( "ID"), tempFp(0)
            'rectemp.MoveNext
        'Loop
       
'    Next
End Sub

Private Sub cmdReadMemory_Click()
  Dim i, p1 As Long, p2 As Long
  Dim p As Variant
  Dim s As String
 
  p1 = 0
  p2 = 23
  ZKFPEngX1.DongleMemRead p1, p2, p
 
  s = ""
  For i = LBound(p) To UBound(p)
    's = s & Chr(p(i))
  Next
 
  TextRead.Text = s

End Sub

Private Sub cmdSave_Click()
    Dim strsql As String
   
    strsql = "insert into template values (" & CInt(txtuserID.Text) & ",'" & sTemp & "')"
    conn.Execute strsql
    cmdSave.Enabled = False
End Sub

Private Sub cmdSaveMemory_Click()
'  Dim p1 As Long, p2 As Long
'  Dim p() As Byte
'  Dim i As Integer
'  Dim s As String
'
'  p1 = 0
'  p2 = Len(TextWrite.Text)
'  s = TextWrite.Text
'  ReDim p(Len(s))
'  For i = 0 To Len(s) - 1
'    p(i) = Asc(Mid(s, i + 1))
'  Next
'  ZKFPEngX1.DongleMemWrite p1, p2, p
End Sub

Private Sub cmdSaveImage_Click()
    Dim sFileName As String
   
    sFileName = ImgPath + TextFingerName.Text
    If OptionBmp.Value Then
        ZKFPEngX1.SaveBitmap sFileName & ".bmp"
       'ZKFPEngX1.SaveBitmap "c:cwx.bmp"
    Else
        ZKFPEngX1.SaveJPG sFileName + ".jpg"
    End If
    MessageBox 0, "File Saved(" + sFileName + ")!", "", 0
End Sub

Private Sub cmdVerify_Click()
    Dim sqlstr As String
    Dim recxx As ADODB.Recordset
   
    If txtuserID.Text = "" Then Exit Sub
    Set recxx = New ADODB.Recordset
    sqlstr = "select * from template where ID=" & CLng(txtuserID.Text) & ""
    FRegTemplate = ""
   
    If ZKFPEngX1.IsRegister Then
        ZKFPEngX1.CancelEnroll
    End If
    StatusBar.Caption = "Start Verification(1:1)"
    FMatchType = 1
    recxx.Open sqlstr, conn, adOpenKeyset, adLockOptimistic
'    If recxx.RecordCount = 0 Then Exit Sub
    FRegTemplate = recxx.Fields( "FingerTmplate")
End Sub

Private Sub Command1_Click()
    ZKFPEngX1.AddImageFile ImgPath + TextFingerName.Text + ".bmp", 500
End Sub

Private Sub Form_Load()
Dim recFP As New Recordset
Dim connFP As New Recordset
Dim strDataSourcePath As String
Dim Cuantos
    FingerCount = 0
    FpcHandle = ZKFPEngX1.CreateFPCacheDB
    cmdSave.Enabled = False
    conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "mydb.mdb;Persist Security Info=False"
    'of course ,you can connect with other DB,pls consult.
'    conn.Open "DRIVER={MySQL ODBC 3.51 Driver};Server=localhost;UID=pei;PWD=;Database=aaa"
   
'    strDataSourcePath = App.Path
'    connFP.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDataSourcePath & "mydb.mdb;Persist Security Info=False"
   
    If conn.State = 1 Then conn.Close
    DB
   
    FingerCount = 0
    FpcHandle = ZKFPEngX1.CreateFPCacheDB
   
    cors.Open "select * from template", conn, adOpenKeyset, adLockReadOnly
    cors.MoveFirst
    While Not cors.EOF
         Cuantos = cors.Fields(1)
'         ZKFPEngX1.AddRegTemplateToFPCacheDB FpcHandle, FingerCount, Cuantos
        ZKFPEngX1.AddRegTemplateStrToFPCacheDB FpcHandle, FingerCount, Cuantos
        ReDim Preserve FFingerNames(FingerCount + 1)
        FFingerNames(FingerCount) = cors.Fields( "FingerTmplate")
        FingerCount = FingerCount + 1
        cors.MoveNext
   
    Wend
    cors.Close
   
    ImgPath = "c:finger_collect_by3.5"
End Sub

Private Sub Form_Unload(Cancel As Integer)
    ZKFPEngX1.FreeFPCacheDB (FpcHandle)
End Sub

Private Sub ZKFPEngX1_OnCapture(ByVal ActionResult As Boolean, ByVal ATemplate As Variant)
    Dim fi As Long, i As Long
    Dim Score As Long, ProcessNum As Long
    Dim RegChanged As Boolean
    Dim sTemp As String
    Dim tempxx As String
   
    sTemp = ZKFPEngX1.GetTemplateAsString()
    tempxx = ZKFPEngX1.GetTemplateAsString()
   
    StatusBar.Caption = "Acqired Template"
    If FMatchType = 1 Then  '1:1
       'If ZKFPEngX1.VerFinger(FRegTemplate, ATemplate, False, RegChanged) Then
        If ZKFPEngX1.VerFingerFromStr(FRegTemplate, tempxx, False, False) Then
          MessageBox 0, "Verify success", "information", 0
       Else
          MessageBox 0, "Verify Failed", "information", 0
       End If
    ElseIf FMatchType = 2 Then '1:N
       Score = 12
       fi = ZKFPEngX1.IdentificationFromStrInFPCacheDB(FpcHandle, tempxx, Score, ProcessNum)
       If fi = -1 Then
          MessageBox 0, "Identification failed£¡", "information", 0
       Else
            MsgBox "Identify Success = " & FFingerNames(fi), vbInformation + vbOKOnly, "Success"
'          MessageBox 0, "Identification Success Name= " & FFingerNames(fi) & " Score = " & Score & " Processed Number = " & ProcessNum & "", "information", 0
'          MsgBox "successful"
       End If
    End If
End Sub

Private Sub ZKFPEngX1_OnEnroll(ByVal ActionResult As Boolean, ByVal ATemplate As Variant)
  Dim i As Long
  Dim ID As Integer
 
  If Not ActionResult Then
      MessageBox 0, "Register failed", "Warning", 0
  Else
      MessageBox 0, "Regsiter success", "Information", 0
   
    sTemp = ZKFPEngX1.GetTemplateAsString()
    cmdSave.Enabled = True
   
    If conn.State = 1 Then conn.Close
   
    DB
    cors.Open "SELECT * FROM template", conn, adOpenKeyset, adLockPessimistic
        ID = cors.RecordCount + 1
        cors.AddNew
        cors.Fields(0) = ID
        cors.Fields(1) = sTemp
        cors.Update
    conn.Close
'    ZKFPEngX1.AddRegTemplateStrToFPCacheDB fpcHandle, FingerCount, sTemp
'        ReDim Preserve FFingerNames(FingerCount + 1)
    'FFingerNames(FingerCount) = TextFingerName.Text
    FingerCount = FingerCount + 1
  End If
End Sub

Private Sub ZKFPEngX1_OnFeatureInfo(ByVal AQuality As Long)
  Dim sTemp As String
 
  sTemp = ""
  If ZKFPEngX1.IsRegister Then
     sTemp = "Register status: still press finger " & ZKFPEngX1.EnrollIndex & " times"
  End If
  sTemp = sTemp & " Fingerprint quality"
  If AQuality <> 0 Then
     sTemp = sTemp & " no good " & AQuality
  Else
     sTemp = sTemp & " good"
  End If
  StatusBar.Caption = sTemp
End Sub

Private Sub ZKFPEngX1_OnImageReceived(AImageValid As Boolean)
  ZKFPEngX1.PrintImageAt hDC, FrameCommands.Width + 6, FrameCommands.Top, ZKFPEngX1.ImageWidth, ZKFPEngX1.ImageHeight
End Sub

'---------------------------------------------------------------------------------------------

 

w  Download SourceCode