ZXing WinRT QR scanner with overlay

Mar 3, 2015 at 1:52 AM
I have successfully implement a QR scanner with Zxing library, but I can't create a custom overlay to read the QR scanner on the centered area of the screen. Currently, as long the QR code is detected in any region of the screen it will be read.

private async Task InitializeQrCode()
    {
        try
        {
            // Find all available webcams
            DeviceInformationCollection webcamList = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);

            // Get the proper webcam (back camera one)
            DeviceInformation backWebcam = (from webcam in webcamList
                                            where webcam.EnclosureLocation != null && webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back
                                            select webcam).FirstOrDefault();

            // Initializing MediaCapture
            _mediaCapture = new MediaCapture();
            await _mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings
            {
                VideoDeviceId = backWebcam.Id,
                AudioDeviceId = "",
                StreamingCaptureMode = StreamingCaptureMode.Video,
                PhotoCaptureSource = PhotoCaptureSource.VideoPreview
            });

            // Set the source of CaptureElement to MediaCapture
            captureElement.Source = _mediaCapture;
            await _mediaCapture.StartPreviewAsync();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

    private async void QrScanner_Loaded(object sender, RoutedEventArgs e)
    {
        await InitializeQrCode();
        readingStatus = true;

        var bounds = Window.Current.Bounds;

        double screenWidth = bounds.Width;
        double screenHeight = bounds.Height;

        System.Diagnostics.Debug.WriteLine(screenWidth);
        System.Diagnostics.Debug.WriteLine(screenHeight);

        uint tempW = Convert.ToUInt32(screenWidth);
        uint tempH = Convert.ToUInt32(screenHeight);

        var imgProp = new ImageEncodingProperties { Subtype = "BMP", Width = tempW, Height = tempH };
        var bcReader = new BarcodeReader();

        while (readingStatus)
        {
            var stream = new InMemoryRandomAccessStream();
            await _mediaCapture.CapturePhotoToStreamAsync(imgProp, stream);
            stream.Seek(0);

            var wbm = new WriteableBitmap(Convert.ToInt32(screenWidth), Convert.ToInt32(screenHeight));
            await wbm.SetSourceAsync(stream);

            var result = bcReader.Decode(wbm);

            if (result != null)
            {
                await _mediaCapture.StopPreviewAsync();
                // The URI to launch
                localSettings.Values["UrlValue"] = @result.Text;
                this.Frame.Navigate(typeof(HiltiOnline));
                readingStatus = false;
            }

        }
    }
Coordinator
Mar 5, 2015 at 7:30 PM
You have to crop the overlay region out of the captured image before you try to decode it.