繁体   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