c# - Access preview frame from MediaCapture -
i grab preview frames displayed inside captureelement
xaml element. source
of captureelement
set mediacapture
object , use startpreview()
method start displaying camera. access frames being shown without saving them img or video file. goal capture 10 fps preview , send each frame class accepts byte[].
i tried using capturephototostoragefileasync
method not feasible option not want take 10 actual images / second. not want use screencapture
stores captured video file. ideally not want store media files temporarily on phone. after looking @ msdn mediacapture
, noticed there's method called getpreviewframeasync()
method not exist inside windows phone 8.1. stumbled on example not understand how works.
any suggestions on how approach appreciated.
there sample on microsoft github page relevant, although target windows 10. may interested in migrating project functionality.
getpreviewframe: sample capture preview frames opposed full-blown photos. once has preview frame, can edit pixels on it.
here relevant part:
private async task getpreviewframeassoftwarebitmapasync() { // information preview var previewproperties = _mediacapture.videodevicecontroller.getmediastreamproperties(mediastreamtype.videopreview) videoencodingproperties; // create video frame request softwarebitmap preview frame var videoframe = new videoframe(bitmappixelformat.bgra8, (int)previewproperties.width, (int)previewproperties.height); // capture preview frame using (var currentframe = await _mediacapture.getpreviewframeasync(videoframe)) { // collect resulting frame softwarebitmap previewframe = currentframe.softwarebitmap; // add simple green filter effect softwarebitmap editpixels(previewframe); } } private unsafe void editpixels(softwarebitmap bitmap) { // effect hard-coded operate on bgra8 format if (bitmap.bitmappixelformat == bitmappixelformat.bgra8) { // in bgra8 format, each pixel defined 4 bytes const int bytes_per_pixel = 4; using (var buffer = bitmap.lockbuffer(bitmapbufferaccessmode.readwrite)) using (var reference = buffer.createreference()) { // pointer pixel buffer byte* data; uint capacity; ((imemorybufferbyteaccess)reference).getbuffer(out data, out capacity); // information bitmapbuffer var desc = buffer.getplanedescription(0); // iterate on pixels (uint row = 0; row < desc.height; row++) { (uint col = 0; col < desc.width; col++) { // index of current pixel in buffer (defined next 4 bytes, bgra8) var currpixel = desc.startindex + desc.stride * row + bytes_per_pixel * col; // read current pixel information b,g,r channels (leave out alpha channel) var b = data[currpixel + 0]; // blue var g = data[currpixel + 1]; // green var r = data[currpixel + 2]; // red // boost green channel, leave other 2 untouched data[currpixel + 0] = b; data[currpixel + 1] = (byte)math.min(g + 80, 255); data[currpixel + 2] = r; } } } } }
and declare outside class:
[comimport] [guid("5b0d3235-4dba-4d44-865e-8f1d0e4fd04d")] [interfacetype(cominterfacetype.interfaceisiunknown)] unsafe interface imemorybufferbyteaccess { void getbuffer(out byte* buffer, out uint capacity); }
and of course, project have allow unsafe code of work.
have closer @ sample see how details. or, have walkthrough, can watch camera session recent //build/ conference, includes little bit of walkthrough through camera samples.
Comments
Post a Comment