Advertisement
Guest User

Untitled

a guest
May 4th, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. private async Task GetPreviewFrameAsSoftwareBitmapAsync()
  2. {
  3. // Get information about the preview
  4. var previewProperties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;
  5.  
  6. // Create the video frame to request a SoftwareBitmap preview frame
  7. var videoFrame = new VideoFrame(BitmapPixelFormat.Bgra8, (int)previewProperties.Width, (int)previewProperties.Height);
  8.  
  9. // Capture the preview frame
  10. using (var currentFrame = await _mediaCapture.GetPreviewFrameAsync(videoFrame))
  11. {
  12. // Collect the resulting frame
  13. SoftwareBitmap previewFrame = currentFrame.SoftwareBitmap;
  14.  
  15. // Add a simple green filter effect to the SoftwareBitmap
  16. EditPixels(previewFrame);
  17. }
  18. }
  19.  
  20. private unsafe void EditPixels(SoftwareBitmap bitmap)
  21. {
  22. // Effect is hard-coded to operate on BGRA8 format only
  23. if (bitmap.BitmapPixelFormat == BitmapPixelFormat.Bgra8)
  24. {
  25. // In BGRA8 format, each pixel is defined by 4 bytes
  26. const int BYTES_PER_PIXEL = 4;
  27.  
  28. using (var buffer = bitmap.LockBuffer(BitmapBufferAccessMode.ReadWrite))
  29. using (var reference = buffer.CreateReference())
  30. {
  31. // Get a pointer to the pixel buffer
  32. byte* data;
  33. uint capacity;
  34. ((IMemoryBufferByteAccess)reference).GetBuffer(out data, out capacity);
  35.  
  36. // Get information about the BitmapBuffer
  37. var desc = buffer.GetPlaneDescription(0);
  38.  
  39. // Iterate over all pixels
  40. for (uint row = 0; row < desc.Height; row++)
  41. {
  42. for (uint col = 0; col < desc.Width; col++)
  43. {
  44. // Index of the current pixel in the buffer (defined by the next 4 bytes, BGRA8)
  45. var currPixel = desc.StartIndex + desc.Stride * row + BYTES_PER_PIXEL * col;
  46.  
  47. // Read the current pixel information into b,g,r channels (leave out alpha channel)
  48. var b = data[currPixel + 0]; // Blue
  49. var g = data[currPixel + 1]; // Green
  50. var r = data[currPixel + 2]; // Red
  51.  
  52. // Boost the green channel, leave the other two untouched
  53. data[currPixel + 0] = b;
  54. data[currPixel + 1] = (byte)Math.Min(g + 80, 255);
  55. data[currPixel + 2] = r;
  56. }
  57. }
  58. }
  59. }
  60. }
  61.  
  62. [ComImport]
  63. [Guid("5b0d3235-4dba-4d44-865e-8f1d0e4fd04d")]
  64. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  65. unsafe interface IMemoryBufferByteAccess
  66. {
  67. void GetBuffer(out byte* buffer, out uint capacity);
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement