尝试从网络摄像头预览视频的性能问题

时间:2011-12-21 21:40:26

标签: c# windows webcam

我写了一个小应用程序,只显示网络摄像头或我的捕获卡的预览。目前它的工作方式正是我想要的,除了捕获卡显示的帧速率低于我想要的帧速率。

以下是我的相关代码:

    private const int WM_CAP_DRIVER_CONNECT = 1034;
    private const int WM_CAP_SET_PREVIEW = 1074;
    private const int WM_CAP_SET_PREVIEWRATE = 1076;
    private const int WM_CAP_SET_SCALE = 1077;
    private const int WS_CHILD = 1073741824;
    private const int WS_VISIBLE = 268435456;
    private const short SWP_NOMOVE = 2;
    private const short SWP_NOZORDER = 4;
    private const short HWND_BOTTOM = 1;
    private const int iDevice = 0;
    private int hHwnd;
    private int previewRate = 34;
    private int width = 640;
    private int height = 480;

    [DllImport("user32.dll", EntryPoint="SendMessageA")]
    public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);

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

    [DllImport("user32.dll")]
    static extern bool DestroyWindow(int hndw);

    [DllImport("avicap32.dll")]
    public static extern int capCreateCaptureWindow(string lpszWindowName, int dwStyle, int X, int Y, int nWidth, int nHeight, int hwndParent, int nID);

    private void OpenPreviewWindow() 
    {
        hHwnd = capCreateCaptureWindow(iDevice.ToString(), (WS_VISIBLE | WS_CHILD), 0, 0, width, height, Handle.ToInt32(), 0);

        //  Connect to device
        if (SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) != -1) 
        {
            SendMessage(hHwnd, WM_CAP_SET_SCALE, 1, 0);
            SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, previewRate, 0);
            SendMessage(hHwnd, WM_CAP_SET_PREVIEW, 1, 0);
            SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, width, height, (SWP_NOMOVE | SWP_NOZORDER));
        }
        else 
        {
            DestroyWindow(hHwnd);
        }
    }

当我在像FMLE这样的应用程序中预览捕获卡时,它以30 FPS预览,这是我的目标帧速率(每帧约34毫秒),但是当我使用我的应用程序进行预览时,它接近10-15 FPS。我可能还应该注意,我的程序将从我的网络摄像头预览30 FPS。什么可能导致捕获卡的问题,我该如何解决它?

1 个答案:

答案 0 :(得分:0)