/// <summary> /// Converts the given image in NV12 format into a bitmap image. /// </summary> /// <param name="yuvPixelArray">The image data in NV12 format.</param> /// <param name="width">The width of the image in pixels.</param> /// <param name="height">The height of the image in pixels.</param> /// <returns>A BitmapImage instance containing the image.</returns> public static BitmapImage Nv12PixelArrayToBitmapImage(byte[] yuvPixelArray, int width, int height) { BitmapImage bitmapImage = null; byte[] rgbPixelArray = ImageProcessingUtils.Nv12PixelArrayToRgbPixelArray(yuvPixelArray, width, height); if (rgbPixelArray != null) { bitmapImage = new BitmapImage(); InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream(); stream.AsStreamForWrite().Write(rgbPixelArray, 0, rgbPixelArray.Length); stream.Seek(0); try { bitmapImage.SetSource(stream); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); bitmapImage = null; } } return(bitmapImage); }
/// <summary> /// Stores the given image in NV12 format into a file. /// </summary> /// <param name="yuvPixelArray">The image data in NV12 format.</param> /// <param name="width">The width of the image in pixels.</param> /// <param name="height">The height of the image in pixels.</param> /// <param name="fileName">The desired filename.</param> public async static Task NV12PixelArrayToWriteableBitmapFileAsync(byte[] yuvPixelArray, int width, int height, string fileName) { byte[] rgbPixelArray = ImageProcessingUtils.Nv12PixelArrayToRgbPixelArray(yuvPixelArray, width, height); var file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync( fileName, CreationCollisionOption.GenerateUniqueName); using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite)) { Guid BitmapEncoderGuid = BitmapEncoder.JpegEncoderId; BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoderGuid, stream); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)width, (uint)height, 96.0, 96.0, rgbPixelArray); await encoder.FlushAsync(); } }
/// <summary> /// Converts the given image in NV12 format into a writeable bitmap. /// </summary> /// <param name="yuvPixelArray">The image data in NV12 format.</param> /// <param name="width">The width of the image in pixels.</param> /// <param name="height">The height of the image in pixels.</param> /// <returns>A WriteableBitmap instance containing the image.</returns> public async static Task <WriteableBitmap> Nv12PixelArrayToWriteableBitmapAsync(byte[] yuvPixelArray, int width, int height) { WriteableBitmap writeableBitmap = null; byte[] rgbPixelArray = ImageProcessingUtils.Nv12PixelArrayToRgbPixelArray(yuvPixelArray, width, height); if (rgbPixelArray != null) { writeableBitmap = new WriteableBitmap(width, height); using (Stream stream = writeableBitmap.PixelBuffer.AsStream()) { if (stream.CanWrite) { await stream.WriteAsync(rgbPixelArray, 0, rgbPixelArray.Length); stream.Flush(); } } } return(writeableBitmap); }