Search code examples
winrt-xamlzxing

barcodeReader.Decode(writeableBitMap) returning null


When I try a barcode that I've told is a CASE39 barcode I always get null returned from the decode

  if (cbBarcode.IsChecked == true)
            {
                var photoStorageFile = await KnownFolders.PicturesLibrary.CreateFileAsync("scan.jpg", CreationCollisionOption.GenerateUniqueName);

                Size aspectRatio = new Size(3, 1);
                dialog.PhotoSettings.CroppedAspectRatio = aspectRatio;
                StorageFile file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);
                string ImageValues = "bbc_photo" + x;
                var stream = await file.OpenReadAsync();
                // initialize with 1,1 to get the current size of the image
                var writeableBmp = new WriteableBitmap(1, 1);
                writeableBmp.SetSource(stream);
                // and create it again because otherwise the WB isn't fully initialized and decoding
                // results in a IndexOutOfRange
                writeableBmp = new WriteableBitmap(writeableBmp.PixelWidth, writeableBmp.PixelHeight);
                stream.Seek(0);
                writeableBmp.SetSource(stream);
                var result = ScanBitMap(writeableBmp);
                if (result != null)
                {
                    MessageDialog dialog2 = new MessageDialog(result.Text.ToString());
                    await dialog2.ShowAsync();
                    //photoStorageFile = writeableBmp;
                }
                else
                {
                    MessageDialog errdialog = new MessageDialog("Error reading barcode.. Please try again");
                    await errdialog.ShowAsync();
                }
                return;



     private Result ScanBitMap(WriteableBitmap writeableBmp)
     {
         var barcodeReader = new BarcodeReader
         {
             AutoRotate = true,
             Options = new DecodingOptions
             {
                 TryHarder = true,
                 // restrict to one or more supported types, if necessary
                 PossibleFormats = new []
                  {
                    BarcodeFormat.CODE_39
                   }
             }
         };
         var result = barcodeReader.Decode(writeableBmp);

         if (result != null)
         {
             CapturedPhoto.Source = writeableBmp;
         }

         return result;
     }

I recently added the code for Options but nothing seems to be changing the output coming from the Decode function. I am writing this for an app on the tablet that runs windows 8.1 metro app xaml.


Solution

  • The answer to this question is to not use Zxing barcode scanner sdk and download the Easy Barcode Scanner Free windows app because it will decode barcodes and is easier to implement.