internal Result Decode(SoftwareBitmapLuminanceSource luminanceSource) { var binaryBitmap = CreateBinarizer(luminanceSource); var result = Reader.Decode(binaryBitmap); if (result != null && ResultFound != null) { ResultFound(result); } return(result); }
public async Task StartScanningAsync(Action <Result> scanCallback, MobileBarcodeScanningOptions options) { if (stopping) { return; } displayRequest.RequestActive(); isAnalyzing = true; ScanCallback = scanCallback; ScanningOptions = options; topText.Text = TopText ?? string.Empty; bottomText.Text = BottomText ?? string.Empty; // Find which device to use var preferredCamera = await GetFilteredCameraOrDefaultAsync(ScanningOptions); if (preferredCamera == null) { System.Diagnostics.Debug.WriteLine("No camera available"); isMediaCaptureInitialized = false; return; } if (preferredCamera.EnclosureLocation == null || preferredCamera.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Unknown) { } else { // Only mirror the preview if the camera is on the front panel. mirroringPreview = preferredCamera.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front; } mediaCapture = new MediaCapture(); // Initialize the capture with the settings above try { await mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings { StreamingCaptureMode = StreamingCaptureMode.Video, VideoDeviceId = preferredCamera.Id }); isMediaCaptureInitialized = true; } catch (UnauthorizedAccessException) { System.Diagnostics.Debug.WriteLine("Denied access to the camera"); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception when init MediaCapture: {0}", ex); } if (!isMediaCaptureInitialized) { return; } // Set the capture element's source to show it in the UI captureElement.Source = mediaCapture; // Start the preview await mediaCapture.StartPreviewAsync(); // Get all the available resolutions for preview var availableProperties = mediaCapture.VideoDeviceController .GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview) .Cast <VideoEncodingProperties>(); VideoEncodingProperties previewResolution = null; // Loop through all supported sizes foreach (var sps in availableProperties) { // Find one that's >= 640x360 but <= 1000x1000 // This will likely pick the *smallest* size in that range, which should be fine if (sps.Width >= 640 && sps.Width <= 1000 && sps.Height >= 360 && sps.Height <= 1000) { previewResolution = sps; break; } } if (previewResolution == null) { previewResolution = availableProperties.LastOrDefault(); } if (previewResolution == null) { throw new Exception("No preview resolution available. Camera may be in use by another application."); } // Find the matching property based on the selection, again var chosenProp = availableProperties.FirstOrDefault(ap => ap.Width == previewResolution.Width && ap.Height == previewResolution.Height); // Pass in the requested preview size properties // so we can set them at the same time as the preview rotation // to save an additional set property call await SetPreviewRotationAsync(chosenProp); // *after* the preview is setup, set this so that the UI layout happens // otherwise the preview gets stuck in a funny place on screen captureElement.Stretch = Stretch.UniformToFill; await SetupAutoFocus(); var zxing = ScanningOptions.BuildBarcodeReader(); timerPreview = new Timer(async(state) => { var delay = 300; if (stopping || processing || !isAnalyzing || (mediaCapture == null || mediaCapture.CameraStreamState != CameraStreamState.Streaming)) { timerPreview.Change(delay, Timeout.Infinite); return; } processing = true; SoftwareBitmapLuminanceSource luminanceSource = null; try { // Get preview var frame = await mediaCapture.GetPreviewFrameAsync(videoFrame); // Create our luminance source luminanceSource = new SoftwareBitmapLuminanceSource(frame.SoftwareBitmap); } catch { } Result result = null; try { // Try decoding the image if (luminanceSource != null) { result = zxing.Decode(luminanceSource); } } catch { } // Check if a result was found if (result != null && !string.IsNullOrEmpty(result.Text)) { if (!ContinuousScanning) { delay = Timeout.Infinite; await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { await StopScanningAsync(); }); } else { delay = ScanningOptions.DelayBetweenContinuousScans; } LastScanResult = result; ScanCallback(result); } processing = false; timerPreview.Change(delay, Timeout.Infinite); }, null, 300, Timeout.Infinite); }
static BinaryBitmap CreateBinarizer(SoftwareBitmapLuminanceSource luminanceSource) { return(new BinaryBitmap(luminanceSource)); }