码迷,mamicode.com
首页 > 其他好文 > 详细

Emgu.CV摄像头通用操作类

时间:2020-05-19 14:54:36      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:double   exception   main   ide   打开   dex   style   返回   trace   

  1  class CameraHelper
  2     {
  3         /// <summary>
  4         /// 连接摄像头数量
  5         /// </summary>
  6         public static int cameraCount = 0;
  7         /// <summary>
  8         /// 当前选择的摄像头Id
  9         /// </summary>
 10         public static int cameraId = 0;
 11         /// <summary>
 12         /// 摄像头对象
 13         /// </summary>
 14         public static VideoCapture _cameraCapture;
 15         public static bool _captureInProgress = false;
 16         public static  Mat _frame;
 17         public static Image<Bgr, byte> imgSource = null;
 18         public static Image<Bgr, byte> imgClone = null;
 19         /// <summary>
 20         /// 视频展示PictureBox句柄
 21         /// </summary>
 22         public static PictureBox picCapture;
 23         /// <summary>
 24         /// 打开摄像头
 25         /// </summary>
 26         /// <param name="width"></param>
 27         /// <param name="height"></param>
 28         /// <param name="a"></param>
 29         public static void OpenUSBCarmer(double width, double height,int a=0)
 30         {
 31             try
 32             {
 33                 if (_cameraCapture != null)
 34                 {
 35                     _cameraCapture.Stop();
 36                     _cameraCapture.Dispose();
 37                 }
 38                 _cameraCapture = returnCapture( width,  height,a);
 39                 //usb
 40                 _cameraCapture.ImageGrabbed += ProcessMainFrame;
 41 
 42                 //_cameraCapture = new VideoCapture(Common.GetAppConfig("url"));
 43                 //capture_tick.Start();
 44                 _cameraCapture.Start();
 45                 _captureInProgress = true;
 46             }
 47             catch (Exception ex)
 48             {
 49                 Log.WriteLog(new StackTrace(new StackFrame(true)), ex.Message);
 50             }
 51         }
 52         /// <summary>
 53         /// 连接摄像头,设置摄像头参数,并返回摄像头对象
 54         /// </summary>
 55         /// <param name="a">usb摄像头的话,可设置index=a</param>
 56         /// <returns>摄像头对象</returns>
 57         public static VideoCapture returnCapture( double width,double height, int a = 0)
 58         {
 59             VideoCapture capture;//摄像头对象
 60             try
 61             {
 62 
 63                 capture = new VideoCapture(a);
 64                 //Mat frame = new Mat();
 65                 double xx = capture.GetCaptureProperty(CapProp.FrameHeight);
 66                 double xw = capture.GetCaptureProperty(CapProp.FrameWidth);
 67                 //MessageBox.Show("h:" + xx + "w:" + xw+ "width:" + width + "height:" + height);
 68                 double xy = capture.GetCaptureProperty(CapProp.GigaFrameSensWidth);
 69                // 这里设置的分辨率是从配置文件读取分辨率
 70                 capture.SetCaptureProperty(CapProp.FrameWidth, 2448);
 71                 capture.SetCaptureProperty(CapProp.FrameHeight, 3264);
 72 
 73                // capture.SetCaptureProperty(CapProp.Fps, Common.GetInstance().Con_Model.FrameFPS);
 74                 return capture;
 75 
 76             }
 77             catch (Exception ex)
 78             {
 79                 Log.WriteLog(new StackTrace(new StackFrame(true)), ex.StackTrace);
 80                 return null;
 81             }
 82         }
 83 
 84         /// <summary>
 85         /// 对于视频流中的每一帧进行处理
 86         /// </summary>
 87         /// <param name="sender"></param>
 88         /// <param name="arg"></param>
 89       public static  void ProcessMainFrame(object sender, EventArgs arg)
 90         {
 91             if (_cameraCapture != null && _cameraCapture.Ptr != IntPtr.Zero)
 92             {
 93                 if (_captureInProgress)
 94                 {
 95                     _frame = new Mat();
 96                     _cameraCapture.Retrieve(_frame, 0);
 97                     if (_frame == null || _frame.IsEmpty)
 98                     {
 99                         return;
100                     }
101                     if (imgSource != null)
102                     {
103                         imgSource.Dispose();
104                     }
105                     imgSource = _frame.ToImage<Bgr, byte>();
106                     imgClone = imgSource.Copy();
107                     //将每一帧图片放到视频流框中显示
108                     picCapture.Image = imgSource.ToBitmap();
109                 }
110             }
111 
112         }
113         /// <summary>
114         /// 停止获取摄像头画面
115         /// </summary>
116         public static void StopCamera()
117         {
118             _cameraCapture.Stop();
119             _cameraCapture.Dispose();
120         }
121         /// <summary>
122         /// 获取本地摄像头列表(例如笔记本上有自身摄像头也有USB连接的摄像头)
123         /// </summary>
124         public static void GetCameras()
125         {
126             cameraCount = 0;
127             for (int i = 0; i < 5; i++)
128             {
129                 VideoCapture videoCapture = new VideoCapture(i);
130                 if (videoCapture.IsOpened)
131                 {
132                     cameraCount++;
133                 }
134                 videoCapture.Dispose();
135             }
136         }
137     }

操作类使用方法:

1 CameraHelper.picCapture = this.picCapture;
2 CameraHelper.OpenUSBCarmer(this .picCapture.Width,this .picCapture.Height);

 

Emgu.CV摄像头通用操作类

标签:double   exception   main   ide   打开   dex   style   返回   trace   

原文地址:https://www.cnblogs.com/Betty-IT/p/12916520.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!