WinRT C ++(Win10)从SoftwareBitmap / BitmapBuffer访问字节

时间:2015-08-05 20:01:27

标签: c++ windows-runtime buffer windows-10 uwp

要在OpenCV中处理我的相机的previewFrames,我需要访问原始像素数据/字节。所以,有新的SoftwareBitmap,它应该提供这个。

c#有一个example,但在visual c ++中,我无法获得IMemoryBufferByteAccess(参见备注)界面的工作。

带例外的代码:

// Capture the preview frame
return create_task(_mediaCapture->GetPreviewFrameAsync(videoFrame))
    .then([this](VideoFrame^ currentFrame)
{
    // Collect the resulting frame
    auto previewFrame = currentFrame->SoftwareBitmap;

    auto buffer = previewFrame->LockBuffer(Windows::Graphics::Imaging::BitmapBufferAccessMode::ReadWrite);
    auto reference = buffer->CreateReference();

    // Get a pointer to the pixel buffer
    byte* pData = nullptr;
    UINT capacity = 0;

    // Obtain ByteAccess
    ComPtr<IUnknown> inspectable = reinterpret_cast<IUnknown*>(buffer);
    // Query the IBufferByteAccess interface.
    Microsoft::WRL::ComPtr<IMemoryBufferByteAccess> bufferByteAccess;
    ThrowIfFailed(inspectable.As(&bufferByteAccess));     // ERROR ---> Throws HRESULT = E_NOINTERFACE

    // Retrieve the buffer data.
    ThrowIfFailed(bufferByteAccess->GetBuffer(_Out_ &pData, _Out_ &capacity));      // ERROR ---> Throws HRESULT = E_NOINTERFACE, because bufferByteAccess is null

我也试过了:

    HRESULT hr = ((IMemoryBufferByteAccess*)reference)->GetBuffer(&pData, &capacity);

HRESULT没问题,但我无法访问pData - &gt;访问违规读取内存。

感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

你应该在reinterpret_cast中使用reference而不是buffer。

#include "pch.h"
#include <wrl\wrappers\corewrappers.h>
#include <wrl\client.h>

MIDL_INTERFACE("5b0d3235-4dba-4d44-865e-8f1d0e4fd04d") 
IMemoryBufferByteAccess : IUnknown
{
    virtual HRESULT STDMETHODCALLTYPE GetBuffer(
        BYTE   **value,
        UINT32 *capacity
        );
};

        auto previewFrame = currentFrame->SoftwareBitmap;

        auto buffer = previewFrame->LockBuffer(BitmapBufferAccessMode::ReadWrite);

        auto reference = buffer->CreateReference();

        ComPtr<IMemoryBufferByteAccess> bufferByteAccess;

        HRESULT result = reinterpret_cast<IInspectable*>(reference)->QueryInterface(IID_PPV_ARGS(&bufferByteAccess));

        if (result == S_OK)
        {
            WriteLine("Get interface successfully");

            BYTE* data = nullptr;

            UINT32 capacity = 0;

            result = bufferByteAccess->GetBuffer(&data, &capacity);

            if (result == S_OK)
            {
                WriteLine("get data access successfully, capacity: " + capacity);
            }
        }

答案 1 :(得分:0)

我目前正在unsigned char* data事件中获取的每个帧中访问原始MediaFrameReader::FrameArrived,而不使用WRL和COM ...

这是如何:

void MainPage::OnFrameArrived(MediaFrameReader ^reader, MediaFrameArrivedEventArgs ^args)
{
    MediaFrameReference ^mfr = reader->TryAcquireLatestFrame();
    VideoMediaFrame ^vmf = mfr->VideoMediaFrame;
    VideoFrame ^vf = vmf->GetVideoFrame();
    SoftwareBitmap ^sb = vf->SoftwareBitmap; 
    Buffer ^buff = ref new Buffer(sb->PixelHeight * sb->PixelWidth * 2);
    sb->CopyToBuffer(buff);
    DataReader ^dataReader = DataReader::FromBuffer(buffer);
    Platform::Array<unsigned char, 1> ^arr = ref new Platform::Array<unsigned char, 1>(buffer->Length);
    dataReader->ReadBytes(arr);
    // here arr->Data is a pointer to the raw pixel data
}

注意MediaCapture对象需要配置MediaCaptureMemoryPreference::Cpu才能拥有有效的SoftwareBitmap

希望以上帮助某人

答案 2 :(得分:0)

基于@ jeffrey-chen的answer和@kennykerr的example,我组装了一个更简洁的解决方案:

#include <wrl/client.h>
// other includes, as required by your project

MIDL_INTERFACE("5b0d3235-4dba-4d44-865e-8f1d0e4fd04d")
IMemoryBufferByteAccess : ::IUnknown
{
    virtual HRESULT __stdcall GetBuffer(BYTE **value, UINT32 *capacity) = 0;
};

// your code:
    auto previewFrame = currentFrame->SoftwareBitmap;

    auto buffer = previewFrame->LockBuffer(BitmapBufferAccessMode::ReadWrite);

    auto bufferByteAccess= buffer->CreateReference().as<IMemoryBufferByteAccess>();
    WriteLine("Get interface successfully"); // otherwise - exception is thrown

    BYTE* data = nullptr;
    UINT32 capacity = 0;

    winrt::check_hresult(bufferByteAccess->GetBuffer(&data, &capacity));
    WriteLine("get data access successfully, capacity: " + capacity);