0
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using libzkfpcsharp;
using System;
using System.Threading;

namespace API.Controllers
{
    [Route("api/")]
    [ApiController]
    public class HomeController : Controller
    {
        private class ZKTecoReader
        {
            public byte[] CapturedFingerprintData { get; private set; }
            private IntPtr mDevHandle = IntPtr.Zero;
            private IntPtr mDBHandle = IntPtr.Zero;
            private byte[] FPBuffer;
            private int mfpWidth = 256; // Replace with the actual width of the fingerprint image
            private int mfpHeight = 288; // Replace with the actual height of the fingerprint image
            private bool isCapturing = false;

            public bool Initialize(out int errorCode)
            {
                int ret = zkfp2.Init();
                errorCode = ret;
                return ret == zkfperrdef.ZKFP_ERR_OK;
            }

            public bool OpenDevice(int deviceIndex)
            {
                mDevHandle = zkfp2.OpenDevice(deviceIndex);
                return mDevHandle != IntPtr.Zero;
            }

            public bool InitDB()
            {
                mDBHandle = zkfp2.DBInit();
                return mDBHandle != IntPtr.Zero;
            }

            public bool StartCapture()
            {
                FPBuffer = new byte[mfpWidth * mfpHeight];
                isCapturing = true;
                Thread captureThread = new Thread(DoCapture);
                captureThread.IsBackground = true;
                captureThread.Start();
                return true;
            }

            public void StopCapture()
            {
                isCapturing = false;
            }

            private void DoCapture()
            {
                try
                {
                    while (isCapturing && CapturedFingerprintData == null)
                    {
                        int cbCapTmp = 2048;
                        byte[] CapTmp = new byte[cbCapTmp];
                        int ret = zkfp2.AcquireFingerprint(mDevHandle, FPBuffer, CapTmp, ref cbCapTmp);
                        if (ret == zkfp.ZKFP_ERR_OK)
                        {
                            CapturedFingerprintData = CapTmp;
                            // Handle captured fingerprint data
                            // You can raise an event, store the data, or perform any desired operations.
                        }
                        Thread.Sleep(200);
                    }

                }catch(Exception ex)
                {
                    string x = ex.Message + ex.StackTrace;
                }
               
            }

            // Add additional methods as needed for enrollment, verification, identification, etc.

            public void CloseDevice()
            {
                StopCapture();
                zkfp2.CloseDevice(mDevHandle);
                zkfp2.DBFree(mDBHandle);
                zkfp2.Terminate();
            }
        }

        private ZKTecoReader reader = new ZKTecoReader();

        [HttpGet("readfinger")]
        public JsonResult ReadFingerPrint()
        {
            int errorCode;
            bool initialized = reader.Initialize(out errorCode);
            if (!initialized)
            {
                Console.WriteLine($"Failed to initialize the SDK. Error code: {errorCode}");
                return Json($"Failed to initialize the SDK. Error code: {errorCode}");
            }

            bool deviceOpened = reader.OpenDevice(0);
            if (!deviceOpened)
            {
                Console.WriteLine("Failed to open the device.");
                return Json("Failed to open the device.");
            }
            bool dbInitialized = reader.InitDB();
            if (!dbInitialized)
            {
                Console.WriteLine("Failed to initialize the fingerprint database.");
                return Json("Failed to initialize the fingerprint database.");
            }
            bool captureStarted = reader.StartCapture();
            if (!captureStarted)
            {
                Console.WriteLine("Failed to start capturing fingerprints.");
                return Json("Failed to start capturing fingerprints.");
            }
            // Sleep for a while to capture the fingerprint
            Thread.Sleep(5000);
            // Access the captured fingerprint data
            byte[] capturedFingerprintData = reader.CapturedFingerprintData;
            string fingerprintData = Convert.ToBase64String(capturedFingerprintData);


            reader.StopCapture();
            reader.CloseDevice();
            // You can now use the fingerprintData as needed

            return Json(new { status = true, message = "Fingerprint captured successfully.", data = fingerprintData });
        }
    }
}

this part has issue:

          int ret = zkfp2.AcquireFingerprint(mDevHandle, FPBuffer, CapTmp, ref cbCapTmp);

the error is :

System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'

SAR
  • 1,765
  • 3
  • 18
  • 42
  • I'm not a big thread expert, but mDevHandle and FPBuffer are defined on a different thread than the one you are capturing from. I think that could cause problems. Does it work without the extra thread? – Palle Due Jul 11 '23 at 10:10

0 Answers0