拍照
1. 获取视频输入设备
// 获取所有摄像头设备
public FilterInfoCollection GetDevices()
{
try
{
//枚举所有视频输入设备
FilterInfoCollection videoDevice = new
FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevice.Count != 0)
{
}
return videoDevice;
else
return null;
{
}
}
catch
return null;
{
}
}
2. 加载视频输入设备
private FilterInfoCollection _videoDevices;
private VideoCaptureDevice _videoSource;
private VideoCapabilities _videoCapability;
private int _selectedDeviceIndex = 0;
private string drawDate = string.Empty;
//加载
private void InitDevice()
{
_videoDevices = GetDevices();
if (_videoDevices == null || _videoDevices.Count <= 0)
{
cmbVideoDevices.Items.Add("未检查到摄像头");
cmbVideoDevices.SelectedIndex = 0;
_msg = "摄像头异常!";
btnConnection.Enabled = false;
}
else
{
cmbVideoDevices.Items.Clear();
foreach (FilterInfo device in _videoDevices)
{
}
cmbVideoDevices.Items.Add(device.Name);
cmbVideoDevices.SelectedIndex = 0;
_videoSource = VideoConnect(0, 0);
if (_videoSource == null)
{
}
return;
_videoCapability = _videoSource.VideoCapabilities[0];
}
}
3.
//打开视频设备
private void OpenCamers()
{
}
if (_videoDevices == null || _videoDevices.Count <= 0)
{
}
return;
_videoSource = VideoConnect(0, 0);
//打开视频设备
if (_videoSource == null)
{
}
return;
if (!videoSourcePlayer1.IsRunning)
{
}
_videoSource.VideoResolution = _videoCapability;
videoSourcePlayer1.VideoSource = _videoSource;
videoSourcePlayer1.Start();
4.
//拍照并在PictureBox上显示
public bool Photograph(string photoName)
{
}
if (videoSourcePlayer1.IsRunning)
{
_videoSource.VideoResolution = _videoCapability;
if (photoName == null)
{
}
photoName = (DateTime.Now).ToString("yyMMddHHmmssff");
Bitmap bitmap = videoSourcePlayer1.GetCurrentVideoFrame();
Bitmap _bitmap = (Bitmap)bitmap.Clone();
pbShow.Image = _bitmap;
ImageFormat imageFormat = ImageFormat.Jpeg;
bitmap.Save(photoDir + photoName + ".JPEG", imageFormat);
bitmap.Dispose();
return true;
}
return false;
5.
// 连接视频摄像头
public VideoCaptureDevice VideoConnect(int deviceIndex, int resolutionIndex)
{
if (_videoDevices.Count <= 0)
{
}
return null;
_selectedDeviceIndex = deviceIndex;
VideoCaptureDevice videoSource = new
VideoCaptureDevice(_videoDevices[deviceIndex].MonikerString);
videoSource.VideoResolution = videoSource.VideoCapabilities[resolutionIndex];
return videoSource;
}
在页面上需要的引用:
using AForge.Video.DirectShow;
using System.IO;
using System.Drawing.Imaging;
主要是 AForge 类库的引用,此类库是非常强大的调用视频输入设备类库,在使用 AForge 类库的视频拍照
功能时,需要将 AForge 类库中的 AForge.Controls.DLL 在工具箱中加载,显示出类库自定义的
视频输出控件 VideoSourcePlayer(在项目外将 AForge.Controls.DLL 拖入工具箱即可),如下图:
然后将用户自定义控件 VideoSourcePlayer 拖入页面。
视频录制源码已完善,在拍照的基础上只将拍照方法改成视频录制方法:
视频
///
/// 视频录制方法
///
///
///
private void videoSourcePlayer1_NewFrame(object sender, ref Bitmap image)
{
//录像
Graphics g = Graphics.FromImage(image);
SolidBrush drawBrush = new SolidBrush(Color.Blue);
Font drawFont = new Font("Arial", 6, FontStyle.Bold, GraphicsUnit.Millimeter);
int xPos = image.Width - (image.Width - 15);
int yPos = 10;
//写到屏幕上的时间
drawDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
g.DrawString(drawDate, drawFont, drawBrush, xPos, yPos);
if (_stopREC)
{
_stopREC = true;
_createNewFile = true;
//这里要设置为true表示要创建新文件
if (_videoWriter != null)
_videoWriter.Close();
{
}
}
else
{
//开始录像
if (_createNewFile)
{
videoFileFullPath = videoDir + _videoName + ".avi";
_createNewFile = false;
if (_videoWriter != null)
{
}
_videoWriter.Close();
_videoWriter.Dispose();
_videoWriter = new VideoFileWriter();
//这里必须是全路径,否则会默认保存到程序运行根据录下了
_videoWriter.Open(videoFileFullPath, image.Width, image.Height,
_frameRate, VideoCodec.MPEG4);
}
_videoWriter.WriteVideoFrame(image);
}
}
此方法是 VideoSourcePlayer 的事件:NewFrame,视频录制主要是_videoWriter,有该对象将录制的资源写
入视频文件,详情见代码。
DLL 与实例:
拍照视频.rar