UWP SoftwareBitmap ( ) JPEG . , , , .
, ( ):
private async Task GetPreviewFrameAsSoftwareBitmapAsync()
{
var previewProperties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;
var videoFrame = new VideoFrame(BitmapPixelFormat.Bgra8, (int)previewProperties.Width, (int)previewProperties.Height);
using (var currentFrame = await _mediaCapture.GetPreviewFrameAsync(videoFrame))
{
SoftwareBitmap previewFrame = currentFrame.SoftwareBitmap;
EditPixels(previewFrame);
}
}
private unsafe void EditPixels(SoftwareBitmap bitmap)
{
if (bitmap.BitmapPixelFormat == BitmapPixelFormat.Bgra8)
{
const int BYTES_PER_PIXEL = 4;
using (var buffer = bitmap.LockBuffer(BitmapBufferAccessMode.ReadWrite))
using (var reference = buffer.CreateReference())
{
byte* data;
uint capacity;
((IMemoryBufferByteAccess)reference).GetBuffer(out data, out capacity);
var desc = buffer.GetPlaneDescription(0);
for (uint row = 0; row < desc.Height; row++)
{
for (uint col = 0; col < desc.Width; col++)
{
var currPixel = desc.StartIndex + desc.Stride * row + BYTES_PER_PIXEL * col;
var b = data[currPixel + 0];
var g = data[currPixel + 1];
var r = data[currPixel + 2];
data[currPixel + 0] = b;
data[currPixel + 1] = (byte)Math.Min(g + 80, 255);
data[currPixel + 2] = r;
}
}
}
}
}
, GetBuffer:
[ComImport]
[Guid("5b0d3235-4dba-4d44-865e-8f1d0e4fd04d")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
unsafe interface IMemoryBufferByteAccess
{
void GetBuffer(out byte* buffer, out uint capacity);
}
, , .
, SoftwareBitmap JPEG:
private static async Task SaveSoftwareBitmapAsync(SoftwareBitmap bitmap, StorageFile file)
{
using (var outputStream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, outputStream);
encoder.SetSoftwareBitmap(bitmap);
await encoder.FlushAsync();
}
}