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

发布于 2024-12-22 15:24:05 字数 2061 浏览 2 评论 0原文

我编写了一个小应用程序,它仅显示网络摄像头或捕获卡的预览。目前,它完全按照我想要的方式工作,除了采集卡显示的帧速率比我想要的低得多。

这是我的相关代码:

    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 帧/秒。我可能还应该注意到,我的程序将从我的网络摄像头预览 30 FPS。采集卡出现问题的原因是什么?如何修复?

I have a small application I wrote that simply displays a preview of webcam or my capture card. At the moment it works exactly how I want it to, with the exception that the capture card displays at a much lower framerate than I'd like it to.

Here is my relevant code:

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

When I preview the capture card in an application like FMLE, it previews at 30 FPS, which is my target framerate (~34 milliseconds per frame,) however when I use my application to preview it's closer to 10-15 FPS. I should probably also note that my program will preview 30 FPS from my webcam. What could be causing the problem with the capture card, and how can I fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文