我如何捕获视频从一个网络摄像头? [英] How do I capture video from a webcam?

查看:130
本文介绍了我如何捕获视频从一个网络摄像头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从一个摄像头捕获视频。有没有在C#/。NET的类,可以帮助我与此有关。我感兴趣的只是实时数据。

I need to capture video from a webcam. Are there any classes in C#/.NET that can help me with this. I am only interested in real time data.

和是否有任何好的C#/。NET的书,我可以学习获得深入了解的语言和平台?

And are there any good C#/.NET books that I can study to gain deep knowledge on the language and the platform?

推荐答案

这是我使用。 你需要一个一流的遍历你的设备:

This is what I use. You need a first class to iterate your devices:

public class DeviceManager
{
    [DllImport("avicap32.dll")]
    protected static extern bool capGetDriverDescriptionA(short wDriverIndex,
        [MarshalAs(UnmanagedType.VBByRefStr)]ref String lpszName,
       int cbName, [MarshalAs(UnmanagedType.VBByRefStr)] ref String lpszVer, int cbVer);

    static ArrayList devices = new ArrayList();

    public static TCamDevice[] GetAllDevices()
    {
        String dName = "".PadRight(100);
        String dVersion = "".PadRight(100);

        for (short i = 0; i < 10; i++)
        {
            if (capGetDriverDescriptionA(i, ref dName, 100, ref dVersion, 100))
            {
                TCamDevice d = new TCamDevice(i);
                d.Name = dName.Trim();
                d.Version = dVersion.Trim();

                devices.Add(d);
            }
        }

        return (TCamDevice[])devices.ToArray(typeof(TCamDevice));
    }

    public static TCamDevice GetDevice(int deviceIndex)
    {
        return (TCamDevice)devices[deviceIndex];
    }
}

和这一个控制凸轮。

public class TCamDevice
{
     private const short WM_CAP = 0x400;
    private const int WM_CAP_DRIVER_CONNECT = 0x40a;
    private const int WM_CAP_DRIVER_DISCONNECT = 0x40b;
    private const int WM_CAP_EDIT_COPY = 0x41e;
    private const int WM_CAP_SET_PREVIEW = 0x432;
    private const int WM_CAP_SET_OVERLAY = 0x433;
    private const int WM_CAP_SET_PREVIEWRATE = 0x434;
    private const int WM_CAP_SET_SCALE = 0x435;
    private const int WS_CHILD = 0x40000000;
    private const int WS_VISIBLE = 0x10000000;

    [DllImport("avicap32.dll")]
    protected static extern int capCreateCaptureWindowA([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpszWindowName,
        int dwStyle, int x, int y, int nWidth, int nHeight, int hWndParent, int nID);

    [DllImport("user32", EntryPoint = "SendMessageA")]
    protected static extern int SendMessage(int hwnd, int wMsg, int wParam, [MarshalAs(UnmanagedType.AsAny)] object lParam);

    [DllImport("user32")]
    protected static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);

    [DllImport("user32")]
    protected static extern bool DestroyWindow(int hwnd);

    int index;
    int deviceHandle;

    public TCamDevice(int index)
    {
        this.index = index;
    }

    private string _name;

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    private string _version;

    public string Version
    {
        get { return _version; }
        set { _version = value; }
    }

    public override string ToString()
    {
        return this.Name;
    }
    /// <summary>
    /// To Initialize the device
    /// </summary>
    /// <param name="windowHeight">Height of the Window</param>
    /// <param name="windowWidth">Width of the Window</param>
    /// <param name="handle">The Control Handle to attach the device</param>
    public void Init(int windowHeight, int windowWidth, int handle)
    {
        string deviceIndex = Convert.ToString(this.index);
        deviceHandle = capCreateCaptureWindowA(ref deviceIndex, WS_VISIBLE | WS_CHILD, 0, 0, windowWidth, windowHeight, handle, 0);

        if (SendMessage(deviceHandle, WM_CAP_DRIVER_CONNECT, this.index, 0) > 0)
        {
            SendMessage(deviceHandle, WM_CAP_SET_SCALE, -1, 0);
            SendMessage(deviceHandle, WM_CAP_SET_PREVIEWRATE, 0x42, 0);
            SendMessage(deviceHandle, WM_CAP_SET_PREVIEW, -1, 0);

            SetWindowPos(deviceHandle, 1, 0, 0, windowWidth, windowHeight, 6);
        }
    }

    /// <summary>
    /// Shows the webcam preview in the control
    /// </summary>
    /// <param name="windowsControl">Control to attach the webcam preview</param>
    public void ShowWindow(global::System.Windows.Forms.Control windowsControl)
    {
        Init(windowsControl.Height, windowsControl.Width, windowsControl.Handle.ToInt32());                        
    }

    /// <summary>
    /// Stop the webcam and destroy the handle
    /// </summary>
    public void Stop()
    {
        SendMessage(deviceHandle, WM_CAP_DRIVER_DISCONNECT, this.index, 0);

        DestroyWindow(deviceHandle);
    }
}

本的ShowWindow把你的图片框的参数。

The ShowWindow takes your PictureBox as parameter.

这篇关于我如何捕获视频从一个网络摄像头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆