我正在尝试从 WinUI3 应用程序中的网络摄像头捕获图像。 经过一番尝试和错误后,我终于成功地通过 MediaFrameReader 做到了这一点。 现在,我已将捕获的图像作为 SoftwareBitmap,并且我想将这些图像提供给 OCR,因此我需要旋转它们。
我已经尝试了几种旋转图像的方法:
注意:我不想显示旋转的图像,因此我的应用程序 Xaml 中的 RenderTransform/LayoutTransform 不是我在这里寻找的。
这里是旋转 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 无关。