中控指纹仪关于9.0版本的封装

版权声明:学习之路有很多引荐的地方,希望能帮到大家就可以! https://blog.csdn.net/mingzaiwang/article/details/84615372
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;
using System.IO;
using Sample;
using System.Drawing;
using libzkfpcsharp;
using System.Threading;
using System.Runtime.InteropServices;

namespace citms.Chrome.Winform
{
    public partial class Form1 : Form
    {
        private static string strBase64;
        //指纹仪属性
        IntPtr mDevHandle = IntPtr.Zero;
        IntPtr mDBHandle = IntPtr.Zero;
        IntPtr FormHandle = IntPtr.Zero;

        byte[] FPBuffer;
        bool bIsTimeToDie = false;

        private int mfpWidth = 0;
        private int mfpHeight = 0;

        byte[][] RegTmps = new byte[3][];
        byte[] RegTmp = new byte[2048];
        byte[] CapTmp = new byte[2048];

        int cbCapTmp = 2048;
        int cbRegTmp = 0;
        const int MESSAGE_CAPTURED_OK = 0x0400 + 6;

        public ChromiumWebBrowser browser;

        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

        public Form1()
        {
            InitializeComponent();
            CefSharpSettings.LegacyJavascriptBindingEnabled = true;

            this.browser = new ChromiumWebBrowser("http://192.168.7.39:8021/tl/utc/index.html") { Dock = DockStyle.Fill };
            this.browser.RegisterJsObject("sss", new Class1() { ID = 1, Name = "sss" }, null);

            this.browser.RegisterJsObject("Fingerprint_machine", ceshi123(), null);

            //string kill = "Fingerprint_machine();";
            //var ceshi=this.browser.EvaluateScriptAsync(kill);
            

            this.Controls.Add(this.browser);


            this.browser.FrameLoadEnd += Browser_FrameLoadEnd;

        }

        private void Browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
        {
            this.browser.ExecuteScriptAsync("ceshi1(Fingerprint_machine)");
        }



   

        int jinru = 2;
        public object ceshi123()
        {
            var result1 = new results();
            if (jinru != 1)
            {
                strBase64 = null;
                
                if (Fingerprint_machine_Con() == "success")
                {
                    if (Fingerprint_machine_Open() == "Open succ")
                    {
                        //获取当前时间
                        System.DateTime currentTime = new System.DateTime();
                        currentTime = System.DateTime.Now;
                        Task t = Task.Run(() => {
                            Thread.Sleep(10000);
                        });
                        t.Wait();
                        if (strBase64 != null)
                        {
                            result1.result = strBase64;
                            MessageBox.Show(result1.result);
                            strBase64 = null;
                        }
                        else
                        {
                            result1.result = "超时";
                            MessageBox.Show(result1.result);
                        }
                    }

                }    
            }
            jinru++;
            return result1;

        }

        public class results
        {
            public string result
            {
                get ;
                set ;
            }



        }


        private void button1_Click(object sender, EventArgs e)
        {
            string s = "alert('aaaaaa');";
            this.browser.ExecuteScriptAsync(s);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            strBase64 = null;
            //this.browser.ExecuteScriptAsync("alert(sss.name+';'+sss.iD)");
            if (Fingerprint_machine_Con() == "success")
            {
                if (Fingerprint_machine_Open() == "Open succ")
                {
                    //获取当前时间
                    System.DateTime currentTime = new System.DateTime();
                    currentTime = System.DateTime.Now;
                    Task t = Task.Run(() =>
                    {
                        Thread.Sleep(10000);
                    });
                    t.Wait();
                    if (strBase64 != null)
                    {
                        string ceshi = strBase64;
                        MessageBox.Show(ceshi);
                        strBase64 = null;
                    }
                    else
                    {
                        string ceshi = "超时";
                        MessageBox.Show(ceshi);
                    }
                    bnClose_Click();

                }

            }
        }





        //连接对应的指纹机
        public string Fingerprint_machine_Con()
        {
            string Result = "";

            FormHandle = this.Handle;
            int ret = zkfperrdef.ZKFP_ERR_OK;
            if ((ret = zkfp2.Init()) == zkfperrdef.ZKFP_ERR_OK)
            {
                int nCount = zkfp2.GetDeviceCount();
                if (nCount > 0)
                {
                    Result = "success";
                }
                else
                {
                    zkfp2.Terminate();
                    Result = "无法连接到指纹仪!";
                }
            }
            else
            {
                Result = "success";
            }
            return Result;
        }


        public string Fingerprint_machine_Open()
        {
            string result = "";
            mDevHandle = zkfp2.OpenDevice(0);
            cbRegTmp = 0;
            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];

            Thread captureThread = new Thread(new ThreadStart(DoCapture));
            captureThread.IsBackground = true;
            captureThread.Priority = ThreadPriority.AboveNormal;
            captureThread.Start();
            bIsTimeToDie = false;
            result = "Open succ";
            return result;
        }


        private void DoCapture()
        {
            while (!bIsTimeToDie)
            {
                cbCapTmp = 2048;
                int ret = zkfp2.AcquireFingerprint(mDevHandle, FPBuffer, CapTmp, ref cbCapTmp);
                if (ret == zkfp.ZKFP_ERR_OK)
                {
                    SendMessage(FormHandle, MESSAGE_CAPTURED_OK, IntPtr.Zero, IntPtr.Zero);
                }
                Thread.Sleep(200);
            }
        }

        protected override void DefWndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case MESSAGE_CAPTURED_OK:
                {
                    MemoryStream ms = new MemoryStream();
                    BitmapFormat.GetBitmap(FPBuffer, mfpWidth, mfpHeight, ref ms);

                        String strBase64 = zkfp2.BlobToBase64(CapTmp, cbCapTmp);
                        byte[] blob = zkfp2.Base64ToBlob(strBase64);
                        //Bitmap bmp = new Bitmap(ms);
                        string ceshi = Convert.ToBase64String(blob);
                }
                    break;

                default:
                    base.DefWndProc(ref m);
                    break;
            }
        }

        private void bnClose_Click()
        {
            bIsTimeToDie = true;
            Thread.Sleep(1000);
            zkfp2.CloseDevice(mDevHandle);
        }


    }


}


猜你喜欢

转载自blog.csdn.net/mingzaiwang/article/details/84615372