繁体   English   中英

显示视频帧和 RenderTargetBitmap 位置

[英]Display videoframe and RenderTargetBitmap position

我正在尝试从网络摄像头裁剪图像并显示在相机预览旁边。

裁剪图像应考虑 3 个因素。

  1. 裁剪图像的输出应采用VideoFrame形式
  2. 上面的输出VideoFrame需要显示(在 XAML 上)
  3. 目标裁剪图像位于原始图像的中间

我发现RenderTargetBitmap可以帮助我获得裁剪的图像。

但我仍然不知道如何显示VideoFrame (不保存图像),设置裁剪位置。

我被卡在下面了...

    public async Task<VideoFrame> CroppingImage(Grid grid)
    {
        RenderTargetBitmap renderBitmap = new RenderTargetBitmap();

        await renderBitmap.RenderAsync(grid);
        var buffer = await renderBitmap.GetPixelsAsync();
        var softwareBitmap = SoftwareBitmap.CreateCopyFromBuffer(buffer, BitmapPixelFormat.Bgra8, renderBitmap.PixelWidth, renderBitmap.PixelHeight, BitmapAlphaMode.Ignore);

        buffer = null;
        renderBitmap = null;

        VideoFrame vf = VideoFrame.CreateWithSoftwareBitmap(softwareBitmap);
        await CropAndDisplayInputImageAsync(vf);

        return cropped_vf;
    }


    private async Task CropAndDisplayInputImageAsync(VideoFrame inputVideoFrame)
    {
        //some cropping algorithm here
        //i have a rectangle on a canvas(camera preview is on CaptureElement)
        //I know the left top position and width and height but no idea how to use
    }

有什么帮助吗?

这就是我发现并完成的:)

(假设有一个名为croppedface的视频帧)

        croppedFace = new VideoFrame(BitmapPixelFormat.Bgra8, (int)width, (int)height, BitmapAlphaMode.Ignore);

        await inputVideoFrame.CopyToAsync(croppedFace, cropBounds, null);

        SoftwareBitmap asdf = croppedFace.SoftwareBitmap;
        asdf = SoftwareBitmap.Convert(asdf, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore);
        var qwer = new SoftwareBitmapSource();
        await qwer.SetBitmapAsync(asdf);
        CroppedFaceImage.Source = qwer;

但我仍然不知道如何显示 VideoFrame(不保存图像),设置裁剪位置。

如果要在 xaml 上显示框架,则需要将框架转换为可显示格式并渲染到屏幕上。 请查看官方相机帧示例中FrameRender类。 它有一个ConvertToDisplayableImage方法,应该是你想要的。

然后,您可以在图像控件中显示它。 之后,您可以使用Image.Clip设置要裁剪的位置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM