我从运行视频捕获一个帧并将其转换为SoftwareBitmap以用于进一步的目的。在此之前,我想将该框架裁剪成矩形。怎么可能?
var thumbnail = await GetThumbnailAsync(file,seek_position);
StringBuilder ocr=null;
InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
await RandomAccessStream.CopyAsync(thumbnail, randomAccessStream);
randomAccessStream.Seek(0);
SoftwareBitmap inputBitmap;
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(randomAccessStream);
// Get the SoftwareBitmap representation of the file
inputBitmap = await decoder.GetSoftwareBitmapAsync();
//crop inputBitmap
public async Task<IInputStream> GetThumbnailAsync(StorageFile file,int i)
{
//int duration_millisecond = i * 1000;
var mediaClip = await MediaClip.CreateFromFileAsync(file);
var mediaComposition = new MediaComposition();
mediaComposition.Clips.Add(mediaClip);
return await mediaComposition.GetThumbnailAsync(
TimeSpan.FromMilliseconds(i), 0, 0, VideoFramePrecision.NearestFrame);
}
答案 0 :(得分:1)
BitmapDecoder对象的GetSoftwareBitmapAsync
方法有几个重载方法。您可以使用GetSoftwareBitmapAsync(BitmapPixelFormat, BitmapAlphaMode, BitmapTransform, ExifOrientationMode, ColorManagementMode)
方法裁剪软件位图。您只需要为它定义一个BitmapTransform对象。
请参阅以下代码示例:
SoftwareBitmap inputBitmap;
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(randomAccessStream);
// Get the SoftwareBitmap representation of the file
inputBitmap = await decoder.GetSoftwareBitmapAsync(decoder.BitmapPixelFormat,BitmapAlphaMode.Ignore,new BitmapTransform() {Bounds=new BitmapBounds() {X=100,Y=200,Width=200,Height=100 } },ExifOrientationMode.IgnoreExifOrientation,ColorManagementMode.DoNotColorManage);
您只需要为其Bounds属性指定一个新的BitmapBounds
。
请注意,在此步骤中,您有一个裁剪的软件位图,但如果您想使用它初始化SoftwareBitmapSource
并将其显示在Image
控件中。您将收到异常“ SetBitmapAsync仅支持具有正宽度/高度,bgra8像素格式和预乘或无alpha 的SoftwareBitmap。”。您需要使用SoftwareBitmap _softbitmap = SoftwareBitmap.Convert()
创建一个新的软件位图,如下所示:
SoftwareBitmap _softbitmap = SoftwareBitmap.Convert(inputBitmap,BitmapPixelFormat.Bgra8,BitmapAlphaMode.Premultiplied);
var source = new SoftwareBitmapSource();
await source.SetBitmapAsync(_softbitmap);
image.Source = source; //image is a Image control