UPEK API 代碼

 UPEK API 代碼
 
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.IO;
using QlClr;
using QlClr.Bio;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Threading;
using System;
using System.Threading;
using System.Runtime.InteropServices;
using TPFingerprint.BsAPI;
namespace WpfApplication1
{
    /// <summary>
    /// MainWindow.xaml 的??交?互??邏?輯-
    /// </summary>
    public partial class Default : Window
    {
        private bool _firstRun;
        private BioFrame _bioframe;
        private Dispatcher _dispatcher;
        private BitmapPalette _palette;
        internal ControlCenter.BioFeedback _feedback;
        // public static DependencyProperty AuthentifyEventProperty = DependencyProperty.Register("AuthentifyEvent", typeof(QlClr.Event), typeof(ControlCenter.HelpTutorial));
        //public static DependencyProperty FeedbackImageProperty = DependencyProperty.Register("FeedbackImage", typeof(ImageSource), typeof(ControlCenter.HelpTutorial));
        //public static DependencyProperty FingerColorProperty = DependencyProperty.Register("FingerColor", typeof(System.Windows.Media.Brush), typeof(ControlCenter.HelpTutorial));
        public static DependencyProperty FeedbackImageProperty = DependencyProperty.Register("FeedbackImage" , typeof( ImageSource), typeof(WpfApplication1.Default ));
        private string mProcessName;
        //public static DependencyProperty ReplayVisibleProperty = DependencyProperty.Register("ReplayVisible", typeof(bool), typeof(ControlCenter.HelpTutorial), new PropertyMetadata(true));
        public static readonly RoutedEvent LinkActivatedEvent = EventManager.RegisterRoutedEvent("LinkActivated" , RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler <string>), typeof(ControlCenter.Instructions ));
        ControlCenter. DataSource ds;
        public Default()
        {
            InitializeComponent();
            // base.AddHandler(ControlCenter.Instructions.LinkActivatedEvent, new RoutedPropertyChangedEventHandler<string>(this.InstructionsActivated));
            this._dispatcher = Dispatcher .CurrentDispatcher;
            this._firstRun = true ;
            if (this ._firstRun)
            {
                ControlCenter. BioFeedback bb = new ControlCenter.BioFeedback();
                this._feedback = bb;
                List<System.Windows.Media.Color > colors = new List<System.Windows.Media.Color > {
                    Colors.Transparent,
                    ControlCenter. FingerColors.GetRidgesColor(this ._feedback)
                };
                for (int i = 0; i < 0xfe; i++)
                {
                    colors.Add( Colors.Transparent);
                }
                this._palette = new BitmapPalette(colors);
            }
            this._firstRun = false ;
        }
 
        public virtual void OnActivated()
        {
            ControlCenter. DataSource dataContext = new ControlCenter.DataSource();
            this.mProcessName = string .Empty;
            this._bioframe = dataContext.BioFrame;
            this._bioframe.OnTutorGuiEvent += new TutorGuiEventDelegate(OnTutorialEvent);
            //this.ReplayVisible = true;
            //this.OnPropertyChanged(new PropertyChangedEventArgs("IsAreaSensor"));
            //this.OnPropertyChanged(new PropertyChangedEventArgs("IsNotAreaSensor"));
        }
 
        public delegate object ob(object o);
        private void OnTutorialEvent(QlClr.Event __p1, Exception e)
        {
            try
            {
                //this.labelmessage.Content = "TPFingerprint is rund";
                byte[] fingerData = null ;
                byte[] imageData = null ;
                int fingerWidth = 0;
                int fingerHeight = 0;
                if (((__p1 != null ) && __p1.Attributes.ContainsKey("image")) && (__p1.Attributes["image" ] is QlClr.Bio. Image))
                {
                    QlClr.Bio. Image image = (QlClr.Bio.Image )__p1.Attributes["image"];
                    FormatType imageFormat = image.GetImageFormat();
                    fingerData = image.GetBWImageData();
                    imageData = image.GetImageData();
                    fingerWidth = ( int)imageFormat.mWidth;
                    fingerHeight = ( int)imageFormat.mHeight;
                }
                if (e != null )
                {
                    this._bioframe.OnTutorGuiEvent -= new TutorGuiEventDelegate(this .OnTutorialEvent);
                }
                this._dispatcher.BeginInvoke(DispatcherPriority .Normal, new ob(delegate (object arg)
                {
                    if (__p1 != null )
                    {
                        if (fingerData != null )
                        {
                            //FormatConvertedBitmapExample2 fom = new FormatConvertedBitmapExample2();
                            //System.Windows.Controls.Image image = new System.Windows.Controls.Image();
                            this.FeedbackImage = BitmapSource .Create(fingerWidth, fingerHeight, 92.0, 92.0,
                                 PixelFormats.Indexed8, this ._palette, fingerData, fingerWidth);
                            this.FeedbackImage1.Source = this .FeedbackImage;
                            this.labelmessage.Content = "Getingerprint is ok." ;
                        }
                    }
                    return null ;
                }), null);
            }
            catch (Exception exception)
            {
            }
        }
 
        public bool IsAreaSensor
        {
            get
            {
                DeviceInfo currentDevice = BioFrame .GetBioFrame().CurrentDevice;
                if (currentDevice == null )
                {
                    return false ;
                }
                if (this .IsNotAreaSensor)
                {
                    return false ;
                }
                return (((currentDevice.Type == 0) || (currentDevice.Type == 3)) || (currentDevice.Type == 0x65));
            }
        }
 
        public bool IsNotAreaSensor
        {
            get
            {
                DeviceInfo currentDevice = BioFrame .GetBioFrame().CurrentDevice;
                return ((currentDevice == null ) || (((currentDevice.Type == 1) || (currentDevice.Type == 2)) || (currentDevice.Type == 100)));
            }
        }
 
        public ImageSource FeedbackImage
        {
            get
            {
                return (ImageSource )base.GetValue(FeedbackImageProperty);
            }
            set
            {
                base.SetValue(FeedbackImageProperty, value );
            }
        }
 
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //RoutedPropertyChangedEventArgs<string> args = new RoutedPropertyChangedEventArgs<string>("", "MainWindows")
            //{
            //    RoutedEvent = LinkActivatedEvent
            //};
            //base.RaiseEvent(args);
            //this.image1.Source = ControlCenter.Moudle.FeedbackImage;
 
            OnActivated();
        }
        private void InstructionsActivated(object sender, RoutedPropertyChangedEventArgs<string > e)
        {
            ControlCenter. DataSource dataContext = base .DataContext as ControlCenter.DataSource;
            dataContext.CurrentPage();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //HelpTutorial
            //ControlCenter.HelpTutorial a = new ControlCenter.HelpTutorial();
            //a.OnActivated();
            //dataContext.CurrentPage();--注???釋???
 
            //mainwindows
        }
        private uint conn = 0;
        private IntPtr pOperation;
        private int result = 0;
        delegate void SendToParent( string txt);
        /// <summary>
        /// Initialize
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonil_Click(object sender, RoutedEventArgs e)
        {
 
            ThreadStart start = delegate ()
            {
                #region 注???釋???
                //uint conn = 0;
                //int result = 0;
                //result = FingerPrintReader.ABSInitialize();
                //Debug.WriteLine("ABSInitialize returns " + result);
                //if (result != ABS_Error_Defs.ABS_STATUS_OK)
                //    g_WaitForFingerPrint = false;
                //result = FingerPrintReader.ABSOpen("usb", ref conn);
                //Debug.WriteLine("ABSOpen returns " + result + " conn = " + conn);
                //if (result != ABS_Error_Defs.ABS_STATUS_OK)
                //    g_WaitForFingerPrint = false;
 
                //ABS_Type_Defs.ABS_OPERATION op = new ABS_Type_Defs.ABS_OPERATION();
                //op.OperationID = 0;
                //op.Context = IntPtr.Zero;
                //op.Timeout = 0;
                //op.Flags = 0;
                ////We will use default callback - need pointer to bsgui.dll which will be sent in the call
                //IntPtr hMod = FingerPrintReader.LoadLibrary("bsgui.dll");
                //IntPtr pAddr = FingerPrintReader.GetProcAddress(hMod, "ABSDefaultCallback");
                //op.Callback = pAddr;
                //// Prepare pointer to ABS_OPERATION
                //IntPtr pOperation = Marshal.AllocHGlobal(Marshal.SizeOf(op));
                //Marshal.StructureToPtr(op, pOperation, false);
 
                //while (g_WaitForFingerPrint)
                //{
                //    IntPtr pTemplate1;
                //    result = FingerPrintReader.ABSCapture(conn, pOperation, 1, out pTemplate1, 0);
                //    Debug.WriteLine("ABSCapture returns " + result + " pTemplate1 = " + pTemplate1);
                //    if (result != ABS_Error_Defs.ABS_STATUS_OK)
                //        continue;
                //    IntPtr pTemplate2;
                //    result = FingerPrintReader.ABSCapture(conn, pOperation, 1, out pTemplate2, 0);
                //    Debug.WriteLine("ABSCapture returns " + result + " pTemplate2 = " + pTemplate2);
                //    if (result != ABS_Error_Defs.ABS_STATUS_OK)
                //        continue;
 
                //    int matchResult = 0;
                //    result = FingerPrintReader.ABSVerifyMatch(conn, pTemplate1, pTemplate2, ref matchResult, 0);
                //    Debug.WriteLine("ABSVerifyMatch returns " + result + " matchResult = " + matchResult);
 
                //    if (result != ABS_Error_Defs.ABS_STATUS_OK)
                //        continue;
                //    if (matchResult == 1)
                //    {
                //        Dispatcher.Invoke(DispatcherPriority.Normal,
                //                          new Action<string>(ShowMainWindow),
                //                          "Finger print verifies success");
                //    }
                //}
                //FingerPrintReader.ABSClose(conn);
                //FingerPrintReader.ABSTerminate();
                #endregion
                result = FingerPrintReader.ABSInitialize();
                if (result != ABS_Error_Defs .ABS_STATUS_OK)
                {
                    MessageBox.Show("Initialize Error!" );
                    return;
                }
                Debug.WriteLine("ABSInitialize returns " + result);
                result = FingerPrintReader.ABSOpen("usb" , ref conn);
                if (result != ABS_Error_Defs .ABS_STATUS_OK)
                {
                    MessageBox.Show("Open Error!" );
                    return;
                }
                Debug.WriteLine("ABSOpen returns " + result + " conn = " + conn);
 
                ABS_Type_Defs.ABS_OPERATION op = new ABS_Type_Defs.ABS_OPERATION ();
                op.OperationID = 0;
                op.Context = IntPtr.Zero;
                op.Timeout = 0;
                op.Flags = 0;
                //We will use default callback - need pointer to bsgui.dll which will be sent in the call
                IntPtr hMod = FingerPrintReader .LoadLibrary("bsgui.dll");
                IntPtr pAddr = FingerPrintReader .GetProcAddress(hMod, "ABSDefaultCallback");
                op.Callback = pAddr;
                // Prepare pointer to ABS_OPERATION
                pOperation = Marshal.AllocHGlobal(Marshal .SizeOf(op));
                Marshal.StructureToPtr(op, pOperation, false );
                SendToParent s = new SendToParent(label);
                this.Dispatcher.Invoke(s, new object[] { "Open Successfull" });
            };
            // Create the thread and kick it started!
            Thread th = new Thread(start);
            th.IsBackground = true;
            th.Start();
            return;
        }
 
        private void label(string str)
        {
            this.label1.Content = str;
            this.label1.Foreground = new SolidColorBrush( Colors.Green);
        }
 
        /// <summary>
        /// Close
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonClose_Click(object sender, RoutedEventArgs e)
        {
            FingerPrintReader.ABSClose(conn);
            FingerPrintReader.ABSTerminate();
        }
 
 
        [ DllImport("kernel32 " )]
        public extern static void CopyMemory(System.IntPtr dest, System.IntPtr source, int size);
        /// <summary>
        /// Enroll
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonEnroll_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ABS_BIR bir = new ABS_BIR();
                IntPtr pbir = IntPtr .Zero;
 
 
                int size = Marshal .SizeOf(bir);
                pbir = Marshal.AllocHGlobal(size);
                Marshal.StructureToPtr(bir, pbir, false );
                result = FingerPrintReader.ABSEnroll(conn, pOperation, ref pbir, 0);
                bir = ( ABS_BIR)Marshal .PtrToStructure(pbir, typeof( ABS_BIR));
 
                byte[] b = new byte[bir.Header.Length];
                for (int i = 0; i < bir.Header.Length; i++)
                {
                    b[i] = Marshal.ReadByte(pbir, i);
                }
 
                string strFilex = @"d:\test.txt" ;
                FileStream fsx = new FileStream(strFilex, FileMode.Create, FileAccess .Write);
                //byte[] bytDatax = rawSerialize(bir);
                byte[] bytDatax = b;
                fsx.Write(bytDatax, 0, bytDatax.Length);
                fsx.Close();
                MessageBox.Show("保???存??成??功|!" );
               
            }
            catch (Exception ex)
            {
                MessageBox.Show("#" +ex.Message);
            }
 
        }
 
 
 
 
        #region 寫??入??二t進?制?文?件t
 
        //結??構1類???型??轉?a數?y組???
        private byte [] Struct2Byte(ABS_BIR s)
        {
            int structSize = Marshal .SizeOf(s);
            byte[] buffer = new byte[structSize];
            //分??配?結??構1體??大???小?的??內??存??空?間?
            IntPtr structPtr = Marshal .AllocHGlobal(structSize);
            //將?結??構1體??拷?到??分??配?好?的??內??存??空?間?
            Marshal.StructureToPtr(s, structPtr, false );
            //從???內??存??空?間?拷?到??byte數?y組???
            Marshal.Copy(structPtr, buffer, 0, structSize);
            //釋???放??內??存??空?間?
            Marshal.FreeHGlobal(structPtr);
            return buffer;
        }
 
        //使?1用??這a個?方??法???將?byte數?y組???轉?a化??爲a結??構1體??
        public static object BytesToStuct( byte[] bytes, Type type)
        {
        //得??到??結??構1體??的??大???小?
        int size = Marshal .SizeOf(type);
        //byte數?y組???長??度??小?於???結??構1體??的??大???小?
        if (size > bytes.Length)
        {
        //返???回?空?
        return null ;
        }
        //分??配?結??構1體??大???小?的??內??存??空?間?
        IntPtr structPtr = Marshal .AllocHGlobal(size);
        //將?byte數?y組???拷?到??分??配?好?的??內??存??空?間?
        Marshal.Copy(bytes, 0, structPtr, size);
        //將?內??存??空?間?轉?a換?爲a目?標???結??構1體??
        object obj = Marshal .PtrToStructure(structPtr, typeof( Type));
        //釋???放??內??存??空?間?
        Marshal.FreeHGlobal(structPtr);
        //返???回?結??構1體??
        return obj;
        }
        //序??列?D化??
        public static byte[] rawSerialize( object obj)
        {
            int rawsize = Marshal .SizeOf(obj);
            IntPtr buffer = Marshal .AllocHGlobal(rawsize);
            Marshal.StructureToPtr(obj, buffer, false );
            byte[] rawdatas = new byte[rawsize]; Marshal.Copy(buffer, rawdatas, 0, rawsize);
            Marshal.FreeHGlobal(buffer);return rawdatas;
        }
        #endregion
    }
}
  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章