using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;
using System.Windows;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace testOcx
{
   

    public partial class Form1 : Form
    {
        private delegate void DelegateRefreshUI(int type,string str);  
         [DllImport("shell32.dll")]
        public extern static IntPtr ShellExecute(IntPtr hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, int nShowCmd);
        bool isopen = false;
        int CamID = 0;  //此例程默认以第一个摄像头打开
        int SupportFormat;    //设备支持的视频格式代号：1->MJPG; 2->YUY2 
        int OpenFormat;      //打开格式：0->YUY；1->MJPG
        bool flag = false;

        int ww = 0, hh = 0;//分辨率
        int RotateType = 0;//旋转的方向,0为0度与180度,1为90度与270度

        public Form1()
        {
            InitializeComponent();
            this.Load += new EventHandler(Form1_Load);
           // axBaucheOcx1.helloTest();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            initLoad();
        }


        private void initLoad()
        {
            int i = axBaucheOcx1.GetDevCount();  //获取设备数目
            if (i > 1)
            {
                CamID = axBaucheOcx1.GetMainCameraID(1);
                //int isCheck = axBaucheOcx1.CheckDevice(CamID);
                //if (isCheck == 0)
                //{
                //    CamID = 1;
                //    ComboBox2.SelectedIndex = 1;
                //}
            }
            else CamID = 0;



            CamID = axBaucheOcx1.GetMainCameraID(1);
            int FormatSum = 0;  //设备支持的视频格式代号总和
            FormatSum = axBaucheOcx1.GetFormatCount(CamID);
            switch (FormatSum)
            {
                case 1:   //（设备只支持MJPG格式）
                    OpenFormat = 1;     //打开格式为MJPG
                    SupportFormat = 1;
                    break;
                case 2:   //（设备只支持YUY2格式）
                    OpenFormat = 0;     //打开格式为YUY
                    SupportFormat = 2;
                    break;
                case 3:   //（设备支持MJPG与YUY2两种格式）
                    OpenFormat = 1;     //默认打开格式为MJPG
                    SupportFormat = 1;
                    break;
            }

            ComboBox1.Items.Clear();
            ComboBox2.Items.Clear();

            for (int j = 0; j < i; j++)
            {
                ComboBox2.Items.Add(axBaucheOcx1.GetDevFriendName(j));  //获取设备名称
            }
            if (i > 0) ComboBox2.SelectedIndex = CamID;

            i = axBaucheOcx1.GetResolutionCount(CamID, SupportFormat); //获取设备支持的分辨率数目
            for (int j = 0; j < i; j++)
            {
                ComboBox1.Items.Add(axBaucheOcx1.GetResolution(j));  //获取分辨率     
            }
            if (i > 0) ComboBox1.SelectedIndex = 0;



            ComboBox3.SelectedIndex = 0;
            ComboBox4.SelectedIndex = 0;
            flag = true;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (isopen == true) return;
            string SizeStr = ComboBox1.SelectedItem.ToString();
            int pos, width, height;
            pos = SizeStr.LastIndexOf("*");
            width = Convert.ToInt32(SizeStr.Substring(0, pos));  //宽
            height = Convert.ToInt32(SizeStr.Substring(pos + 1, SizeStr.Length - (pos + 1)));
            int i = axBaucheOcx1.CAM_Open(CamID, OpenFormat, width, height);
            if (i == 0)
            {
                isopen = true;
                ww = width;
                hh = height;
            }
            else if (i == -1)
            {
                MessageBox.Show("请使用本公司设备！");
            }
            else if (i == -2)
            {
                MessageBox.Show("开启失败！");
            }

            

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (isopen == true) {
                axBaucheOcx1.CAM_Close();
                isopen = false;
            }
        }

        private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isopen == true)
            {
                axBaucheOcx1.CAM_Close();

                string SizeStr = ComboBox1.SelectedItem.ToString();
                int pos, width, height;
                pos = SizeStr.LastIndexOf("*");
                width = Convert.ToInt32(SizeStr.Substring(0, pos));  //宽
                height = Convert.ToInt32(SizeStr.Substring(pos + 1, SizeStr.Length - (pos + 1)));  //高

                int i = axBaucheOcx1.CAM_Open(CamID, OpenFormat, width, height);  //开启摄像头
                if (i == 0)
                {
                   // Debug.WriteLine("切换分辨率成功");
                    ww = width;
                    hh = height;
                    isopen = true;
                }
                else if (i == -1)
                {
                    MessageBox.Show("请使用本公司设备！");
                }
                else if (i == -2)
                {
                    MessageBox.Show("开启失败！");
                } 
            } 
        }

        private void button3_Click(object sender, EventArgs e)
        {
            axBaucheOcx1.CaptureImage();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int num = axBaucheOcx1.GetSerImgFileCount();
            if (num > 0)
            {
                string imgPath = axBaucheOcx1.GetSerImgFileNameAt(num - 1);
                ShellExecute(this.Handle, "open", imgPath, null, null, 4);
            }
        }

        private void ComboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (flag == true)
            {
                axBaucheOcx1.CAM_Close();

                CamID = ComboBox2.SelectedIndex;
                int FormatSum = 0;  //设备支持的视频格式代号总和
                FormatSum = axBaucheOcx1.GetFormatCount(CamID);   //获取设备支持的视频格式数目
                //for (int k = 0; k < scount; k++)
                //{
                //    FormatSum = FormatSum + axCam_Ocx1.GetFormatNumber(k);    //GetFormatNumber（）获取设备支持的视频格式代号
                //}

                //判断视频格式代号总和的值
                switch (FormatSum)
                {
                    case 1:   //（设备只支持MJPG格式）
                        OpenFormat = 1;     //打开格式为MJPG
                        SupportFormat = 1;
                        break;
                    case 2:   //（设备只支持YUY2格式）
                        OpenFormat = 0;     //打开格式为YUY
                        SupportFormat = 2;
                        break;
                    case 3:   //（设备支持MJPG与YUY2两种格式）
                        OpenFormat = 1;     //默认打开格式为MJPG
                        SupportFormat = 1;
                        break;
                }

                ComboBox1.Items.Clear();
                int i = axBaucheOcx1.GetResolutionCount(CamID, SupportFormat); //获取设备支持的分辨率数目
                for (int j = 0; j < i; j++)
                {
                    ComboBox1.Items.Add(axBaucheOcx1.GetResolution(j));  //获取分辨率     
                }
                if (i > 0) ComboBox1.SelectedIndex = 0;
            }
        }

        private void CheckBox4_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox4.Checked == true) axBaucheOcx1.DragVideo(1);
            else axBaucheOcx1.DragVideo(0);
        }

        private void CheckBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox3.Checked == true) axBaucheOcx1.FrozenVideo(1);
            else axBaucheOcx1.FrozenVideo(0);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            axBaucheOcx1.CAM_zoomin();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            axBaucheOcx1.CAM_zoomout();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            CheckBox3.Checked = false;
            axBaucheOcx1.CAM_RotateL();
            RotateType++;
            if (RotateType > 1) RotateType = 0;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            CheckBox3.Checked = false;
            axBaucheOcx1.CAM_RotateR();
            RotateType++;
            if (RotateType > 1) RotateType = 0;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            axBaucheOcx1.BestSize();
        }

        private void button10_Click(object sender, EventArgs e)
        {
            axBaucheOcx1.TrueSize(); 
        }

        private void ComboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            axBaucheOcx1.SetImageColorMode(ComboBox3.SelectedIndex);
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true) axBaucheOcx1.SetAutoCut(0);
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked == true) axBaucheOcx1.SetAutoCut(1);
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton3.Checked == true) axBaucheOcx1.CusCrop(1);
        }

        private void button11_Click(object sender, EventArgs e)
        {
          

            //int num = axBaucheOcx1.GetSerImgFileCount();
            //if (num > 0)
            //{
            //    string imgPath = axBaucheOcx1.GetSerImgFileNameAt(num - 1);
            //    string aa = axBaucheOcx1.GetBarcodeContent(imgPath);
            //    MessageBox.Show(aa);
            //}

            string str = axBaucheOcx1.ReadBarQrcodeD(1);
            MessageBox.Show(str);
        }

        private void button13_Click(object sender, EventArgs e)
        {
            //int num = axBaucheOcx1.GetSerImgFileCount();
            //if (num > 0)
            //{
            //    string imgPath = axBaucheOcx1.GetSerImgFileNameAt(num - 1);
            //    string aa = axBaucheOcx1.GetQRcodeContent(imgPath);
            //    MessageBox.Show(aa);
            //}

            string str = axBaucheOcx1.ReadBarQrcodeD(2);
            MessageBox.Show(str);
        }

        int dir = 1;
        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton4.Checked == true) dir = 1; //垂直
        }

        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton5.Checked == true) dir = 2; //水平
        }

        private void button14_Click(object sender, EventArgs e)
        {
            string combinePath = "D:\\CombineImg.jpg";
            string ImgPath1 = "D:\\1.jpg";
            string ImgPath2 = "D:\\2.jpg";
            int ret = axBaucheOcx1.CombineTwoImage(combinePath, ImgPath1, ImgPath2, dir);
        }

        private void button15_Click(object sender, EventArgs e)
        {
            if (isopen == true)
            {
                Label1.Enabled = true;
                Label2.Enabled = true;
                TrackBar1.Enabled = true;
                CheckBox1.Enabled = true;
                int min, max, CurrentVal;

                string strRang = axBaucheOcx1.GetBrightnessRange();  //获取亮度值范围
                int pos = strRang.LastIndexOf("*");
                min = Convert.ToInt32(strRang.Substring(0, strRang.LastIndexOf("*")));
                max = Convert.ToInt32(strRang.Substring(pos + 1, strRang.Length - (pos + 1)));

                CurrentVal = axBaucheOcx1.GetCurrentBrightness();  //获取亮度当前值
                TrackBar1.Minimum = min;
                TrackBar1.Maximum = max;
                if (CurrentVal > -1000) TrackBar1.Value = CurrentVal;

                strRang = axBaucheOcx1.GetExposureRange();  //获取曝光值范围
                pos = strRang.LastIndexOf("*");
                min = Convert.ToInt32(strRang.Substring(0, strRang.LastIndexOf("*")));
                max = Convert.ToInt32(strRang.Substring(pos + 1, strRang.Length - (pos + 1)));

                CurrentVal = axBaucheOcx1.GetCurrentExposure();  //获取曝光当前值
                TrackBar2.Minimum = min;
                TrackBar2.Maximum = max;
                if (CurrentVal > -1000) TrackBar2.Value = CurrentVal;

                CheckBox1.Checked = true;
            }

        }

        private void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox1.Checked == true)
            {
                axBaucheOcx1.SetAutoExposure(1);
                TrackBar2.Enabled = false;
            }
            else
            {
                axBaucheOcx1.SetAutoExposure(0);
                TrackBar2.Enabled = true;
            } 
        }

        private void TrackBar1_Scroll(object sender, EventArgs e)
        {
            axBaucheOcx1.SetBrightness(TrackBar1.Value);
           
        }

        private void TrackBar2_Scroll(object sender, EventArgs e)
        {
            axBaucheOcx1.SetExposure(TrackBar2.Value);
        }

        private void button16_Click(object sender, EventArgs e)
        {
            axBaucheOcx1.ShowImageSettingWindow();
        }

        private void button18_Click(object sender, EventArgs e)
        {
            int i = axBaucheOcx1.GetSerImgFileCount();
            if (i > 0)
            {
                String str = axBaucheOcx1.GetSerImgFileNameAt(i-1);

                MessageBox.Show(str);
            }
        }

        private void button19_Click(object sender, EventArgs e)
        {
            axBaucheOcx1.SetAddMark(1);
            axBaucheOcx1.SetWaterMark("水印测试", 30, 0, 255, 0, 0, 50, 50);
        }

        private void button20_Click(object sender, EventArgs e)
        {
            axBaucheOcx1.SetAddMark(0);
        }

        private void button21_Click(object sender, EventArgs e)
        {
            int num = axBaucheOcx1.GetSerImgFileCount();
            if (num > 0)
            {
                string imgPath = axBaucheOcx1.GetSerImgFileNameAt(num - 1);
                string str = axBaucheOcx1.GetBase64FromFile(imgPath);

            }      
        }

        private void axBaucheOcx1_MessageCallback(object sender, AxBaucheOcxLib._DBaucheOcxEvents_MessageCallbackEvent e)
        {
                this.Invoke(new DelegateRefreshUI(freshUI), e.type, e.str);  
        }

        private void ComboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            axBaucheOcx1.SetFileType(ComboBox4.SelectedIndex);
        }

        private void button23_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
            dialog.Description = "请选择Txt所在文件夹";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (string.IsNullOrEmpty(dialog.SelectedPath))
                {
                    MessageBox.Show(this, "文件夹路径不能为空", "提示");
                    return;
                }
                else {
                    int aa = axBaucheOcx1.setFilePath(dialog.SelectedPath);
                    if (aa == 0)
                    {
                        MessageBox.Show(this, "成功", "提示");
                    }
                    else {
                        MessageBox.Show(this, "失败", "提示");
                    }
                    
                }
            }
        }

        private void radioButton6_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton6.Checked)
            {
                String str = textBox1.Text;
                axBaucheOcx1.setFileNameCustom(str, 0);
            }
        }

        private void radioButton7_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton7.Checked) axBaucheOcx1.setFileNameTime();
        }

        private void radioButton8_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton8.Checked)
            {
                String str = textBox1.Text;
                axBaucheOcx1.setFileNameFixed(str);
            }
        }

        private void button24_Click(object sender, EventArgs e)
        {
            String str1 = "D:\\1.jpg";
            String str2 = "D:\\2.jpg";
            String str3 = "D:\\3.jpg";
            String str4 = "D:\\4.pdf";
            axBaucheOcx1.AddPDFImageFile(str1, str1, 0);
            axBaucheOcx1.AddPDFImageFile(str2, str1, 0);
            axBaucheOcx1.AddPDFImageFile(str3, str1, 0);
            axBaucheOcx1.Convert2PDF(str4);
        }

        private void freshUI(int type,string str)
        {
            if (type == 2)
            {
                Label12.Text = "智能连拍完成张数:" + str;
            }
            else if (type == 3)
            {
                label3.Text = "定时连拍完成张数:" + str;
            }
            else if (type == 1)
            {
                    Label13.Text = "图片:" + str;
            }
            else if (type == 4)
            {
                if (isopen == true)
                {
                    axBaucheOcx1.CAM_Close();
                    isopen = false;
                }
                initLoad();
                MessageBox.Show(this, "当前设备数量为" + str, "提示");

            }
            else if (type == 5)
            {
                Label13.Text = "图片:保存失败" + str;
            }
            else if (type == 6)
            {
                Label13.Text = "身份证信息" + str;
            }
            else if (type == 7)
            {
                MessageBox.Show(this, "未发现身份证模块", "提示");
            }
            else if (type == 8)
            {
            Label13.Text = "图片base64已返回";
            byte[] contents = Convert.FromBase64String(str);
                string outPath = "D:\\测试base64.jpg";
                using (var fs = new FileStream(outPath, FileMode.Create, FileAccess.Write))
                {
                    fs.Write(contents, 0, contents.Length);
                    fs.Flush();
                }
            }
            else if (type == 11)
            {
                int result = int.Parse(str);
                if (result == 1)
                {
                    MessageBox.Show(this, "指纹认证成功", "提示");
                }else MessageBox.Show(this, "指纹认证失败" , "提示");
            }
            else if (type == 12)
            {
                MessageBox.Show(this, "录像停止", "提示");
            }
            else if (type == 13)
            {
                Label13.Text = "人脸捕抓图片base64已返回";
                byte[] contents = Convert.FromBase64String(str);
                MemoryStream ms = new MemoryStream(contents);
                Bitmap bmp = new Bitmap(ms);
                Bitmap bmpShow=ResizeImage(bmp, pictureBox2);
                pictureBox2.Image = bmpShow;
                //string outPath = "D:\\人脸base64.jpg";
                //using (var fs = new FileStream(outPath, FileMode.Create, FileAccess.Write))
                //{
                //    fs.Write(contents, 0, contents.Length);
                //    fs.Flush();
                //}
            }
            else if (type == 14)
            {
                MessageBox.Show(this, "人脸捕抓失败", "提示");
            }
            else if (type == 15)
            {
                if (str == "1")
                {
                    MessageBox.Show(this, "指纹录入成功", "提示");
                }else  MessageBox.Show(this, "指纹录入失败", "提示");
            }
            else if (type == 16)
            {
                MessageBox.Show(this, "指纹录入失败,请录入同样的指纹", "提示");
            }
            else if (type == 17)
            {
                MessageBox.Show(this, "请再完整指纹", "提示");
            }
            else if (type == 18)
            {
                if (str == "1")
                {
                    MessageBox.Show(this, "指纹对比成功", "提示");
                }else MessageBox.Show(this, "指纹对比失败", "提示");
               
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked)
            {
                axBaucheOcx1.setBuBai(1);
            }else axBaucheOcx1.setBuBai(0);
        }

        private void checkBox5_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox5.Checked)
            {
                axBaucheOcx1.setQudise(1);
            }
            else axBaucheOcx1.setQudise(0);
        }

        private void radioButton9_CheckedChanged(object sender, EventArgs e)
        {
            //if (radioButton9.Checked)
            //{
            //    axBaucheOcx1.WiseCapture(0);
            //    axBaucheOcx1.TimeCapture(0, 5000);
            //}
        }

        private void radioButton10_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton10.Checked)
            {
                int i = axBaucheOcx1.WiseCapture(1);
                if (i != 0) { MessageBox.Show("开启智能连拍失败！"); }
            }
            else
            {

                axBaucheOcx1.WiseCapture(0);
                Label12.Text = "智能连拍完成张数:0";
            }
        }

        private void radioButton11_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton11.Checked)
            {
                int i = axBaucheOcx1.TimeCapture(1, 5000);
                if (i != 0)
                {
                    MessageBox.Show("开启定时连拍失败！");
                }
            }
            else {
                axBaucheOcx1.TimeCapture(0, 5000);
                label3.Text = "定时连拍完成张数:0";
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            int num = axBaucheOcx1.GetSerImgFileCount();
            if (num > 0)
            {
                string imgPath = axBaucheOcx1.GetSerImgFileNameAt(num - 1);
                axBaucheOcx1.UpdataFile("localhost", 19890, "/WebSite1/WebService.asmx/HelloWorld?cmd1=cmd1",imgPath);
            }
        }

        private void radioButton12_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton12.Checked) axBaucheOcx1.setJiubianModel(0);
        }

        private void radioButton13_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton13.Checked) axBaucheOcx1.setJiubianModel(1);
        }


        private void radioButton14_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton14.Checked == true) axBaucheOcx1.PreviewCrop(1);
        }

        private void button17_Click(object sender, EventArgs e)
        {
            axBaucheOcx1.StartIDCard();
        }

        private void button18_Click_1(object sender, EventArgs e)
        {
            int ret = axBaucheOcx1.GetOneIC();

            if (ret == 0)
            {
                MessageBox.Show("身份证读卡成功\r\n");
            }
            else if (ret == -1)
            {
                MessageBox.Show("请重新操作\r\n");
            }
            else if (ret == -2)
            {
                MessageBox.Show("身份证读卡失败\r\n");
            }
            else if (ret == -3)
            {
                MessageBox.Show("没发现身份证模块\r\n");
            }
            else if (ret == -4)
            {
                MessageBox.Show("请先启动身份证功能\r\n");
            }
        }

        private void button22_Click(object sender, EventArgs e)
        {
            string name = axBaucheOcx1.GetICValues("NAME");
            string IC = axBaucheOcx1.GetICValues("IC");
            string SEX = axBaucheOcx1.GetICValues("SEX");
            string NATION = axBaucheOcx1.GetICValues("NATION");
            string BIRTHDAY = axBaucheOcx1.GetICValues("BIRTHDAY");
            string ADDRESS = axBaucheOcx1.GetICValues("ADDRESS");
            string SIGNDEPT = axBaucheOcx1.GetICValues("SIGNDEPT");
            string VALIDSTARTDATE = axBaucheOcx1.GetICValues("VALIDSTARTDATE");
            string VALIDENDDATE = axBaucheOcx1.GetICValues("VALIDENDDATE");
            string str = name + "***" + IC + "***" + SEX + "***" + NATION + "***" + BIRTHDAY + "***" + ADDRESS + "***" + SIGNDEPT + "***" + VALIDSTARTDATE + "***" + VALIDENDDATE + "\r\n";
            MessageBox.Show(str);
        }

        private void button25_Click(object sender, EventArgs e)
        {
            string strBase64 = axBaucheOcx1.GetICPicture();
            byte[] arr = Convert.FromBase64String(strBase64);
            MemoryStream ms = new MemoryStream(arr);
            Bitmap bmp = new Bitmap(ms);
            pictureBox1.Image = bmp;
        }

        private void button26_Click(object sender, EventArgs e)
        {
            int ret = axBaucheOcx1.WorkIDCard(1);
            if (ret == 0)
            {
                MessageBox.Show("开始读卡\r\n");
            }
            else if (ret == -1)
            {
                MessageBox.Show("已经在读卡\r\n");
            }
            else if (ret == -2)
            {
                MessageBox.Show("身份证功能未启动\r\n");
            }
            else if (ret == -3)
            {
                MessageBox.Show("为发现身份证模块\r\n");
            }
        }

        private void button27_Click(object sender, EventArgs e)
        {
            int ret = axBaucheOcx1.WorkIDCard(0);
            if (ret == 0)
            {
                MessageBox.Show("停止读卡\r\n");
            }
            else if (ret == -2)
            {
                MessageBox.Show("身份证功能未启动\r\n");
            }
        }

        private void button28_Click(object sender, EventArgs e)
        {
            if (ww != 0 && hh != 0)
            {
                int ptr = axBaucheOcx1.GetRGBData();
                //如果旋转方式是0度与180度
                if (RotateType == 0)
                {
                    RGBDataProcess(ptr, ww, hh);
                }
                //如果旋转方式是90度与270度
                else
                {
                    RGBDataProcess(ptr, hh, ww);
                }
            }
        }

        unsafe public static void RGBDataProcess(int p,int w,int h)
        {
            byte* rgbp = (byte*)p;
            int len = w * h * 3;
            byte[] rgbValues = new byte[len];
            for (int i = 0; i < h; i++)
            {
                for (int j = 0; j < w; j++)
                {
                    rgbValues[i * w * 3 + j * 3] = rgbp[i * w * 3 + j * 3];
                    rgbValues[i * w * 3 + j * 3 + 1] = rgbp[i * w * 3 + j * 3 + 1];
                    rgbValues[i * w * 3 + j * 3 + 2] = rgbp[i * w * 3 + j * 3 + 2];
                }
            }

            Bitmap bmp = new Bitmap(w, h);

            System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new Rectangle(Point.Empty, bmp.Size), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            IntPtr ptr = bmpData.Scan0;
            System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, len);

            bmp.UnlockBits(bmpData);
            bmp.Save("D:\\rbgdata.jpg");

        }

        private void button29_Click(object sender, EventArgs e)
        {
            string base64 = axBaucheOcx1.GetICPictureAll();
            if (base64.Length > 0)
            {
                byte[] contents = Convert.FromBase64String(base64);//获取图片二进制数组

                string outPath = "D:\\身份证.jpg";
                using (var fs = new FileStream(outPath, FileMode.Create, FileAccess.Write))
                {
                    fs.Write(contents, 0, contents.Length);
                    fs.Flush();
                }
            }
            else MessageBox.Show("获取身份正反面失败\r\n");

        }

        private void 设置拍照只返回base64_Click(object sender, EventArgs e)
        {
            axBaucheOcx1.IsImageBase64(1);
        }

        private void button30_Click(object sender, EventArgs e)
        {
            axBaucheOcx1.SetCusCropPlace(200, 200, 500, 500);
        }

        private void button31_Click(object sender, EventArgs e)
        {
            int ret = axBaucheOcx1.InitFinger();
            if (ret == 1)
            {
                  MessageBox.Show("初始化指纹模块成功\r\n");
            }
            else MessageBox.Show("初始化指纹模块失败\r\n");
        }

        private void button32_Click(object sender, EventArgs e)
        {
            int ret = axBaucheOcx1.StartFinger();
            if (ret == 1)
            {
                MessageBox.Show("开始指纹认证\r\n");
            }
            else if (ret == -1)
            {
                MessageBox.Show("未初始化指纹模块\r\n");
            }
            else if (ret == -2)
            {
                MessageBox.Show("身份证没有指纹数据\r\n");
            }
            else if (ret == -3)
            {
                MessageBox.Show("未发现指纹模块\r\n");
            }
            else if (ret == -4)
            {
                MessageBox.Show("正在指纹认证\r\n");
            }
        }

        private void button33_Click(object sender, EventArgs e)
        {
            axBaucheOcx1.CloseFinger();
        }

        private void button34_Click(object sender, EventArgs e)
        {
            int ret = axBaucheOcx1.BeginVideoID("D:\\test.avi",0,15);
            if (ret == -1)
            {
                MessageBox.Show("录像最大支持分辨率是1600*1200\r\n");
            }
            else if (ret == -2)
            {
                MessageBox.Show("正在录像中\r\n");
            }
        }

        private void button35_Click(object sender, EventArgs e)
        {
            axBaucheOcx1.StopVideo();
        }

        private void button36_Click(object sender, EventArgs e)
        {
            string str = axBaucheOcx1.GetAudioName();
            if (str == "-1")
            {
                MessageBox.Show("正在录像中,无法获取麦克风\r\n");
            }
            else if (str == "0")
            {
                MessageBox.Show("没有发现麦克风\r\n");
            }else MessageBox.Show("麦克风: "+str);
        }

        private void button37_Click(object sender, EventArgs e)
        {
            int ret = axBaucheOcx1.FaceCheckInit();
            if (ret == 1)
            {
                MessageBox.Show("初始化成功\r\n");
            }
            else MessageBox.Show("初始化失败\r\n");
        }

        private void button39_Click(object sender, EventArgs e)
        {
            int ret = axBaucheOcx1.StartFaceCheck();
            if (ret == 1)
            {
                //初始化成功
                //MessageBox.Show("初始化成功\r\n");
            }
            else if (ret == -1)
            {
                MessageBox.Show("请先读取身份证\r\n");

            }
            else if (ret == -2)
            {
                MessageBox.Show("未初始化\r\n");

            }

        }

        private void button38_Click(object sender, EventArgs e)
        {
            axBaucheOcx1.FaceCheckUninit();
        }

        private Bitmap ResizeImage(Bitmap bmp, PictureBox picBox)
        {
            float xRate = (float)bmp.Width / picBox.Size.Width;
            float yRate = (float)bmp.Height / picBox.Size.Height;
            if (xRate <= 1 && yRate <= 1)
            {
                return bmp;
            }
            else
            {
                float tRate = (xRate >= yRate) ? xRate : yRate;
                Graphics g = null;
                try
                {
                    int newW = (int)(bmp.Width / tRate);
                    int newH = (int)(bmp.Height / tRate);
                    Bitmap b = new Bitmap(newW, newH);
                    g = Graphics.FromImage(b);
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
                    g.Dispose();
                    //bmp.Dispose();
                    return b;
                }
                catch
                {
                    //bmp.Dispose();
                    return null;
                }
                finally
                {
                    if (null != g)
                    {
                        g.Dispose();
                    }
                }
            }
        }

        private void button40_Click(object sender, EventArgs e)
        {
            axBaucheOcx1.StopFaceCheck();
        }

        private void button41_Click(object sender, EventArgs e)
        {
            int ret = axBaucheOcx1.InitBankCard();
            if (ret == 1)
            {
                MessageBox.Show("初始化银行卡成功\r\n");
            }
            else {
                MessageBox.Show("没有发现银行卡模块\r\n");
            }
        }

        private void button42_Click(object sender, EventArgs e)
        {
            string str = axBaucheOcx1.GetBankCardNo();
            if (str.Length > 10)
            {
                string[] s = str.Split(new char[] { 'D' });
                MessageBox.Show("银行卡号" + s[0] + "   有效日期(年月)" + s[1]);
            }
            else
            {
                if (str == "-1")
                {
                    MessageBox.Show("先初始化银行卡\r\n");
                }else if (str == "-2")
                {
                    MessageBox.Show("没有发现银行卡模块\r\n");
                }
                else if (str == "-3")
                {
                    MessageBox.Show("没有检查到银行卡\r\n");
                }
                else if (str == "-5")
                {
                    MessageBox.Show("请检查卡片是否插入卡座正确\r\n");
                }
                else 
                {
                    MessageBox.Show("解析数据有误\r\n");
                }
            }
        }

        private void button43_Click(object sender, EventArgs e)
        {
            axBaucheOcx1.UninitBankCard();

        }

        private void button44_Click(object sender, EventArgs e)
        {
            int ret = axBaucheOcx1.InitFingerFun();
            if (ret == 1)
            {
                MessageBox.Show("初始化指纹成功\r\n");
            }
            else
            {
                MessageBox.Show("初始化指纹失败\r\n");
            }
        }

        private void button45_Click(object sender, EventArgs e)
        {
            axBaucheOcx1.CloseFingerFun();
        }

        private void button46_Click(object sender, EventArgs e)
        {
            int ret = axBaucheOcx1.RegisterFingerData();
            if (ret == 1)
            {
                MessageBox.Show("开始指纹录入\r\n");
            }
            else if (ret == -1)
            {
                MessageBox.Show("先初始化指纹功能\r\n");
            }
            else if (ret == -2)
            {
                MessageBox.Show("正在处于对比指纹状态,请先停止指纹比对\r\n");
            }
            else if (ret == -3)
            {
                MessageBox.Show("已经启动指纹录入\r\n");
            }
            else
            {
                MessageBox.Show("启动指纹录入失败\r\n");
            }
        }

        private void button47_Click(object sender, EventArgs e)
        {
            axBaucheOcx1.StopRegisterFinger();
        }

        private void button48_Click(object sender, EventArgs e)
        {
            if (fingerDatalen > 0)
            {
                GCHandle hObject = GCHandle.Alloc(fingerData, GCHandleType.Pinned);
                IntPtr pObject = hObject.AddrOfPinnedObject();

                if (hObject.IsAllocated)
                    hObject.Free();

                int ret = axBaucheOcx1.VerifyFingerData((int)pObject, (int)fingerDatalen);
                if (ret == 1)
                {
                    MessageBox.Show("开始指纹对比\r\n");
                }
                else if (ret == -1)
                {
                    MessageBox.Show("先初始化指纹功能\r\n");
                }
                else if (ret == -2)
                {
                    MessageBox.Show("正在处于指纹录入状态,请先停止指纹录入\r\n");
                }
                else if (ret == -3)
                {
                    MessageBox.Show("已经启动指纹对比\r\n");
                }
                else if (ret == -4)
                {
                    MessageBox.Show("指纹数据不存在\r\n");
                }
                else
                {
                    MessageBox.Show("启动指纹对比失败\r\n");
                }
            }
            else MessageBox.Show("先获取指纹数据\r\n");
        }

        private void button49_Click(object sender, EventArgs e)
        {
            axBaucheOcx1.StopVerifyFinger();
        }

        byte[] fingerData;
        int fingerDatalen = 0;
        private void button50_Click(object sender, EventArgs e)
        {
            fingerDatalen = axBaucheOcx1.GetFingerDataLen();
            unsafe
            {
                fingerData = new byte[fingerDatalen];
                int ptr = axBaucheOcx1.GetFingerData();
                byte* data = (byte*)ptr;
                for (int i = 0; i < fingerDatalen; i++)
                {
                    fingerData[i] = data[i];
                }

            }
        }

        private void checkBox6_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox6.Checked)
            {
                axBaucheOcx1.InitFaceCheck();
            }
            else axBaucheOcx1.DeInitFaceCheck();
        }

        private void button52_Click(object sender, EventArgs e)
        {
            axBaucheOcx1.CaptureFace();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            axBaucheOcx1.CAM_Close();
        }

    }
}
