public static async void ScanFirstCameraForQrCode( Action <Result> resultCallback, TimeSpan timeout) { Result result = null; // Note - I keep this frame processor around which means keeping the // underlying MediaCapture around because when I didn't keep it // around I ended up with a crash in Windows.Media.dll related // to disposing of the MediaCapture. // So...this isn't what I wanted but it seems to work better :-( if (frameProcessor == null) { var mediaFrameSourceFinder = new MediaFrameSourceFinder(); // We want a source of media frame groups which contains a color video // preview (and we'll take the first one). var populated = await mediaFrameSourceFinder.PopulateAsync( MediaFrameSourceFinder.ColorVideoPreviewFilter, MediaFrameSourceFinder.FirstOrDefault); if (populated) { // We'll take the first video capture device. var videoCaptureDevice = await VideoCaptureDeviceFinder.FindFirstOrDefaultAsync(); if (videoCaptureDevice != null) { // Make a processor which will pull frames from the camera and run // ZXing over them to look for QR codes. frameProcessor = new QrCaptureFrameProcessor( mediaFrameSourceFinder, videoCaptureDevice, MediaEncodingSubtypes.Bgra8); // Remember to ask for auto-focus on the video capture device. frameProcessor.SetVideoDeviceControllerInitialiser( vd => vd.Focus.TrySetAuto(true)); } } } if (frameProcessor != null) { // Process frames for up to 30 seconds to see if we get any QR codes... await frameProcessor.ProcessFramesAsync(timeout); // See what result we got. result = frameProcessor.QrZxingResult; } // Call back with whatever result we got. resultCallback(result); }
public static async void ScanFirstCameraForQrCode( Action <Result> resultCallback, TimeSpan timeout) { Result result = null; var mediaFrameSourceFinder = new MediaFrameSourceFinder(); // We want a source of media frame groups which contains a color video // preview (and we'll take the first one). var populated = await mediaFrameSourceFinder.PopulateAsync( MediaFrameSourceFinder.ColorVideoPreviewFilter, MediaFrameSourceFinder.FirstOrDefault); if (populated) { // We'll take the first video capture device. var videoCaptureDevice = await VideoCaptureDeviceFinder.FindFirstOrDefaultAsync(); if (videoCaptureDevice != null) { // Make a processor which will pull frames from the camera and run // ZXing over them to look for QR codes. var frameProcessor = new QrCaptureFrameProcessor( mediaFrameSourceFinder, videoCaptureDevice, MediaEncodingSubtypes.Bgra8); // Remember to ask for auto-focus on the video capture device. frameProcessor.SetVideoDeviceControllerInitialiser( vd => vd.Focus.TrySetAuto(true)); // Process frames for up to 30 seconds to see if we get any QR codes... await frameProcessor.ProcessFramesAsync(timeout); // See what result we got. result = frameProcessor.QrZxingResult; } } // Call back with whatever result we got. resultCallback(result); }
async void OnStartOcrAsync(object sender, RoutedEventArgs e) { this.txtResult.Text = "Starting..."; var videoCaptureDeviceFinder = new VideoCaptureDeviceFinder(); #if HOLOLENS await videoCaptureDeviceFinder.InitialiseAsync( devices => devices.First()); #else await videoCaptureDeviceFinder.InitialiseAsync( devices => devices.Where(device => device.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front).Single()); #endif // HOLOLENS if (videoCaptureDeviceFinder.HasDevice) { var mediaFrameSourceFinder = new MediaFrameSourceFinder(videoCaptureDeviceFinder); #if HOLOLENS var filters = new FilterSet() .Append(MediaFrameSourceFinder.VideoPreviewFilter, MediaFrameSourceFinder.ColorFilter) .Append(m => ((m.Width == 1280) && (m.FrameRate >= 30))); #else var filters = new FilterSet() .Append(MediaFrameSourceFinder.VideoPreviewFilter, MediaFrameSourceFinder.ColorFilter) .Append(m => ((m.Width == 1920) && (m.FrameRate >= 30))); #endif // HOLOLENS await mediaFrameSourceFinder.InitialiseAsync( matchingMediaSourceInfos => matchingMediaSourceInfos.First(), filters); var ocrScanner = new DeviceAndCloudOcrScanner( mediaFrameSourceFinder, new Regex(IP_ADDRESS_REGEX)); this.txtResult.Text = "Matching with the local device camera..."; using (var matchedResult = await ocrScanner.MatchOnDeviceAsync(TimeSpan.FromSeconds(10))) { if (matchedResult.ResultType == OcrMatchResult.Succeeded) { // We found what we were looking for, finished! this.txtResult.Text = $"Found result {matchedResult.MatchedText}"; } else if (matchedResult.ResultType == OcrMatchResult.TimedOutCloudCallAvailable) { // We haven't matched but we have got a frame with at least some text in // it which the on-device OCR hasn't matched. // So...we can try the cloud. this.txtResult.Text = $"Calling cloud..."; var result = await ocrScanner.MatchOnCloudAsync( AZURE_TEXT_RECOGNITION_API, AZURE_COMPUTER_VISION_KEY, matchedResult, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(30)); if (result.ResultType == OcrMatchResult.Succeeded) { this.txtResult.Text = $"Found result {result.MatchedText}"; } else { this.txtResult.Text = $"Didn't work {result.ResultType}"; } } else { this.txtResult.Text = $"Result returned {matchedResult.ResultType}"; } } } }