Search code examples
c#camerawin-universal-appqr-code

Decode Barcode from Image in Universal Windows App


I'm realize an universal windows app on windows tablet (windows 10). This tablet have camera and i want take a photo of qr code and decode it.

So i've tried with Zxing.net, but doesn't work on universal windows app. There's a method to decode a qr code inside a foto.

 var bounds = ApplicationView.GetForCurrentView().VisibleBounds;
                var scaleFactor = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
                var size = new Size(bounds.Width * scaleFactor, bounds.Height * scaleFactor);

                CameraCaptureUI captureUI = new CameraCaptureUI();
                captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
                captureUI.PhotoSettings.CroppedSizeInPixels = new Size(size.Width, size.Height);

                StorageFile file = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);

                if (file != null)
                {
                    //QR code conversion from jepg and return string.
                }

I've visited some links...for example: https://github.com/Redth/ZXing.Net.Mobile and https://archive.codeplex.com/?p=zxingnet#trunk/Clients/WindowsRTDemo/Package.appxmanifest

But nothing work for universal windows app. Solutions ?


Solution

  • But nothing work for universal windows app

    By testing on my side the sample you linked do works.

    Since you didn't provide your code snippet that decode the bar-code, so that I provide the whole project that can work well on my side that you could test and try to find what's wrong with your code snippet.

    Install the ZXing.uwp nuget package firstly, and testing with the following code snippet.

    XAML

    <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
       <Image x:Name="imgshow" Height="150" Width="150"></Image>
       <TextBox x:Name="txtDecoderType"  Height="50" Width="250"></TextBox>
       <TextBox x:Name="txtDecoderContent"  Height="50" Width="250"></TextBox>
       <Button x:Name="btnscan" Click="btnscan_Click" Content=" scan"></Button>
    </StackPanel>
    

    Code behind

    private async void btnscan_Click(object sender, RoutedEventArgs e)
    {
        var bounds = ApplicationView.GetForCurrentView().VisibleBounds;
        var scaleFactor = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
        var size = new Size(bounds.Width * scaleFactor, bounds.Height * scaleFactor);
        CameraCaptureUI captureUI = new CameraCaptureUI();
        captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
        captureUI.PhotoSettings.CroppedSizeInPixels = new Size(size.Width, size.Height);
        StorageFile file = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
        if (file != null)
        {
            //QR code conversion from jepg and return string.
            WriteableBitmap writeableBitmap;    
            using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
            {
                BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);                
                writeableBitmap = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight);
                writeableBitmap.SetSource(fileStream);
                imgshow.Source = writeableBitmap;
            }
            // create a barcode reader instance
            IBarcodeReader reader = new BarcodeReader();
            // detect and decode the barcode inside the  writeableBitmap
            var barcodeReader = new BarcodeReader
            {
                AutoRotate = true,
                Options = { TryHarder = true }
            }; 
            Result result = reader.Decode(writeableBitmap);
            // do something with the result
            if (result != null)
            {
                txtDecoderType.Text = result.BarcodeFormat.ToString();
                txtDecoderContent.Text = result.Text;
            }
        }
    }
    

    Additionally, from 1803, UWP also provide APIs for Barcode scanner, details please reference this tutorial and the official sample.