protected override bool ProcessFrame(MediaFrameReference frameReference, CameraCapture.ImageProcess processMethod) { this.Result = null; _result = null; // doc here https://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.media.capture.frames.videomediaframe.aspx // says to dispose this softwarebitmap if you access it. using (bitmap = frameReference.VideoMediaFrame.SoftwareBitmap) { try { if (this.buffer == null) { this.buffer = new byte[4 * bitmap.PixelHeight * bitmap.PixelWidth]; } if (processMethod == null) { Result = new object[1] { 1 }; var task = SaveSoftwareBitmapToFile(); task.Wait(); } else { bmpBuffer = bitmap.LockBuffer(BitmapBufferAccessMode.ReadWrite); reference = bmpBuffer.CreateReference(); //ComPtr<IMemoryBufferByteAccess> pBufferByteAccess; sourceImage = new Mat(bitmap.PixelHeight, bitmap.PixelWidth, MatType.CV_8UC4, buffer); //Cv2.CvtColor(sourceImage, sourceImage, ColorConversionCodes.BGRA2BGR); //<Remove> processMethod(sourceImage, out _result); if (_result != null) { Result = _result; } } } catch { } } return(this.Result != null); }
public async Task ProcessFramesAsync( TimeSpan?timeout, CameraCapture.ImageProcess processMethod, bool repeat, Action <T> resultCallback = null, Action preResultCallback = null) { await Task.Run( async() => { var startTime = DateTime.Now; this.Result = default(T); if (this.mediaCapture == null) { this.mediaCapture = await this.CreateMediaCaptureAsync(); } var allStreamProperties = mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview).Select(x => new StreamPropertiesHelper(x)); allStreamProperties = allStreamProperties.OrderByDescending(x => x.Height *x.Width); await mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, allStreamProperties.ElementAt(0).EncodingProperties); var mediaFrameSource = this.mediaCapture.FrameSources[ this.mediaFrameSourceFinder.FrameSourceInfo.Id]; using (var frameReader = await this.mediaCapture.CreateFrameReaderAsync( mediaFrameSource, this.mediaEncodingSubtype)) { bool done = false; await frameReader.StartAsync(); while (!done) { using (var frame = frameReader.TryAcquireLatestFrame()) { preResultCallback?.Invoke(); if (frame != null) { if (this.ProcessFrame(frame, processMethod) && (resultCallback != null)) { resultCallback(this.Result); } } } if (timeout.HasValue) { var timedOut = (DateTime.Now - startTime) > timeout; if (timedOut && (resultCallback != null)) { resultCallback(default(T)); } done = (this.Result != null && !repeat) || (timedOut) || CameraCapture.Stop; } else { done = (this.Result != null && !repeat) || CameraCapture.Stop; } } await frameReader.StopAsync(); CameraCapture.isRunning = false; } } ); }
protected abstract bool ProcessFrame(MediaFrameReference frameReference, CameraCapture.ImageProcess processMethod);