c# 编写免驱动摄像头监控程式
最近在写摄像头监控程式,但程式运行后一直显示不了画面,想问下,哪位大哥有做过类似的程式,能提供给我参考不,谢谢! --------------------编程问答-------------------- --------------------编程问答-------------------- DllImport 调用user32
using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
namespace Video
{
/// <summary>
/// 一个控制摄像头的类
/// </summary>
public class VideoWork
{
private const int WM_USER = 0x400;
private const int WS_CHILD = 0x40000000;
private const int WS_VISIBLE = 0x10000000;
private const int WM_CAP_START = WM_USER;
private const int WM_CAP_STOP = WM_CAP_START + 68;
private const int WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10;
private const int WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11;
private const int WM_CAP_SAVEDIB = WM_CAP_START + 25;
private const int WM_CAP_GRAB_FRAME = WM_CAP_START + 60;
private const int WM_CAP_SEQUENCE = WM_CAP_START + 62;
private const int WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20;
private const int WM_CAP_SEQUENCE_NOFILE =WM_CAP_START+ 63;
private const int WM_CAP_SET_OVERLAY =WM_CAP_START+ 51;
private const int WM_CAP_SET_PREVIEW =WM_CAP_START+ 50;
private const int WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START +6;
private const int WM_CAP_SET_CALLBACK_ERROR=WM_CAP_START +2;
private const int WM_CAP_SET_CALLBACK_STATUSA= WM_CAP_START +3;
private const int WM_CAP_SET_CALLBACK_FRAME= WM_CAP_START +5;
private const int WM_CAP_SET_SCALE=WM_CAP_START+ 53;
private const int WM_CAP_SET_PREVIEWRATE=WM_CAP_START+ 52;
private IntPtr hWndC;
private bool bWorkStart = false;
private IntPtr mControlPtr;
private int mWidth;
private int mHeight;
private int mLeft;
private int mTop;
private byte[] lpszName = new byte[100];
// 初始化显示图像
public VideoWork(IntPtr handle, int left, int top, int width,int height)
{
mControlPtr = handle;
mWidth = width;
mHeight = height;
mLeft = left;
mTop = top;
}
[DllImport("avicap32.dll")]
private static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID);
[DllImport("avicap32.dll")]
private static extern int capGetVideoFormat(IntPtr hWnd, IntPtr psVideoFormat, int wSize );
[DllImport("User32.dll")]
private static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, long lParam);
// 开始显示图像
public void Start()
{
if (bWorkStart)
return;
bWorkStart = true;
hWndC = capCreateCaptureWindowA(lpszName,WS_CHILD|WS_VISIBLE ,mLeft,mTop,mWidth,mHeight,mControlPtr,0);
if (hWndC.ToInt32() != 0)
{
SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);
SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, 0, 0);
SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, 0, 0);
SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0);
SendMessage(hWndC, WM_CAP_SET_SCALE, 1, 0);
SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, 66, 0);
SendMessage(hWndC, WM_CAP_SET_OVERLAY, 1, 0);
SendMessage(hWndC, WM_CAP_SET_PREVIEW, 1, 0);
//Global.log.Write("SendMessage");
}
return;
}
// 停止显示
public void Stop()
{
SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0);
bWorkStart = false;
}
//抓图
public void GrabImage(string path)
{
IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);
SendMessage(hWndC,WM_CAP_SAVEDIB,0,hBmp.ToInt64());
}
public void Kinescope(string path)
{
IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);
SendMessage(hWndC, WM_CAP_FILE_SET_CAPTURE_FILEA, 0, hBmp.ToInt64());
SendMessage(hWndC, WM_CAP_SEQUENCE, 0, 0);
}
//停止录像
public void StopKinescope()
{
SendMessage(hWndC, WM_CAP_STOP, 0, 0);
}
}
}
--------------------编程问答-------------------- 楼上正解。。。 --------------------编程问答-------------------- 普通的视频聊天摄像头可以用该程序,进行录像么? --------------------编程问答-------------------- 谢谢各位提供资料,我试试! --------------------编程问答-------------------- 学习了 --------------------编程问答-------------------- 可以 --------------------编程问答-------------------- mark --------------------编程问答-------------------- 收藏! --------------------编程问答-------------------- 学习。。。 --------------------编程问答--------------------
可以给出一段实例化此类的操作代码么? --------------------编程问答-------------------- 我综合了一下网上的摄像头代码:
===============================================================================================
控制摄像头的类
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
namespace Camera
{
/// <summary>
/// 一个控制摄像头的类
/// </summary>
public class PCCamera
{
private const int WM_USER = 0x400;
private const int WS_CHILD = 0x40000000;
private const int WS_VISIBLE = 0x10000000;
private const int WM_CAP_START = WM_USER;
private const int WM_CAP_STOP = WM_CAP_START + 68;
private const int WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10;
private const int WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11;
private const int WM_CAP_SAVEDIB = WM_CAP_START + 25;
private const int WM_CAP_GRAB_FRAME = WM_CAP_START + 60;
private const int WM_CAP_SEQUENCE = WM_CAP_START + 62;
private const int WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20;
private const int WM_CAP_SEQUENCE_NOFILE = WM_CAP_START + 63;
private const int WM_CAP_SET_OVERLAY = WM_CAP_START + 51;
private const int WM_CAP_SET_PREVIEW = WM_CAP_START + 50;
private const int WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START + 6;
private const int WM_CAP_SET_CALLBACK_ERROR = WM_CAP_START + 2;
private const int WM_CAP_SET_CALLBACK_STATUSA = WM_CAP_START + 3;
private const int WM_CAP_SET_CALLBACK_FRAME = WM_CAP_START + 5;
private const int WM_CAP_SET_SCALE = WM_CAP_START + 53;
private const int WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52;
private IntPtr hWndC;
private bool bWorkStart = false;
private IntPtr mControlPtr;
private int mWidth;
private int mHeight;
private int mLeft;
private int mTop;
/// <summary>
/// 初始化显示图像
/// </summary>
/// <param name="handle">控件的句柄 </param>
/// <param name="left">开始显示的左边距 </param>
/// <param name="top">开始显示的上边距 </param>
/// <param name="width">要显示的宽度 </param>
/// <param name="height">要显示的长度 </param>
public PCCamera(IntPtr handle, int left, int top, int width, int height) //PCCamera
{
mControlPtr = handle;
mWidth = width;
mHeight = height;
mLeft = left;
mTop = top;
}
[DllImport("avicap32.dll")]
private static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID);
[DllImport("avicap32.dll")]
private static extern int capGetVideoFormat(IntPtr hWnd, IntPtr psVideoFormat, int wSize);
[DllImport("User32.dll")]
private static extern bool SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
/// <summary>
/// 开始显示图像
/// </summary>
public void Start()
{
if (bWorkStart)
return;
bWorkStart = true;
byte[] lpszName = new byte[100];
hWndC = capCreateCaptureWindowA(lpszName, WS_CHILD | WS_VISIBLE, mLeft, mTop, mWidth, mHeight, mControlPtr, 0);
if (hWndC.ToInt32() != 0)
{
SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, IntPtr.Zero, IntPtr.Zero);
SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, IntPtr.Zero, IntPtr.Zero);
SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, IntPtr.Zero, IntPtr.Zero);
SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, IntPtr.Zero, IntPtr.Zero);
SendMessage(hWndC, WM_CAP_SET_SCALE, (IntPtr) 1, IntPtr.Zero);
SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, (IntPtr)66, IntPtr.Zero);
SendMessage(hWndC, WM_CAP_SET_OVERLAY, (IntPtr)1, IntPtr.Zero);
SendMessage(hWndC, WM_CAP_SET_PREVIEW, (IntPtr)1, IntPtr.Zero);
}
return;
}
/// <summary>
/// 停止显示图像
/// </summary>
public void Stop()
{
SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, IntPtr.Zero, IntPtr.Zero);
bWorkStart = false;
}
/// <summary>
/// 抓图
/// </summary>
/// <param name="path">要保存bmp文件的路径 </param>
public void GrabImage(string path)
{
IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);
SendMessage(hWndC, WM_CAP_SAVEDIB, IntPtr.Zero , hBmp);
}
///<summary>
///录像
///</summary>
///<param name="path">要保存avi文件的路径</param>
public void Kinescope(string path)
{
IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);
SendMessage(hWndC, WM_CAP_FILE_SET_CAPTURE_FILEA, IntPtr.Zero,hBmp );
SendMessage(hWndC, WM_CAP_SEQUENCE, IntPtr.Zero,IntPtr.Zero );
}
///<summary>
///停止录像
///</summary>
public void StopKinescope()
{
SendMessage(hWndC, WM_CAP_STOP, IntPtr.Zero, IntPtr.Zero);
}
}
}
===============================================================================================
操作摄像头的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Temp
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
pcc = new Camera.PCCamera(this.panelCamera.Handle, 0, 0, 320, 240); //panelCamera is Control panel
}
private Camera.PCCamera pcc;
//开始
private void btnStart_Click(object sender, EventArgs e)
{
pcc.Start();
}
//停止
private void btnStop_Click(object sender, EventArgs e)
{
pcc.Stop();
}
//截图
private void btnSaveImage_Click(object sender, EventArgs e)
{
pcc.GrabImage("c:\\" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg");
}
//录像
private void btnKinescope_Click(object sender, EventArgs e)
{
pcc.Kinescope("c:\\" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".avi");
}
//停止录像
private void btnStopKinescope_Click(object sender, EventArgs e)
{
Application.DoEvents();
pcc.StopKinescope();
}
}
}
===============================================================================================
但是我不知道如何判断电脑上是否有摄像头的代码,唉!
我收藏此贴了! --------------------编程问答-------------------- 学习!感谢解题的高手! --------------------编程问答--------------------
up --------------------编程问答-------------------- 路過學習 --------------------编程问答-------------------- Mark
Up
Study --------------------编程问答-------------------- 不收藏都对不起来这一趟了. --------------------编程问答-------------------- 用上面的代码可以实现抓图和录像,但还有个严重的问题!
用我的摄像头录像50几秒,你知道文件多大吗?? 1G多!!
请问这是怎么回事? 请高手帮忙解决 --------------------编程问答-------------------- 好像那种免驱动的USB摄像头不行哦。。我试过好多方法都不行。。不知是不是一定要那种要装驱动的摄像头才能用呢。。各位高手指点迷津啊。。万分感谢 --------------------编程问答-------------------- 学习 --------------------编程问答--------------------
应该要压缩的吧? --------------------编程问答-------------------- 登记一下留个纪念顺便帮顶. --------------------编程问答-------------------- 学习 --------------------编程问答-------------------- ding... --------------------编程问答--------------------
我的也不行啊! --------------------编程问答-------------------- 最近怎么这么多网友问这个方面的问题呀。
解决办法:DirectShow。
如果只是抓图的话,可以使用我以前写的一个动态库:参考
如果是要录像的话,我的dll就不支持了。
至于录像很多是因为采用了未压缩的Avi编码当然是很大的。
压缩了就行了,压缩的话,需要有压缩编码的类库才行。
可以自己写,网上也有免费的,但是要投入时间也精力才行。
我这里有写好的压缩录像编码的程序,可惜不能给你。 --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- 楼主最近我也遇到这个问题了,你有解决办法没,给我说一下啊 --------------------编程问答-------------------- DirectX解决吧
http://www.codeproject.com/info/search.aspx?artkw=webcam
--------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答--------------------
补充:.NET技术 , C#