c#基@于@A@F@@o@rg@e类@库@实@现@摄@像@头@控@制

来源:互联网 发布:创冰 足球球员数据 编辑:程序博客网 时间:2024/04/28 09:52


首先引入AForge库文件

         




程序片段:

///枚举视频输入设备

try
            {
                // 枚举所有视频输入设备
                FilterInfoCollection  videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);


                if (videoDevices.Count == 0)
                    throw new ApplicationException();


                foreach (FilterInfo device in videoDevices)
                {
                    tscbxCameras.Items.Add(device.Name);
                }


                tscbxCameras.SelectedIndex = 0;
            }
            catch (ApplicationException)
            {
                tscbxCameras.Items.Add("No local capture devices");
                videoDevices = null;
            }



 /////连接

   VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[tscbxCameras.SelectedIndex].MonikerString);
            videoSource.DesiredFrameSize = new Size(320, 240);
            videoSource.DesiredFrameRate = 1;


            videPlayer.VideoSource = videoSource;  //videPlayer为AForge.Controls.VideoSourcePlayer控件
            videPlayer.Start();


/////关闭

  videPlayer.SignalToStop();
            videPlayer.WaitForStop();


/////抓图

 private void button3_Click(object sender, EventArgs e)
        {
            Bitmap _bitmap= _videoSourcePlayer.GetCurrentVideoFrame();
            _bitmap.Save(@"C:\Users\yangpeng\Documents\Visual Studio 2013\Projects\WindowsFormsApplication33\WindowsFormsApplication33\bin\a.bmp",ImageFormat.Bmp);
        }


、、、、、=======================

private void button1_Click(object sender, EventArgs e)
        {
            _videoCaptureDevice = new VideoCaptureDevice(g_filterInfo.MonikerString);
            _videoCaptureDevice.DesiredFrameRate = 1;
            _videoSourcePlayer = new VideoSourcePlayer();
            _videoSourcePlayer.VideoSource = _videoCaptureDevice;
            _videoSourcePlayer.Size = new System.Drawing.Size(320,240);
            _videoSourcePlayer.Show();
            this.Controls.Add(_videoSourcePlayer);
            //_videoCaptureDevice.NewFrame += new NewFrameEventHandler(NewFrameEventHandl);
           
                
            //_videoSourcePlayer.NewFrame += new VideoSourcePlayer.NewFrameHandler(NewFrame);
          
            _videoSourcePlayer.Start();
            //g_videoFileWriter = new VideoFileWriter();
            //g_videoFileWriter.Open(@"C:\Users\yangpeng\Documents\Visual Studio 2013\Projects\WindowsFormsApplication33\WindowsFormsApplication33\bin\De.avi", 320, 240, 10, VideoCodec.MPEG4);
           
        }

可能的问题:

1、

混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行

 (2012-03-21 16:30:30)
转载
标签: 

杂谈

  

在使用.Net2.0和.Net4.0混合开发,启动应用程序时遇到了“混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集。”这个错误提示。解决方案如下:

在app.config中的configuration节内添加子节Startup,详细如下:

<startup useLegacyV2RuntimeActivationPolicy="true">
   
 <supportedRuntime version="v4.0" sku = ".NETFramework,Version=v4.0"/>
   
 <supportedRuntime version="v2.0.50727"/>

</startup>

,之后完整的app.config看起来就像这样:

<?xml version="1.0"?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku = ".NETFramework,Version=v4.0"/>
    <supportedRuntime version="v2.0.50727"/>
  </startup>
</configuration>

2、

未能加载文件或程序集“AForge.Video.FFMPEG.dll”或它的某一个依赖项。找不到指定的模块

如果是64位的话,就出来这个问题。编译的时候,请选择X86.

使用AForge.Video.FFMPEG的命名空间,用来保存视频编码,其在底层用了ffmpeg的类库,所以运行程序时需要avcodec-53.dll等类库在场。你可以从AForge的类库包的Externals\ffmpeg\bin中找到这些文件。

首先你的把 avcodec-53.dll  ,avdevice-53.dll,avfilter-2.dll ,avformat-53.dll ,avutil-51.dll,swresample-0.dll,swscale-2.dll这7个DLL 复制到调试目录下面。你知道这几个文件怎么找吧。然后
工具---选项----调试---常规----勾选使用托管兼容模式

最近使用aforge.NET拍照录像功能实现

记录一下以便以后好学习,哈哈,直接上代码

连接摄像头设备,这里需要引入

AForge.Video;

AForge.Video.DirectShow;

AForge.Video.FFMPEG;

还需要添加引用,aforge.dll,aforge.control,

在工具箱中还需要添加AForge.Control,然后找到VideoSourcePlayer这个控件添加到界面上

然后定义变量

private FilterInfoCollection videoDevices;
private VideoCaptureDevice videoSource;

private bool stopREC = true;
private bool createNewFile = true;

private string videoFileFullPath = string.Empty; //视频文件全路径
private string imageFileFullPath = string.Empty; //图像文件全路径
private string videoPath = @"E:\video\"; //视频文件路径
private string imagePath = @"E:\video\images\"; //图像文件路径
private string videoFileName = string.Empty; //视频文件名
private string imageFileName = string.Empty; //图像文件名
private string drawDate = string.Empty;
private VideoFileWriter videoWriter = null;

public delegate void MyInvoke(); //定义一个委托方法

string g_s_AutoSavePath = AppDomain.CurrentDomain.BaseDirectory + "Capture\\";
object objLock = new object(); //定义一个对象的锁
int frameRate = 20; //默认帧率
private Stopwatch stopWatch = null;
IVideoSource iVideoSource = null;

复制代码
private void InitUI(){    //连接       //开启摄像头   videoDevices = vh.GetDevices();    if (videoDevices != null && videoDevices.Count > 0)    {        videoSource = vh.VideoConnect();    }    videoSourcePlayer1.VideoSource = videoSource;    videoSourcePlayer1.Start();}
复制代码

开始录像

复制代码
 private void btnStartVideotape_Click(object sender, EventArgs e)        {            //开始录像            if (btnStartVideotape.Text == "开始录像")            {                stopREC = false;                frameRate = Convert.ToInt32(txtFrameRate.Text.Trim());                btnStartVideotape.Text = "停止录像";            }            else if (btnStartVideotape.Text == "停止录像")            {                stopREC = true;                btnStartVideotape.Text = "开始录像";            }        }
复制代码

添加aforge.Net的一个VideoSourcePlayer控件之后找到NewFrame事件,代码如下:

下面是控件的一个事件,是真正录像的代码

复制代码
private void videoSourcePlayer1_NewFrame(object sender, ref Bitmap image)        {            //录像            Graphics g = Graphics.FromImage(image);            SolidBrush drawBrush = new SolidBrush(Color.Yellow);            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 (!Directory.Exists(videoPath))                Directory.CreateDirectory(videoPath);            //创建文件路径            //fileFullPath = path + fileName;            if (stopREC)            {                stopREC = true;                createNewFile = true;  //这里要设置为true表示要创建新文件                if (videoWriter != null)                    videoWriter.Close();            }            else            {                //开始录像                if (createNewFile)                {                    videoFileName = DateTime.Now.ToString("yyyy.MM.dd HH.mm.ss") + ".avi";                    videoFileFullPath = videoPath + videoFileName;                    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);                }                else                {                    videoWriter.WriteVideoFrame(image);                }            }        }
复制代码

拍照代码

复制代码
/// <summary>/// 手动拍照或抓图/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnCapture_Click(object sender, EventArgs e){    try    {        int number=0;        number++;        string fileImageName = g_s_RequestNo + "-" + number + ".bmp";        string fileCapturePath = g_s_AutoSavePath + g_s_RequestNo + "\\";        if (!Directory.Exists(fileCapturePath))            Directory.CreateDirectory(fileCapturePath);        //抓到图保存到指定路径        Bitmap bmp = null;        bmp = videoSourcePlayer1.GetCurrentVideoFrame();        if (bmp == null)        {            MessageBox.Show("捕获图像失败!", "提示");            return;        }                bmp.Save(fileCapturePath + fileImageName, ImageFormat.Bmp);                }    catch (Exception ex)    {        MessageBox.Show("捕获图像失败!" + ex.Message, "提示");    }}
复制代码

//============

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AForge;
using AForge.Controls;
using AForge.Imaging;
using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Video.FFMPEG;
using System.Drawing.Imaging;
using System.IO;



namespace WindowsFormsApplication33
{
    public partial class Form1 : Form
    {
       
        public Form1()
        {
            InitializeComponent();
        }
        List<string> g_listStringVideoDevices = new List<string>();
        FilterInfo g_filterInfo = null;
        private void Form1_Load(object sender, EventArgs e)
        {
            FilterInfoCollection _filterInfoCollect = null;
            _filterInfoCollect = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            foreach (FilterInfo item in _filterInfoCollect)
            {
                g_listStringVideoDevices.Add(item.Name);
            }
            g_filterInfo = _filterInfoCollect[0];
        }


        VideoSourcePlayer _videoSourcePlayer = null;
        VideoCaptureDevice _videoCaptureDevice = null;
        private void button1_Click(object sender, EventArgs e)
        {
            _videoCaptureDevice = new VideoCaptureDevice(g_filterInfo.MonikerString);
            _videoCaptureDevice.DesiredFrameRate = 1;
            _videoSourcePlayer = new VideoSourcePlayer();
            _videoSourcePlayer.VideoSource = _videoCaptureDevice;
            _videoSourcePlayer.Size = new System.Drawing.Size(320,240);
            _videoSourcePlayer.Show();
            this.Controls.Add(_videoSourcePlayer);
           
                
            _videoSourcePlayer.NewFrame += new VideoSourcePlayer.NewFrameHandler(NewFrame);
          
            _videoSourcePlayer.Start();
            g_videoFileWriter = new VideoFileWriter();
            g_videoFileWriter.Open(@"C:\Users\yangpeng\Documents\Visual Studio 2013\Projects\WindowsFormsApplication33\WindowsFormsApplication33\bin\De.avi",320,240);
           
        }


        private void button2_Click(object sender, EventArgs e)
        {


            _videoCaptureDevice.SignalToStop();
            _videoCaptureDevice.WaitForStop();
            _videoSourcePlayer.Stop();
            g_videoFileWriter.Close();
        }


        VideoFileWriter g_videoFileWriter = null;
        bool createNewFile = true;
        //录像
        void NewFrame(object sender, ref Bitmap image)
        {
                //开始录像
                if (createNewFile)
                {
                   
                    createNewFile = false;
                    if (g_videoFileWriter != null)
                    {
                        g_videoFileWriter.Close();
                        g_videoFileWriter.Dispose();
                    }
                    g_videoFileWriter = new VideoFileWriter();
                    //这里必须是全路径,否则会默认保存到程序运行根据录下了
                    g_videoFileWriter.Open(@"C:\Users\yangpeng\Documents\Visual Studio 2013\Projects\WindowsFormsApplication33\WindowsFormsApplication33\bin\ddd.avi", image.Width, image.Height, 20, VideoCodec.MPEG4);
                    g_videoFileWriter.WriteVideoFrame(image);
                }
                else
                {
                    g_videoFileWriter.WriteVideoFrame(image);
                }
           
        }


        //抓图
        private void button3_Click(object sender, EventArgs e)
        {
            Bitmap _bitmap= _videoSourcePlayer.GetCurrentVideoFrame();
            _bitmap.Save(@"C:\Users\yangpeng\Documents\Visual Studio 2013\Projects\WindowsFormsApplication33\WindowsFormsApplication33\bin\a.bmp",ImageFormat.Bmp);
        }


    }
}

//========================================================

0 0