| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Windows.Forms;
- using AForge.Video;
- using AForge.Video.DirectShow;
- using AForge.Controls;
- using System.Threading;
- namespace SHJX.Service.Common.Camera
- {
- public class WebCamera
- {
- #region privates fields
- public VideoCaptureDevice vidCapDevice { get; set; }
- private VideoSourcePlayer vidPlayer { get; set; }
- private Bitmap _currFrame;
- #endregion
- #region static
- static FilterInfoCollection vidCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
- public static int GetCameraCounts()
- {
- return vidCollection.Count;
- }
- public static string GetCameraMoniker(int index)
- {
- return vidCollection[index].MonikerString;
- }
- public static string GetCameraName(int index)
- {
- return vidCollection[index].Name;
- }
- #endregion
- #region events properties
- /// <summary>
- /// 画面刷新事件
- /// </summary>
- public event NewFrameEventHandler NewFrameEvent;
- /// <summary>
- /// 当前画面,相机开启时有效
- /// </summary>
- public Bitmap CurrFrame
- {
- get => vidCapDevice is not null ? _currFrame.Clone() as Bitmap : null;
- protected set
- {
- _currFrame = value;
- }
- }
- #endregion
- #region public methods
- /// <summary>
- /// 摄像头是否已打开
- /// </summary>
- public bool IsOpened
- {
- get
- {
- bool isOpened = false;
- if (vidPlayer == null)
- {
- return isOpened;
- }
- if (vidPlayer.InvokeRequired)
- {
- vidPlayer.Invoke(new Action(() =>
- {
- isOpened = vidPlayer.IsRunning;
- }));
- }
- else
- {
- isOpened = vidPlayer.IsRunning;
- }
- return isOpened;
- }
- }
- /// <summary>
- /// 初始化摄像头
- /// </summary>
- /// <param name="cameraMoniker">摄像头名称</param>
- public WebCamera(string cameraMoniker)
- {
- vidCapDevice = new VideoCaptureDevice(cameraMoniker);
- vidCapDevice.NewFrame += GetCurrFrame;
- vidPlayer = new VideoSourcePlayer
- {
- VideoSource = vidCapDevice
- };
- _currFrame = null;
- }
- public WebCamera()
- {
- vidCapDevice = new VideoCaptureDevice();
- vidPlayer = new VideoSourcePlayer
- {
- VideoSource = vidCapDevice
- };
- _currFrame = null;
- }
- /// <summary>
- /// 连接指定的摄像头,并打开视频信号
- /// </summary>
- /// <param name="cameraMoniker"></param>
- /// <returns></returns>
- public bool OpenCamera(string cameraMoniker)
- {
- this.vidCapDevice.Source = cameraMoniker;
- vidCapDevice.NewFrame += this.GetCurrFrame;
- this.vidCapDevice.Start();
- return this.vidCapDevice.IsRunning;
- }
- /// <summary>
- /// 设置显示摄像头图像区域
- /// </summary>
- /// <param name="parentControl"></param>
- /// <param name="left"></param>
- /// <param name="top"></param>
- /// <param name="width"></param>
- /// <param name="height"></param>
- public void ShowControl(ref Panel parentControl, int left, int top, int width, int height)
- {
- if (vidPlayer == null) return;
- parentControl.Controls.Add(vidPlayer);
- vidPlayer.Location = new Point(left, top);
- vidPlayer.Size = new Size(width, height);
- vidPlayer.Visible = true;
- }
- /// <summary>
- /// 显示摄像头属性设置窗体
- /// </summary>
- /// <param name="parentWindow"></param>
- public void ShowProperties(IntPtr parentWindow)
- {
- vidCapDevice.DisplayPropertyPage(parentWindow);
- }
- /// <summary>
- /// 设置相机属性值
- /// </summary>
- /// <param name="prop"></param>
- /// <param name="val"></param>
- /// <param name="flags"></param>
- public void SetProperties(CameraControlProperty prop, int val, CameraControlFlags flags)
- {
- vidCapDevice.SetCameraProperty(prop, val, flags);// 取消自动曝光
- }
- /// <summary>
- /// 读取当前属性值
- /// </summary>
- /// <param name="prop"></param>
- /// <param name="val"></param>
- /// <param name="flags"></param>
- public void GetProperties(CameraControlProperty prop, out int val, out CameraControlFlags flags)
- {
- vidCapDevice.GetCameraProperty(prop, out val, out flags);
- }
- /// <summary>
- /// 读取相机参数的取值范围
- /// </summary>
- /// <param name="prop"></param>
- /// <param name="minValue"></param>
- /// <param name="maxValue"></param>
- /// <param name="stepSize"></param>
- /// <param name="defaultValue"></param>
- /// <param name="flags"></param>
- public void GetPropertiesRange(CameraControlProperty prop, out int minValue, out int maxValue, out int stepSize, out int defaultValue, out CameraControlFlags flags)
- {
- vidCapDevice.GetCameraPropertyRange(prop, out minValue, out maxValue, out stepSize, out defaultValue, out flags);
- }
- /// <summary>
- /// 设置视频属性值,亮度、对比度、白平衡、增益等
- /// </summary>
- /// <param name="prop"></param>
- /// <param name="val"></param>
- /// <param name="flags"></param>
- public void SetVideoProcAmpProperties(VideoProcAmpProperty prop, int val, VideoProcAmpFlags flags)
- {
- vidCapDevice.SetVideoProcAmpProperty(prop, val, flags);
- }
- /// <summary>
- /// 读取当前视频参数
- /// </summary>
- /// <param name="prop"></param>
- /// <param name="val"></param>
- /// <param name="flags"></param>
- public void GetVideoProcAmpProperties(VideoProcAmpProperty prop, out int val, out VideoProcAmpFlags flags)
- {
- vidCapDevice.GetVideoProcAmpProperty(prop, out val, out flags);
- }
- /// <summary>
- /// 读取视频参数的取值范围
- /// </summary>
- /// <param name="prop"></param>
- /// <param name="minValue"></param>
- /// <param name="maxValue"></param>
- /// <param name="stepSize"></param>
- /// <param name="defaultValue"></param>
- /// <param name="flags"></param>
- public void GetVideoProcAmpPropertiesRange(VideoProcAmpProperty prop, out int minValue, out int maxValue, out int stepSize, out int defaultValue, out VideoProcAmpFlags flags)
- {
- vidCapDevice.GetVideoProcAmpPropertyRange(prop, out minValue, out maxValue, out stepSize, out defaultValue, out flags);
- }
- /// <summary>
- /// 返回视频支持的分辨率
- /// </summary>
- /// <returns></returns>
- public List<string> GetVideoResolution()
- {
- List<string> resolution = new List<string>();
- foreach (var cap in vidCapDevice.VideoCapabilities)
- {
- resolution.Add(string.Format("{0} x {1}", cap.FrameSize.Width, cap.FrameSize.Height));
- }
- return resolution;
- }
- /// <summary>
- /// 设定视频的分辨率,index可以通过GetVideoResolution()获取所有支持的分辨率来确定
- /// </summary>
- /// <param name="index">在所有支持分辨率中的序号</param>
- public void SetVideoResolution(int index)
- {
- if (vidCapDevice.VideoCapabilities.Length > index)
- {
- vidCapDevice.VideoResolution = vidCapDevice.VideoCapabilities[index];
- }
- }
- /// <summary>
- /// 打开摄像头
- /// </summary>
- /// <returns></returns>
- public bool OpenCamera()
- {
- if (vidCapDevice != null && vidCapDevice.IsRunning == false)
- {
- vidCapDevice.NewFrame += NewFrameEvent;
- vidCapDevice.Start();
- }
- if (vidPlayer.InvokeRequired)
- {
- vidPlayer.Invoke(new Action(() =>
- {
- if (vidPlayer != null && !vidPlayer.IsRunning)
- {
- vidPlayer.Start();
- }
- }));
- }
- else
- {
- if (vidPlayer != null && !vidPlayer.IsRunning)
- {
- vidPlayer.Start();
- }
- }
- return true;
- }
- /// <summary>
- /// 关闭摄像头
- /// </summary>
- public void CloseCamera()
- {
- if (vidCapDevice != null && vidCapDevice.IsRunning)
- {
- vidCapDevice.Stop();
- vidCapDevice.NewFrame -= NewFrameEvent;
- }
- if (vidPlayer.InvokeRequired)
- {
- vidPlayer.Invoke(new Action(() =>
- {
- if (vidPlayer == null || !vidPlayer.IsRunning) return;
- vidPlayer.SignalToStop();
- vidPlayer.WaitForStop();
- }));
- }
- else
- {
- if (vidPlayer == null || !vidPlayer.IsRunning) return;
- vidPlayer.SignalToStop();
- vidPlayer.WaitForStop();
- }
- }
- /// <summary>
- /// 拍照
- /// </summary>
- /// <returns></returns>
- public Bitmap GrabImage()
- {
- Bitmap bmp = null;
- if (vidPlayer.InvokeRequired)
- {
- vidPlayer.Invoke(new Action(() =>
- {
- if (vidPlayer.IsRunning)
- {
- bmp = vidPlayer.GetCurrentVideoFrame();
- }
- else
- {
- // 探头未连接,重试
- int cnt = 0; // 重试10秒
- while (!vidPlayer.IsRunning && cnt < 100)
- {
- vidPlayer.Start();
- Thread.Sleep(100);
- cnt++;
- //PublicDatas.LogProgram.Error("摄像头未连接,正在重试" + cnt.ToString());
- }
- Thread.Sleep(300);
- bmp = vidPlayer.GetCurrentVideoFrame();
- }
- }));
- }
- else
- {
- if (vidPlayer.IsRunning)
- {
- bmp = vidPlayer.GetCurrentVideoFrame();
- }
- else
- {
- // 探头未连接,重试
- int cnt = 0; // 重试10秒
- while (!vidPlayer.IsRunning && cnt < 100)
- {
- vidPlayer.Start();
- Thread.Sleep(100);
- cnt++;
- // PublicDatas.LogProgram.Error("摄像头未连接,正在重试" + cnt.ToString());
- }
- Thread.Sleep(300);
- bmp = vidPlayer.GetCurrentVideoFrame();
- }
- }
- return bmp;
- }
- /// <summary>
- /// 拍照,返回CurrFrame图像
- /// </summary>
- /// <returns></returns>
- public Bitmap GrabImage2()
- {
- return CurrFrame;
- }
- /// <summary>
- /// 实时将新的画面存入CurrFrame对象中
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- public void GetCurrFrame(object sender, NewFrameEventArgs e)
- {
- try
- {
- if (e.Frame != null)
- {
- CurrFrame = e.Frame;
- }
- }
- catch (Exception)
- {
- // ignored
- }
- }
- #endregion
- }
- }
|