怎么样用摄像头录像并且保存

CR_Soft 2007-10-26 01:10:29
我想实现用摄像头录像并且按照自己想要的格式(比如ASF)保存在电脑中。
下面是我在网上找到的摄像头采集的代码,他只提供预览,怎么样在这个基础上加入录像功能,即便是保存成AVI的也行,或者吧影片保存在内存流中也可以,帮帮我

using System;
using System.Runtime.InteropServices;

namespace webcam
{
/**/
///
/// avicap 的摘要说明。
///
public class showVideo
{
// showVideo calls
[DllImport("avicap32.dll")]
public static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID);
[DllImport("avicap32.dll")]
public static extern bool capGetDriverDescriptionA(short wDriver, byte[] lpszName, int cbName, byte[] lpszVer, int cbVer);
[DllImport("User32.dll")]
public static extern bool SendMessage(IntPtr hWnd, int wMsg, bool wParam, int lParam);
[DllImport("User32.dll")]
public static extern bool SendMessage(IntPtr hWnd, int wMsg, short wParam, int lParam);
[DllImport("User32.dll")]
public static extern bool SendMessage(IntPtr hWnd, int wMsg, short wParam, FrameEventHandler lParam);
[DllImport("User32.dll")]
public static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, ref BITMAPINFO lParam);
[DllImport("User32.dll")]
public static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
[DllImport("avicap32.dll")]
public static extern int capGetVideoFormat(IntPtr hWnd, IntPtr psVideoFormat, int wSize);

// Constants
public const int WM_USER = 0x400;
public const int WS_CHILD = 0x40000000;
public const int WS_VISIBLE = 0x10000000;
public const int SWP_NOMOVE = 0x2;
public const int SWP_NOZORDER = 0x4;
public const int WM_CAP_DRIVER_CONNECT = WM_USER + 10;
public const int WM_CAP_DRIVER_DISCONNECT = WM_USER + 11;
public const int WM_CAP_SET_CALLBACK_FRAME = WM_USER + 5;
public const int WM_CAP_SET_PREVIEW = WM_USER + 50;
public const int WM_CAP_SET_PREVIEWRATE = WM_USER + 52;
public const int WM_CAP_SET_VIDEOFORMAT = WM_USER + 45;

// Structures
[StructLayout(LayoutKind.Sequential)]
public struct VIDEOHDR
{
[MarshalAs(UnmanagedType.I4)]
public int lpData;
[MarshalAs(UnmanagedType.I4)]
public int dwBufferLength;
[MarshalAs(UnmanagedType.I4)]
public int dwBytesUsed;
[MarshalAs(UnmanagedType.I4)]
public int dwTimeCaptured;
[MarshalAs(UnmanagedType.I4)]
public int dwUser;
[MarshalAs(UnmanagedType.I4)]
public int dwFlags;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public int[] dwReserved;
}

[StructLayout(LayoutKind.Sequential)]
public struct BITMAPINFOHEADER
{
[MarshalAs(UnmanagedType.I4)]
public Int32 biSize;
[MarshalAs(UnmanagedType.I4)]
public Int32 biWidth;
[MarshalAs(UnmanagedType.I4)]
public Int32 biHeight;
[MarshalAs(UnmanagedType.I2)]
public short biPlanes;
[MarshalAs(UnmanagedType.I2)]
public short biBitCount;
[MarshalAs(UnmanagedType.I4)]
public Int32 biCompression;
[MarshalAs(UnmanagedType.I4)]
public Int32 biSizeImage;
[MarshalAs(UnmanagedType.I4)]
public Int32 biXPelsPerMeter;
[MarshalAs(UnmanagedType.I4)]
public Int32 biYPelsPerMeter;
[MarshalAs(UnmanagedType.I4)]
public Int32 biClrUsed;
[MarshalAs(UnmanagedType.I4)]
public Int32 biClrImportant;
}

[StructLayout(LayoutKind.Sequential)]
public struct BITMAPINFO
{
[MarshalAs(UnmanagedType.Struct, SizeConst = 40)]
public BITMAPINFOHEADER bmiHeader;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1024)]
public Int32[] bmiColors;
}

public delegate void FrameEventHandler(IntPtr lwnd, IntPtr lpVHdr);

// Public methods
public static object GetStructure(IntPtr ptr, ValueType structure)
{
return Marshal.PtrToStructure(ptr, structure.GetType());
}

public static object GetStructure(int ptr, ValueType structure)
{
return GetStructure(new IntPtr(ptr), structure);
}

public static void Copy(IntPtr ptr, byte[] data)
{
Marshal.Copy(ptr, data, 0, data.Length);
}

public static void Copy(int ptr, byte[] data)
{
Copy(new IntPtr(ptr), data);
}

public static int SizeOf(object structure)
{
return Marshal.SizeOf(structure);
}
}

//Web Camera Class
public class WebCamera
{
// Constructur
public WebCamera(IntPtr handle, int width, int height)
{
mControlPtr = handle;
mWidth = width;
mHeight = height;
}
// delegate for frame callback
public delegate void RecievedFrameEventHandler(byte[] data);
public event RecievedFrameEventHandler RecievedFrame;

private IntPtr lwndC; // Holds the unmanaged handle of the control
private IntPtr mControlPtr; // Holds the managed pointer of the control
private int mWidth;
private int mHeight;

private showVideo.FrameEventHandler mFrameEventHandler; // Delegate instance for the frame callback - must keep alive! gc should NOT collect it
// Close the web camera
public void CloseWebcam()
{
this.capDriverDisconnect(this.lwndC);
}

// start the web camera
public void StartWebCam()
{
byte[] lpszName = new byte[100];
byte[] lpszVer = new byte[100];

showVideo.capGetDriverDescriptionA(0, lpszName, 100, lpszVer, 100);
this.lwndC = showVideo.capCreateCaptureWindowA(lpszName, showVideo.WS_VISIBLE + showVideo.WS_CHILD, 0, 0, mWidth, mHeight, mControlPtr, 0);

if (this.capDriverConnect(this.lwndC, 0))
{
this.capPreviewRate(this.lwndC, 66);
this.capPreview(this.lwndC, true);
showVideo.BITMAPINFO bitmapinfo = new showVideo.BITMAPINFO();
bitmapinfo.bmiHeader.biSize = showVideo.SizeOf(bitmapinfo.bmiHeader);
bitmapinfo.bmiHeader.biWidth = 352;
bitmapinfo.bmiHeader.biHeight = 288;
bitmapinfo.bmiHeader.biPlanes = 1;
bitmapinfo.bmiHeader.biBitCount = 24;
this.capSetVideoFormat(this.lwndC, ref bitmapinfo, showVideo.SizeOf(bitmapinfo));
this.mFrameEventHandler = new showVideo.FrameEventHandler(FrameCallBack);
this.capSetCallbackOnFrame(this.lwndC, this.mFrameEventHandler);
showVideo.SetWindowPos(this.lwndC, 0, 0, 0, mWidth, mHeight, 6);
}
}

// private functions
private bool capDriverConnect(IntPtr lwnd, short i)
{
return showVideo.SendMessage(lwnd, showVideo.WM_CAP_DRIVER_CONNECT, i, 0);
}
private bool capDriverDisconnect(IntPtr lwnd)
{
return showVideo.SendMessage(lwnd, showVideo.WM_CAP_DRIVER_DISCONNECT, 0, 0);
}
private bool capPreview(IntPtr lwnd, bool f)
{ return showVideo.SendMessage(lwnd, showVideo.WM_CAP_SET_PREVIEW, f, 0); }

private bool capPreviewRate(IntPtr lwnd, short wMS)
{
return showVideo.SendMessage(lwnd, showVideo.WM_CAP_SET_PREVIEWRATE, wMS, 0);
}

private bool capSetCallbackOnFrame(IntPtr lwnd, showVideo.FrameEventHandler lpProc)
{
return showVideo.SendMessage(lwnd, showVideo.WM_CAP_SET_CALLBACK_FRAME, 0, lpProc);
}

private bool capSetVideoFormat(IntPtr hCapWnd, ref showVideo.BITMAPINFO BmpFormat, int CapFormatSize)
{
return showVideo.SendMessage(hCapWnd, showVideo.WM_CAP_SET_VIDEOFORMAT, CapFormatSize, ref BmpFormat);
}

private void FrameCallBack(IntPtr lwnd, IntPtr lpVHdr)
{
showVideo.VIDEOHDR videoHeader = new showVideo.VIDEOHDR();
byte[] VideoData;
videoHeader = (showVideo.VIDEOHDR)showVideo.GetStructure(lpVHdr, videoHeader);
VideoData = new byte[videoHeader.dwBytesUsed];
showVideo.Copy(videoHeader.lpData, VideoData);
if (this.RecievedFrame != null)
this.RecievedFrame(VideoData);
}

}
...全文
8873 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
xdspower 2007-12-28
  • 打赏
  • 举报
回复
Windows media Encoder只能压缩成有限的格式,不过我想你也够了。
yangsh3002 2007-12-28
  • 打赏
  • 举报
回复
最简单的方法:用VFW技术记录视频。http://zhumeng.org/bbs/a/a.asp?B=1102&ID=70037
sandmangu 2007-12-10
  • 打赏
  • 举报
回复
简单点的方法,每抓一个frame就把它编码成jpeg到一个文件里,内存里开一个数组记录录像过程中所有jpeg的采样时间和jpeg大小,等录像结束以后靠这个jpeg文件和数组就可以生成avi文件,数组是用来生成avi的index索引表的。 如果要无限制录像,就把这个数组也作为一个文件保存下来,最后结束的时候再读出来
yyhzpk 2007-12-10
  • 打赏
  • 举报
回复
微软的media encode是不是专门做这个的?
nosilence_2007 2007-11-29
  • 打赏
  • 举报
回复
w我也在做 你可以直接写一个encoder和写文件(dump)的插件,然后修改摄像头的注册表,调用这个encoder,就可以保存了

你也可以自己写一个应用(.exe),build一个graph,把摄像头filter调用加进去这个graph,然后把你自己写的encoder filter 和dumpfilter 加入这个graph,然后rederstream(...). 写完生成.exe文件,点击.exe文件就可以了
goodluck16 2007-11-08
  • 打赏
  • 举报
回复
知道用vb做的,要用banasoft控件
xdspower 2007-11-06
  • 打赏
  • 举报
回复
没有这么麻烦,你直接找Windows media Encoder控件就OK了,你要的功能直接搞定
starytx 2007-11-05
  • 打赏
  • 举报
回复
用vfw的话就有保存文件的函数,不用自己管啊,只要设置保存路径,然后捕捉时调用那个保存文件的捕捉函数即可
aoosang 2007-11-05
  • 打赏
  • 举报
回复
sdk 中有个例子avcap是采集摄像头,并保存为avi的文件,可以借鉴一下,so easy
蒋晟 2007-11-04
  • 打赏
  • 举报
回复
用Windows Media Encoder SDK/DirectShow+WM Writer都可以
nosilence_2007 2007-10-31
  • 打赏
  • 举报
回复
帮顶

我也不懂

在DS中 摄像头拍摄出来的数据如果要保存未jpeg格式的,首先摄像头拍下来是原始的YUV图像,但是在DS中我如果直接输入YUV图像 根本就不能识别出来它是多媒体!!! 但是如果输入的是bmi格式的就可以让DS识别出他是多媒体,并且进行其他步骤.

这好像需要一个头,VIDEOINFOHEADER 这里面的数值我该在什么时候设置,怎么设置呢? 谢谢

2,554

社区成员

发帖
与我相关
我的任务
社区描述
专题开发/技术/项目 多媒体/流媒体开发
社区管理员
  • 多媒体/流媒体开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧