Newer
Older
CaptureEye / IrisCtrl / IrisColCtrl.cs
yxw on 14 Aug 2020 3 KB first commt
using System;
using System.Drawing;
using System.Windows.Forms;
using IrisCtrl.Json;
using OpenCvSharp;
using System.Threading;
using IrisCtrl.Camera;

namespace IrisCtrl
{
    public partial class IrisColCtrl : UserControl
    {
        private CameraVideo cameraVideo;
        
        public IrisColCtrl()
        {
            InitializeComponent();
            pictureBox_Left.Image = Properties.Resources.left;
            pictureBox_Right.Image = Properties.Resources.right;
        }

        //打开相机、初始化HID打开可见灯
        public int IrisInit(String initStr)
        {
            try
            {
                InitParam initParam = tools.StringToInitParam(initStr);
                pictureBox_Left.Image = null;
                pictureBox_Right.Image = null;
                pictureBox_Left.BackColor = Color.Black;
                pictureBox_Right.BackColor = Color.Black;

                cameraVideo = new CameraVideo();

                Thread th = new Thread(playVideo);
                th.Start();
                
                return 0;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        private void playVideo()
        {
            //打开相机,显示视频
            while (cameraVideo.ShowVideo)
            {
                cameraVideo.LeftVideo.Read(cameraVideo.LeftFrame);
                Bitmap bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(cameraVideo.LeftFrame);
                pictureBox_Left.Image = bitmap;
                Cv2.WaitKey(50);
            }
        }

        //设置参数获取图片(左眼右眼双眼),注册抓图事件
        public int StartCapture(String eyeType)
        {
            cameraVideo.ShowVideo = true;

            StartParam startParam = tools.StringToStartParam(eyeType);
            return 1;
        }
        
        /// <summary>
        /// 重置,关闭摄像机
        /// </summary>
        /// <returns></returns>
        public int ReSet()
        {
            pictureBox_Left.Image = Properties.Resources.left;
            pictureBox_Right.Image = Properties.Resources.right;
            return 3;
        }
        /// <summary>
        /// 停止采集图片
        /// </summary>
        /// <returns></returns>
        public int StopCapture()
        {
            cameraVideo.ShowVideo = false;
            pictureBox_Left.BackColor = Color.Black;
            cameraVideo.LeftFrame = null;
            cameraVideo.RightFrame = null;

            return 4;
        }
        /// <summary>
        /// 获取设备ID
        /// </summary>
        /// <returns></returns>
        public String GetDeviceSn()
        {

            return "5";
        }
        /// <summary>
        /// 关闭设备,关闭可见灯,关闭摄像机
        /// </summary>
        /// <returns></returns>
        public int IrisClose()
        {

            return 6;
        }
        
        //事件委托
        public delegate void CaptureHandler(string info);
        //基于上面的委托定义事件
        public event CaptureHandler CaptureEvent;
        //抓图事件返回
        public void Capture()
        {
            //处理协议,获取图片
            ResultParam resultParam = ResultParam.GetResultParam();
            
            string result = tools.JsonToString(resultParam);
            if (CaptureEvent != null)
            {
                CaptureEvent(result);
            }
        }
        
    }
}