【问题标题】:How to convert SoftwareBitmap from Bgra8 to JPEG in Windows UWP如何在 Windows UWP 中将 SoftwareBitmap 从 Bgra8 转换为 JPEG
【发布时间】:2017-10-21 23:25:40
【问题描述】:

如何在 Windows UWP 中将 SoftwareBitmap 从 Bgra8 转换为 JPEG。 GetPreviewFrameAsync 函数用于获取 Bgra8 中的 videoFrame 数据。下面的代码出了什么问题?我的 jpeg 大小为 0。

    auto previewProperties = static_cast<MediaProperties::VideoEncodingProperties^>
        (mediaCapture->VideoDeviceController->GetMediaStreamProperties(Capture::MediaStreamType::VideoPreview));
    unsigned int videoFrameWidth = previewProperties->Width;
    unsigned int videoFrameHeight = previewProperties->Height;
    FN_TRACE("%s videoFrameWidth %d videoFrameHeight %d\n",
        __func__, videoFrameWidth, videoFrameHeight);

    // Create the video frame to request a SoftwareBitmap preview frame
    auto videoFrame = ref new VideoFrame(BitmapPixelFormat::Bgra8, videoFrameWidth, videoFrameHeight);

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

        auto inputStream = ref new Streams::InMemoryRandomAccessStream();

        create_task(BitmapEncoder::CreateAsync(BitmapEncoder::JpegEncoderId, inputStream))
            .then([this, previewFrame, inputStream](BitmapEncoder^ encoder)
        {
            encoder->SetSoftwareBitmap(previewFrame);
            encoder->FlushAsync();

            FN_TRACE("jpeg size %d\n", inputStream->Size);
            Streams::Buffer^ data = ref new Streams::Buffer(inputStream->Size);
            create_task(inputStream->ReadAsync(data, (unsigned int)inputStream->Size, InputStreamOptions::None));
        });
});

【问题讨论】:

    标签: windows uwp c++-cx


    【解决方案1】:

    Bitmap​Encoder.FlushAsync() 方法是一个异步方法。我们应该像下面这样使用它:

    // Capture the preview frames
    return create_task(mediaCapture->GetPreviewFrameAsync(videoFrame))
        .then([this](VideoFrame^ currentFrame)
    {
        // Collect the resulting frame
        auto previewFrame = currentFrame->SoftwareBitmap;
    
        auto inputStream = ref new Streams::InMemoryRandomAccessStream();
    
        return create_task(BitmapEncoder::CreateAsync(BitmapEncoder::JpegEncoderId, inputStream))
            .then([this, previewFrame](BitmapEncoder^ encoder)
        {
            encoder->SetSoftwareBitmap(previewFrame);
            return encoder->FlushAsync();
        }).then([this, inputStream]()
        {
    
            FN_TRACE("jpeg size %d\n", inputStream->Size);
            //TODO
        });
    });
    

    那么您应该能够获得合适的尺寸。更多信息请见Asynchronous programming in C++

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-20
      • 1970-01-01
      • 1970-01-01
      • 2018-08-21
      • 1970-01-01
      • 2018-08-21
      • 2016-09-19
      相关资源
      最近更新 更多