当前位置:编程学习 > C#/ASP.NET >>

C# 如何采集摄像头的视频,然后通过编码器和解码器在计算机上生成视频?回复有分,谢谢!

我是刚开始要开发视频软件的,现在要用C# 写个程序采集摄像头(云台)的视频,然后通过编码器和解码器在计算机上生成视频,请有过这方面知识的人多多指教!谢谢!!希望在做这个项目的人留下联系方式!! --------------------编程问答-------------------- 估计LZ你要找这样的效果吧
http://www.codeproject.com/cs/media/DirXVidStrm.asp --------------------编程问答-------------------- http://www.codeproject.com/cs/media/cameraviewer.asp --------------------编程问答-------------------- 调用驱动程序 API 获取图像
使用编解码 API 生成文件 --------------------编程问答-------------------- ... --------------------编程问答-------------------- 学习,帮顶 --------------------编程问答-------------------- DirectShowLib.dll --------------------编程问答-------------------- 帮顶 --------------------编程问答-------------------- 关注 --------------------编程问答-------------------- mark! --------------------编程问答-------------------- 1 如果摄像头是接在USB上那就应该是串口操作。
2 那么你得获得串口的波特比。
3 serialport就对了很有用了,你可以读出它的数据流来。
4 获得数据流你还得知道它的编码方式,就其转换成目的格式。 --------------------编程问答-------------------- up --------------------编程问答-------------------- 文件:VedioCapture2.cs:
using System;
using System.Runtime.InteropServices;

namespace Wuyin.ShoesManager
{
/// <summary>
/// VedioCapture 的摘要说明。
/// </summary>
public class VedioCapture
{
private int hCaptureM;
private bool isUnLoad = false;
public VedioCapture()
{        
}
[DllImport("avicap32.dll")]
private static extern int capCreateCaptureWindow( string strWindowName, int dwStyle, int x, int y ,int width, int height , int hwdParent, int nID );
[DllImport("user32.dll")]
private static extern int SendMessage( int hwnd , int wMsg, int wParam , int lParam );
[DllImport("user32.dll")]
private static extern int SendMessage( int hwnd , int wMsg, int wParam , string lParam );
[DllImport("Kernel32.dll")]
private static extern bool CloseHandle( int hObject );
public bool Initialize( System.Windows.Forms.Control aContainer , int intWidth, int intHeight )
{
hCaptureM = capCreateCaptureWindow( "", 0x40000000 | 0x10000000, 0,0,intWidth,intHeight,aContainer.Handle.ToInt32() ,1 );
if( hCaptureM == 0 ) return false;
            
int ret = SendMessage( hCaptureM , 1034, 0,0 );
if( ret == 0 )
{
CloseHandle(hCaptureM);
return false;
}
//WM_CAP_SET_PREVIEW
ret = SendMessage( hCaptureM, 1074, 1, 0 );
if( ret == 0 )
{
this.UnLoad();
return false;
}
//WM_CAP_SET_SCALE
ret = SendMessage( hCaptureM, 1077, 1, 0 );
if( ret == 0 )
{
this.UnLoad();
return false;
}
//WM_CAP_SET_PREVIEWRATE
ret = SendMessage( hCaptureM, 1076, 66, 0 );
if( ret == 0 )
{
this.UnLoad();
return false;
}
return true;
}

public void SingleFrameBegin()
{
//
int ret = SendMessage( hCaptureM, 1094 , 0, 0 );
}
public void SingleFrameEnd()
{
//
int ret = SendMessage( hCaptureM, 1095 , 0, 0 );
}

public void SingleFrameMode()
{
//WM_CAP_GRAB_FRAME
int ret = SendMessage(  hCaptureM, 1084 , 0, 0 );
//WM_CAP_SET_PREVIEW 
//int ret = SendMessage(  hCaptureM, 1074 , 0, 0 );
//WM_CAP_SINGLE_FRAME
//ret = SendMessage( hCaptureM, 1096 , 0, 0 );
}
public void PreviewMode()
{
int ret = SendMessage( hCaptureM, 1074 , 1, 0 );
}        

public void UnLoad()
{
int ret = SendMessage( hCaptureM, 1035, 0, 0 );
CloseHandle( this.hCaptureM );
isUnLoad = true;
}

public void CopyToClipBorad()
{
int ret = SendMessage(  hCaptureM, 1054, 0, 0 );
}

public void ShowFormatDialog()
{
int ret = SendMessage(  hCaptureM, 1065, 0, 0 );
}
public void SaveToDIB( string fileName )
{
int ret = SendMessage(  hCaptureM, 1049, 0, fileName );
}

public void ShowDisplayDialog()
{
int ret = SendMessage( hCaptureM, 1067, 0, 0 );
}
public System.Drawing.Image getCaptureImage()
{
System.Windows.Forms.IDataObject iData = System.Windows.Forms.Clipboard.GetDataObject();
System.Drawing.Image retImage = null;
if( iData !=  null ) 
{
if( iData.GetDataPresent( System.Windows.Forms.DataFormats.Bitmap ) )
{
retImage = (System.Drawing.Image)iData.GetData( System.Windows.Forms.DataFormats.Bitmap );

else if( iData.GetDataPresent( System.Windows.Forms.DataFormats.Dib  ) )
{
retImage = (System.Drawing.Image)iData.GetData( System.Windows.Forms.DataFormats.Dib );
}
}
return retImage;
}

~VedioCapture()
{
if( !isUnLoad )
{
this.UnLoad();
}
}
}
}
--------------------编程问答-------------------- 窗体的cs:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using webcam;
using Wuyin.ShoesManager;
using PickHead;


namespace sxt
{
/// <summary>
/// 摄像头采集保存照片程序。
/// VedioCapture.cs和VedioCapture2.cs是两种不同的摄像头采集代码。
/// 本程序用的是VedioCapture2.cs的方法,注释掉的部分是用VedioCapture.cs的方法
/// int64@163.com
/// 2005年3月28日
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button b_play;
private System.Windows.Forms.Panel panelPreview;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button b_stop;
private System.Windows.Forms.Button b_close;
private System.Windows.Forms.Button b_getpic;
private System.Windows.Forms.PictureBox pictureBox1;
WebCamera wc;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
VedioCapture xxx=new VedioCapture();
private System.Windows.Forms.TextBox t_out;
int savefiles;
private System.Windows.Forms.Button b_save;
private System.Windows.Forms.Button b_savepic;
private System.Windows.Forms.Button button1;
string filepath;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.b_play = new System.Windows.Forms.Button();
this.b_stop = new System.Windows.Forms.Button();
this.panelPreview = new System.Windows.Forms.Panel();
this.b_close = new System.Windows.Forms.Button();
this.b_getpic = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.b_save = new System.Windows.Forms.Button();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.t_out = new System.Windows.Forms.TextBox();
this.b_savepic = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
// 
// b_play
// 
this.b_play.Enabled = false;
this.b_play.Location = new System.Drawing.Point(8, 312);
this.b_play.Name = "b_play";
this.b_play.TabIndex = 0;
this.b_play.Text = "&Play";
this.b_play.Click += new System.EventHandler(this.b_play_Click);
// 
// b_stop
// 
this.b_stop.Location = new System.Drawing.Point(96, 312);
this.b_stop.Name = "b_stop";
this.b_stop.TabIndex = 1;
this.b_stop.Text = "&Stop";
this.b_stop.Click += new System.EventHandler(this.b_stop_Click);
// 
// panelPreview
// 
this.panelPreview.Location = new System.Drawing.Point(8, 8);
this.panelPreview.Name = "panelPreview";
this.panelPreview.Size = new System.Drawing.Size(352, 288);
this.panelPreview.TabIndex = 2;
// 
// b_close
// 
this.b_close.Location = new System.Drawing.Point(648, 312);
this.b_close.Name = "b_close";
this.b_close.TabIndex = 3;
this.b_close.Text = "&Close";
this.b_close.Click += new System.EventHandler(this.b_close_Click);
// 
// b_getpic
// 
this.b_getpic.Location = new System.Drawing.Point(184, 312);
this.b_getpic.Name = "b_getpic";
this.b_getpic.TabIndex = 5;
this.b_getpic.Text = "&GetPic";
this.b_getpic.Click += new System.EventHandler(this.b_getpic_Click);
// 
// pictureBox1
// 
this.pictureBox1.Location = new System.Drawing.Point(376, 8);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(352, 288);
this.pictureBox1.TabIndex = 6;
this.pictureBox1.TabStop = false;
// 
// b_save
// 
this.b_save.Location = new System.Drawing.Point(360, 312);
this.b_save.Name = "b_save";
this.b_save.TabIndex = 7;
this.b_save.Text = "S&ave";
this.b_save.Click += new System.EventHandler(this.b_save_Click);
// 
// saveFileDialog1
// 
this.saveFileDialog1.FileName = "shipin.jpg";
this.saveFileDialog1.Filter = "jpg files (*.jpg)|*.jpg|All files (*.*)|*.*";
this.saveFileDialog1.RestoreDirectory = true;
this.saveFileDialog1.Title = "保存文件";
// 
// t_out
// 
this.t_out.BackColor = System.Drawing.SystemColors.InactiveBorder;
this.t_out.ForeColor = System.Drawing.SystemColors.WindowText;
this.t_out.Location = new System.Drawing.Point(8, 344);
this.t_out.Name = "t_out";
this.t_out.Size = new System.Drawing.Size(712, 21);
this.t_out.TabIndex = 8;
this.t_out.Text = "";
// 
// b_savepic
// 
this.b_savepic.Enabled = false;
this.b_savepic.Location = new System.Drawing.Point(272, 312);
this.b_savepic.Name = "b_savepic";
this.b_savepic.TabIndex = 9;
this.b_savepic.Text = "Sa&vePic";
this.b_savepic.Click += new System.EventHandler(this.b_savepic_Click);
// 
// button1
// 
this.button1.Location = new System.Drawing.Point(448, 312);
this.button1.Name = "button1";
this.button1.TabIndex = 10;
this.button1.Text = "button1";
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(744, 373);
this.Controls.Add(this.button1);
this.Controls.Add(this.b_savepic);
this.Controls.Add(this.t_out);
this.Controls.Add(this.b_save);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.b_getpic);
this.Controls.Add(this.b_close);
this.Controls.Add(this.panelPreview);
this.Controls.Add(this.b_stop);
this.Controls.Add(this.b_play);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "我的摄像头";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion
--------------------编程问答--------------------
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
savefiles=1;
filepath="";
//VedioCapture2.cs的方法
xxx.Initialize( panelPreview,panelPreview.Width,panelPreview.Height);
//VedioCapture.cs的方法
/* wc = new WebCamera( panelPreview.Handle,panelPreview.Width,panelPreview.Height);
wc.StartWebCam();*/
}
private void b_play_Click(object sender, System.EventArgs e)
{
xxx.Initialize( panelPreview,panelPreview.Width,panelPreview.Height);
panelPreview.Visible=true;
b_play.Enabled = false;
b_stop.Enabled = true;
this.b_savepic.Enabled=false;
this.b_save.Enabled=true;
this.b_getpic.Enabled=true;
this.t_out.Text="成功获取视频!";
/* wc = new WebCamera( panelPreview.Handle,panelPreview.Width,panelPreview.Height);
wc.StartWebCam();*/
}
private void b_stop_Click(object sender, System.EventArgs e)
{
xxx.UnLoad();
panelPreview.Visible=false;
            b_play.Enabled = true;
            b_stop.Enabled = false;
this.b_save.Enabled=false;
this.b_savepic.Enabled=false;
this.b_getpic.Enabled=false;
this.t_out.Text="停止获取视频!";
       /*     wc.CloseWebcam();*/
}

private void b_close_Click(object sender, System.EventArgs e)
{
this.Close();
}

private void b_getpic_Click(object sender, System.EventArgs e)
{
this.t_out.Text="";
xxx.CopyToClipBorad();
this.pictureBox1.Image = xxx.getCaptureImage();
this.b_savepic.Enabled=true;
this.t_out.Text="获取图片成功!";
}

private void b_savepic_Click(object sender, System.EventArgs e)
{
this.t_out.Text="";
string filename;
if(filepath=="")
{
filename="图片"+System.DateTime.Now.Date.ToShortDateString();
this.saveFileDialog1.FileName=filename;
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
filepath=this.saveFileDialog1.FileName;
else
return;
}
filename="";
if(savefiles<10)
filename+="_00"+savefiles.ToString();
else if(savefiles<100)
filename+="_0"+savefiles.ToString();
else
filename+="_"+savefiles.ToString();
int leg=filepath.Length;
filename=filepath.Substring(0,leg-4)+filename+filepath.Substring(leg-4,4);
this.pictureBox1.Image.Save(filename);
this.t_out.Text="保存图片“"+filename+"”成功!";
if(savefiles%2==0)
this.t_out.ForeColor=Color.Red;
else
this.t_out.ForeColor=Color.Blue;
savefiles++;
}

private void b_save_Click(object sender, System.EventArgs e)
{
this.t_out.Text="";
xxx.CopyToClipBorad();
this.pictureBox1.Image = xxx.getCaptureImage();
this.t_out.Text="获取图片成功!";
string filename;
if(filepath=="")
{
filename="图片"+System.DateTime.Now.Date.ToShortDateString();
this.saveFileDialog1.FileName=filename;
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
filepath=this.saveFileDialog1.FileName;
else
return;
}
filename="";
if(savefiles<10)
filename+="_00"+savefiles.ToString();
else if(savefiles<100)
filename+="_0"+savefiles.ToString();
else
filename+="_"+savefiles.ToString();
int leg=filepath.Length;
filename=filepath.Substring(0,leg-4)+filename+filepath.Substring(leg-4,4);
this.pictureBox1.Image.Save(filename);
this.t_out.Text="保存图片“"+filename+"”成功!";
if(savefiles%2==0)
this.t_out.ForeColor=Color.Red;
else
this.t_out.ForeColor=Color.Blue;
savefiles++;
}
}
}
--------------------编程问答-------------------- ? --------------------编程问答-------------------- 要想有较强操控的,使用ICaptureGraphBuilder2接口
利用IVideoWindow做视频回放
中间可以用IAMCrossBar进行编码解码的调整
不过整个过程会比较复杂 --------------------编程问答-------------------- up --------------------编程问答-------------------- 不会,
标记,
帮顶。
--------------------编程问答-------------------- 会驱动摄像头~~~~~~~~~~~~~~~~~ --------------------编程问答-------------------- 学习``` --------------------编程问答-------------------- 標記一個^_^ --------------------编程问答-------------------- 这个强,留名。 --------------------编程问答-------------------- 这个强,留名。 --------------------编程问答-------------------- mark --------------------编程问答-------------------- up --------------------编程问答-------------------- 帮顶 --------------------编程问答-------------------- mark,学习了 --------------------编程问答-------------------- mark,学习了 --------------------编程问答-------------------- 在开学这样的软件,有机会一起讨论,QQ270509595 --------------------编程问答-------------------- 不错,以后有用时再看看 --------------------编程问答-------------------- 谢谢各位的答复,现在我的硬件设备已经到位了,但是软件还是进展的很慢,需要快点完成,核心代码还没有完成,努力中^^^^ --------------------编程问答-------------------- mark --------------------编程问答-------------------- mark --------------------编程问答-------------------- 学习,帮顶
--------------------编程问答-------------------- 顶 --------------------编程问答-------------------- mark --------------------编程问答-------------------- 路过学习.......... --------------------编程问答-------------------- 还是实践过程学习的多啊 --------------------编程问答-------------------- 学习当中 --------------------编程问答-------------------- 学习 --------------------编程问答-------------------- qiang xue xi --------------------编程问答-------------------- 要采本机的可以,网上有现成的,要通过HTTP采,C#挺难,一般通过FMS解决 --------------------编程问答-------------------- 学习 --------------------编程问答-------------------- 不会,不用谢... --------------------编程问答-------------------- 学习,帮顶 --------------------编程问答-------------------- 学习 --------------------编程问答-------------------- 出代码的大侠您的webcam和pickhead库在哪儿了啊? --------------------编程问答-------------------- 不会的想学。
会的不教。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,