在 WinUI3 中旋转从网络摄像头捕获的图像

问题描述 投票:0回答:1

我正在尝试从 WinUI3 应用程序中的网络摄像头捕获图像。 经过一番尝试和错误后,我终于成功地通过 MediaFrameReader 做到了这一点。 现在,我已将捕获的图像作为 SoftwareBitmap,并且我想将这些图像提供给 OCR,因此我需要旋转它们。

我已经尝试了几种旋转图像的方法:

  1. MediaCapture.SetPreviewRotation 似乎不适用于 MediaFrameReader(并且我还没有找到任何方法可以使用 VideoPreview 流来捕获图像,而无需将 CameraPreview 控件附加为接收器)
  2. 通过 MediaCapture.SetEncodingPropertiesAsync 设置它似乎也不起作用(也可能仅在您使用 VideoPreview 流时才起作用)
  3. 旋转 SoftwareBitmap 本身,方法是使用 BitmapEncoder 和 BitmapTransform.Rotation 集将其转换为 PNG 或 BMP 流,并使用 BitmapDecoder 对其进行解码。这会产生具有正确尺寸的正确旋转图像,但完全是黑色

注意:我不想显示旋转的图像,因此我的应用程序 Xaml 中的 RenderTransform/LayoutTransform 不是我在这里寻找的。

c# windows-runtime winui-3
1个回答
0
投票

这里是旋转 WinRT 的 SoftwareBitmap 的示例代码:

// prepare a memory buffer
using var ms = new MemoryStream();

// create an encoder
// optionally set a bitmap transform, here a 180 degrees rotation
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, ms.AsRandomAccessStream());
encoder.BitmapTransform.Rotation = BitmapRotation.Clockwise180Degrees;

// write the software bitmap to the memory buffer
encoder.SetSoftwareBitmap(softwareBitmap);
await encoder.FlushAsync();

// create a decoder and get the rotated result
var decoder = await BitmapDecoder.CreateAsync(ms.AsRandomAccessStream());

// get the new software bitmap
// Bgra8, Premultiplied are optional
// but mandatory if you want to use it in a SoftwareBitmapSource for example
var rotated = await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);

注意这实际上与 WinUI3 无关。

© www.soinside.com 2019 - 2024. All rights reserved.