簡體   English   中英

如何使用DirectX捕獲視頻幀

[英]How to capture a frame of a video using directx

我對DirectX還是很陌生,因此我面臨一個新的挑戰,即我試圖處理來自另一台PC(使用Directx C#)通過HDMI作為視頻流的視頻幀(60 fps)。 我正在使用視頻捕獲卡捕獲視頻。 此外,我的代碼使我能夠完美地捕獲視頻。

但是,我有一個要求,我需要能夠在流傳輸的同時處理視頻幀 (可能在單獨的線程中)。

我嘗試使用AForge庫捕獲幀,但是僅適用於集成的網絡攝像頭。當我嘗試使用捕獲卡運行此幀時,它僅顯示黑屏。任何指針或參考鏈接將不勝感激。

最終,我自己找到了解決方案。可以使用Directshow的SampleGrabber方法提取視頻流的幀。

/// <summary> Interface frame event </summary>
public delegate void HeFrame(System.Drawing.Bitmap BM);
/// <summary> Frame event </summary>
public event HeFrame FrameEvent2;
private    byte[] savedArray;
private    int    bufferedSize;

int ISampleGrabberCB.BufferCB(double SampleTime, IntPtr pBuffer,
    int BufferLen )
{
    this.bufferedSize = BufferLen;

    int stride = this.SnapShotWidth * 3;

    Marshal.Copy( pBuffer, this.savedArray, 0, BufferLen );

    GCHandle handle = GCHandle.Alloc( this.savedArray, GCHandleType.Pinned );
    int scan0 = (int) handle.AddrOfPinnedObject();
    scan0 += (this.SnapShotHeight - 1) * stride;
    Bitmap b = new Bitmap(this.SnapShotWidth, this.SnapShotHeight, -stride,
        System.Drawing.Imaging.PixelFormat.Format24bppRgb, (IntPtr) scan0 );
    handle.Free();
    SetBitmap=b;
    return 0;
}
/// <summary> capture event, triggered by buffer callback. </summary>
private void OnCaptureDone()
{
    Trace.WriteLine( "!!DLG: OnCaptureDone" );
}
/// <summary> Allocate memory space and set SetCallBack </summary>
public void GrapImg()
{
    Trace.Write ("IMG");
    if( this.savedArray == null )
    {
        int size = this.snapShotImageSize;
        if( (size < 1000) || (size > 16000000) )
            return;
        this.savedArray = new byte[ size + 64000 ];
    }
    sampGrabber.SetCallback( this, 1 );
}
/// <summary> Transfer bitmap upon firing event </summary>
public System.Drawing.Bitmap SetBitmap
{
    set
    {
        this.FrameEvent2(value);
    }
}

是文章的鏈接。可能需要一些幫助,因為我花了很多時間才能做到這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM