2011-02-10 12 views
14

ウェブカメラからビデオをキャプチャする必要があります。 C#/ .NETには、これを手助けできるクラスがありますか?私はリアルタイムデータだけに興味があります。ウェブカメラからビデオをキャプチャするにはどうすればよいですか?

そして、言語とプラットフォームについて深い知識を得るために勉強できる良いC#/ .NETの書籍はありますか?

+0

ようこそStackOverflowへ。私はあなたの質問を一貫して追跡できるように、アカウントに登録することをお勧めします。これは素晴らしい質問ですが、ここでは2つの質問をします。 2番目の質問を新しい質問にすることはできますか?私はあなたが実際に答えを見つけることができると思います:[The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)私はそれがあなたを助けることができないかどうか尋ねることを躊躇しないでしょう。それはできるはずです。 – jcolebrand

+1

質問をウェブカメラと書籍の2つの部分に分割することをお勧めします。 –

+1

@drachenstern:彼はC++についてのC#の質問に対する答えを見つけるだろう。私はそれが可能だと思います。 –

答えて

12

これは私が使用しているものです。 あなたのデバイスを反復処理する最初のクラスが必要:

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]; 
    } 
} 

とカムを制御するには、この1。

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はパラメータとしてピクチャボックスを取得します。

+0

私はURと同じ方法を使用していますが、ウェブカメラは常に呼び出されません。私のシステムを再起動すると、ウェブカメラが起動ボタンを押したときに呼び出されますが、停止してもう一度開始ボタンを押すと、ビデオは表示されません。 SendMessage(deviceHandle、WM_CAP_DRIVER_CONNECT、this.index、0の値は0になり、ビデオは表示されません。誰かが私にchangrにhandle/this.indexの値を毎回示唆しましたが、これもうまくいきませんでした) – Swati

+0

私もできませんでしたどのようにmp4形式で得られたこのビデオを保存するかを見つけてください。 – Swati

+0

次に、 "System.EntryPointNotFoundExceptionをスローするDeviceManager.GetAllDevices()を呼び出そうとします:" CapGetDriverDescriptionA "DLL" avicap32.dll "私は間違っているのですか? – EgoPingvina

4

サードパーティのライブラリを使用することをお勧めします。自分の自転車を発明するのではなく、最高の解決策になるでしょう。ここでは、私はAForge.Netを使用しました。パフォーマンスに関してはいくつか問題がありますが、パフォーマンスが重要な問題になったときに私は自分自身でライブラリを調整しました。 AForge.Netコードはオープンソースであり、必要に応じて調整することができます。

書籍については、間違いなくJeffrey Richter's "CLR via C#"John Skeet's "C# in Depth"をご覧ください。

-1

WebCam_Capture.dllを写真やビデオの撮影に使用してください。ここにはLinkのソースコードがあります。LINK

関連する問題