ZK4500指纹扫描仪C#开发:重启后无法新增指纹数据问题
问题分析
重启后新增指纹报错Enroll failed, error code=-13的核心原因是**iFid的初始化逻辑冲突**:
- 在
LoadFingerprintData中,你已经从.dat文件读取所有指纹ID,将iFid设置为最大已用ID+1,避免重复。 - 但在
bnOpen_Click方法里,调用LoadFingerprintData后又执行了iFid = 1;,直接覆盖了正确的ID计数器,导致重启后新增指纹时用重复的ID(1)向数据库添加,触发ID重复错误(错误码-13对应ZKFP_ERR_DB_EXIST)。
修复方案
1. 移除bnOpen_Click中的iFid重置语句
删除bnOpen_Click里的iFid = 1;,保留LoadFingerprintData对iFid的设置。
2. 优化LoadFingerprintData中的iFid更新逻辑
原逻辑每次读取一个指纹就把iFid设为当前fid+1,如果.dat文件中的指纹ID不是按递增顺序存储,会导致iFid取到的不是最大ID+1。改为取所有已读ID中的最大值加1:
3. 完整修改后的关键代码
修改后的LoadFingerprintData方法
private void LoadFingerprintData() { if (File.Exists(FINGERPRINT_DATA_FILE)) { try { using (FileStream fs = new FileStream(FINGERPRINT_DATA_FILE, FileMode.Open)) { using (BinaryReader reader = new BinaryReader(fs)) { // 初始化iFid为1,确保无指纹数据时也有初始值 iFid = 1; while (fs.Position < fs.Length) { int fid = reader.ReadInt32(); // Read the fingerprint ID int templateLength = reader.ReadInt32(); // Read the length of the template byte[] template = reader.ReadBytes(templateLength); // Read the template // Add the loaded fingerprint template to the database int ret = zkfp2.DBAdd(mDBHandle, fid, template); if (ret == zkfp.ZKFP_ERR_OK) { textRes.Text = $"Fingerprint with ID {fid} loaded successfully."; } else { textRes.Text = $"Failed to add fingerprint with ID {fid}. Error: {ret}"; } // Update iFid to be the maximum existing ID + 1 iFid = Math.Max(iFid, fid + 1); } } } } catch (Exception ex) { textRes.Text = "Error loading fingerprint data: " + ex.Message; } } else { textRes.Text = "No saved fingerprint data found."; // 无数据时初始化iFid为1 iFid = 1; } }
修改后的bnOpen_Click方法(移除iFid=1)
private void bnOpen_Click(object sender, EventArgs e) { int ret = zkfp.ZKFP_ERR_OK; if (IntPtr.Zero == (mDevHandle = zkfp2.OpenDevice(cmbIdx.SelectedIndex))) { MessageBox.Show("OpenDevice fail"); return; } if (IntPtr.Zero == (mDBHandle = zkfp2.DBInit())) { MessageBox.Show("Init DB fail"); zkfp2.CloseDevice(mDevHandle); mDevHandle = IntPtr.Zero; return; } // Load fingerprint data only after the device and DB are successfully initialized LoadFingerprintData(); bnInit.Enabled = false; bnFree.Enabled = true; bnOpen.Enabled = false; bnClose.Enabled = true; bnEnroll.Enabled = true; bnVerify.Enabled = true; bnIdentify.Enabled = true; RegisterCount = 0; cbRegTmp = 0; // 移除这行:iFid = 1; for (int i = 0; i < 3; i++) { RegTmps[i] = new byte[2048]; } byte[] paramValue = new byte[4]; int size = 4; zkfp2.GetParameters(mDevHandle, 1, paramValue, ref size); zkfp2.ByteArray2Int(paramValue, ref mfpWidth); size = 4; zkfp2.GetParameters(mDevHandle, 2, paramValue, ref size); zkfp2.ByteArray2Int(paramValue, ref mfpHeight); FPBuffer = new byte[mfpWidth * mfpHeight]; captureThread = new Thread(new ThreadStart(DoCapture)); captureThread.IsBackground = true; captureThread.Start(); bIsTimeToDie = false; textRes.Text = "Device opened successfully."; }
验证说明
修复后:
- 首次启动无.dat文件时,
iFid初始化为1,可正常添加指纹,ID依次递增。 - 重启后,
LoadFingerprintData会读取所有已存指纹ID,将iFid设为最大ID+1,新增指纹时使用不重复的ID,避免DBAdd时触发ID重复错误(-13)。
内容的提问来源于stack exchange,提问作者Afandi Azmi
相关产品推荐
相关产品推荐

