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
///
/// 画面刷新事件
///
public event NewFrameEventHandler NewFrameEvent;
///
/// 当前画面,相机开启时有效
///
public Bitmap CurrFrame
{
get => vidCapDevice is not null ? _currFrame.Clone() as Bitmap : null;
protected set
{
_currFrame = value;
}
}
#endregion
#region public methods
///
/// 摄像头是否已打开
///
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;
}
}
///
/// 初始化摄像头
///
/// 摄像头名称
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;
}
///
/// 连接指定的摄像头,并打开视频信号
///
///
///
public bool OpenCamera(string cameraMoniker)
{
this.vidCapDevice.Source = cameraMoniker;
vidCapDevice.NewFrame += this.GetCurrFrame;
this.vidCapDevice.Start();
return this.vidCapDevice.IsRunning;
}
///
/// 设置显示摄像头图像区域
///
///
///
///
///
///
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;
}
///
/// 显示摄像头属性设置窗体
///
///
public void ShowProperties(IntPtr parentWindow)
{
vidCapDevice.DisplayPropertyPage(parentWindow);
}
///
/// 设置相机属性值
///
///
///
///
public void SetProperties(CameraControlProperty prop, int val, CameraControlFlags flags)
{
vidCapDevice.SetCameraProperty(prop, val, flags);// 取消自动曝光
}
///
/// 读取当前属性值
///
///
///
///
public void GetProperties(CameraControlProperty prop, out int val, out CameraControlFlags flags)
{
vidCapDevice.GetCameraProperty(prop, out val, out flags);
}
///
/// 读取相机参数的取值范围
///
///
///
///
///
///
///
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);
}
///
/// 设置视频属性值,亮度、对比度、白平衡、增益等
///
///
///
///
public void SetVideoProcAmpProperties(VideoProcAmpProperty prop, int val, VideoProcAmpFlags flags)
{
vidCapDevice.SetVideoProcAmpProperty(prop, val, flags);
}
///
/// 读取当前视频参数
///
///
///
///
public void GetVideoProcAmpProperties(VideoProcAmpProperty prop, out int val, out VideoProcAmpFlags flags)
{
vidCapDevice.GetVideoProcAmpProperty(prop, out val, out flags);
}
///
/// 读取视频参数的取值范围
///
///
///
///
///
///
///
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);
}
///
/// 返回视频支持的分辨率
///
///
public List GetVideoResolution()
{
List resolution = new List();
foreach (var cap in vidCapDevice.VideoCapabilities)
{
resolution.Add(string.Format("{0} x {1}", cap.FrameSize.Width, cap.FrameSize.Height));
}
return resolution;
}
///
/// 设定视频的分辨率,index可以通过GetVideoResolution()获取所有支持的分辨率来确定
///
/// 在所有支持分辨率中的序号
public void SetVideoResolution(int index)
{
if (vidCapDevice.VideoCapabilities.Length > index)
{
vidCapDevice.VideoResolution = vidCapDevice.VideoCapabilities[index];
}
}
///
/// 打开摄像头
///
///
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;
}
///
/// 关闭摄像头
///
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();
}
}
///
/// 拍照
///
///
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;
}
///
/// 拍照,返回CurrFrame图像
///
///
public Bitmap GrabImage2()
{
return CurrFrame;
}
///
/// 实时将新的画面存入CurrFrame对象中
///
///
///
public void GetCurrFrame(object sender, NewFrameEventArgs e)
{
try
{
if (e.Frame != null)
{
CurrFrame = e.Frame;
}
}
catch (Exception)
{
// ignored
}
}
#endregion
}
}