简体   繁体   English

HoloLens 2 UWP 位图图像伪像

[英]HoloLens 2 UWP bitmap image artifacting

I am building a 2D UWP app to run on HoloLens 2. In the app I am using RenderTargetBitmap to render a bitmap of video feed UI object to get a still from it as a SoftwareBitmap object.我正在构建一个 2D UWP 应用程序以在 HoloLens 2 上运行。在应用程序中,我使用 RenderTargetBitmap 来渲染视频源 UI 对象的位图,以从中获取作为 SoftwareBitmap 对象的静止图像。 The frustrating part of this is that when running it in x86 mode on PC it works fine.令人沮丧的部分是,在 PC 上以 x86 模式运行它时,它工作正常。 However when I run on Hololens 2 (ARM) the image is garbled with an artifact that screws up the image.但是,当我在 Hololens 2 (ARM) 上运行时,图像会出现乱码,并且出现了弄乱图像的伪像。 It appears to be split into mis-matching lines.它似乎被分成了不匹配的行。 I added the ability to save the image file out to a folder on the device and the outputted image looks fine but when it is set as an imageSource in the app UI it looks all screwed up.我添加了将图像文件保存到设备上的文件夹的功能,输出的图像看起来不错,但是当它在应用程序 UI 中设置为 imageSource 时,它​​看起来全都搞砸了。

In my VideoControl.xaml.cs codebehind:在我的 VideoControl.xaml.cs 代码隐藏中:

    private static async Task SetPauseImageSource()
    {
        await Instance.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
        {
            SoftwareBitmap image = await Xaml2Bitmap.CreateBitmapFromElement(Instance, true);
            var source = new SoftwareBitmapSource();
            await source.SetBitmapAsync(image);
            Instance.imagePause.Source = source;
        });         
    }

In my Xaml2Bitmap.cs class:在我的 Xaml2Bitmap.cs 类中:

    public static async Task<SoftwareBitmap> CreateBitmapFromElement(FrameworkElement uielement, bool saveFile = false, string filenamePrefix = "image")
    {           
        SoftwareBitmap bitmap = null;
        var renderTargetBitmap = new RenderTargetBitmap();
        await renderTargetBitmap.RenderAsync(uielement);
        var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();
        bitmap = SoftwareBitmap.CreateCopyFromBuffer(pixelBuffer, BitmapPixelFormat.Bgra8, (int)uielement.ActualWidth, (int)uielement.ActualHeight, BitmapAlphaMode.Ignore);

        if (bitmap == null)
        {
            Debug.WriteLine("Bitmap is null");
        }

        if (saveFile)
        {
            var file = await SaveFile(filenamePrefix);

            using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi;
                var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
                encoder.SetPixelData(
                    BitmapPixelFormat.Bgra8,
                    BitmapAlphaMode.Ignore,
                    (uint)renderTargetBitmap.PixelWidth,
                    (uint)renderTargetBitmap.PixelHeight,
                    logicalDpi,
                    logicalDpi,
                    pixelBuffer.ToArray());
                        
                await encoder.FlushAsync();
            }
        }
        return bitmap;
    }

    private static async Task<StorageFile> SaveFile(string filenamePrefix)
    {
        var savePicker = new FileSavePicker();
        savePicker.DefaultFileExtension = ".png";
        savePicker.FileTypeChoices.Add(".png", new List<string> { ".png" });
        savePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        savePicker.SuggestedFileName = filenamePrefix + ".png";

        // Prompt the user to select a file
        var saveFile = await savePicker.PickSaveFileAsync();
        return saveFile;
    }     

HoloLens 2 上的图像外观

Any help on this is much appreciated as always!一如既往地感谢您对此的任何帮助!

This ended up fixing it.这最终修复了它。 I needed to save the bitmap file to the device and read it back in. Not ideal but it works.我需要将位图文件保存到设备并重新读取它。不理想但它有效。

    public static async Task<string> CreateBitmapFromElement(FrameworkElement uielement)
    {
        SoftwareBitmap bitmap = null;
        var renderTargetBitmap = new RenderTargetBitmap();
        await renderTargetBitmap.RenderAsync(uielement);
        var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();
        bitmap = SoftwareBitmap.CreateCopyFromBuffer(pixelBuffer, BitmapPixelFormat.Bgra8, (int)uielement.ActualWidth, (int)uielement.ActualHeight, BitmapAlphaMode.Ignore);

        if (bitmap == null)
        {
            Debug.WriteLine("Bitmap is null");
        }

        var folder = KnownFolders.PicturesLibrary; 
        var file = await folder.CreateFileAsync("pause.bmp", CreationCollisionOption.ReplaceExisting);
       
        using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
        {
            var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi;
            var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
            encoder.SetPixelData(
                BitmapPixelFormat.Bgra8,
                BitmapAlphaMode.Ignore,
                (uint)renderTargetBitmap.PixelWidth,
                (uint)renderTargetBitmap.PixelHeight,
                logicalDpi,
                logicalDpi,
                pixelBuffer.ToArray());

            await encoder.FlushAsync();


            await stream.WriteAsync(pixelBuffer);

        }

        return file.Name;
    }

    private static async Task SetImageSource()
    {
        await Instance.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
        {
            StorageFolder storageFolder = KnownFolders.PicturesLibrary;
            string imagePath = await Xaml2Bitmap.CreateBitmapFromElement(Instance);
            var file = await storageFolder.GetFileAsync(imagePath);

            using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
            {
                BitmapImage bitmapImage = new BitmapImage();
                await bitmapImage.SetSourceAsync(fileStream);
                Instance.imagePause.Source = bitmapImage;
            }
        });         
    }

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

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