This project has moved. For the latest updates, please go here.

WinRT Decode() always returns null

Apr 10, 2014 at 4:37 PM
Title says it. I've tried quite a few times. I've gotten plenty of good shots of even very simple bar codes. I turned on the Options.TryHarder and the Options.UseCode39RelaxedExtendedMode for testing. I also tried specifying the possible formats hoping to eliminate bar code type recognition.
         var w = new CameraCaptureUI();
         w.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
         w.PhotoSettings.MaxResolution = CameraCaptureUIMaxPhotoResolution.HighestAvailable;

         var file = await w.CaptureFileAsync(CameraCaptureUIMode.Photo);

         if (file != null) {
            
            var br = new BarcodeReader();
            var img = new BitmapImage();
            WriteableBitmap wrb;
            
            using (var fileStream = await file.OpenAsync(FileAccessMode.Read)) {
               // There must be a better way to get the width/height in order to create the wrb?
               // TODO: Stop making two bitmaps here!
               await img.SetSourceAsync(fileStream);
               wrb = new WriteableBitmap(img.PixelWidth, img.PixelHeight);
               fileStream.Seek(0);
               await wrb.SetSourceAsync(fileStream);
            }

            // Test 1
            br.Options.PossibleFormats = new List<BarcodeFormat> { 
               BarcodeFormat.CODE_128, 
               BarcodeFormat.CODE_39, 
               BarcodeFormat.DATA_MATRIX 
            };

            // Test 2
            br.Options.TryHarder = true;

            // Test 3
            br.Options.UseCode39RelaxedExtendedMode = true;

            var res = br.Decode(wrb);

            // Although this is indended to scan a VIN, any scan should return SOMETHING here. It never does.
            var vin = res == null ? string.Empty : res.Text;

            if ((vin.Length == 18) && (vin.StartsWith("I"))) {
               vin = vin.Substring(1);
            }

         }
Coordinator
Apr 11, 2014 at 7:05 PM
Can you provide a sample image which should work but doesn't?
Apr 11, 2014 at 9:32 PM
Any of these fail with the code above. These are the images captured with the CameraCaptureUI.

https://drive.google.com/folderview?id=0B7Bv2lpxvQHnSGVOYTgwNzRwT28&usp=sharing
Apr 15, 2014 at 2:38 PM
One more thing to note here. I just finished adding ZXing to my Windows Phone 8 application as well. The exact same thing happens. No result is ever given back to me. I did try small images of simple bar codes (in the link above). If you look at the code I have you'll see that any code that is not 17 or 18 characters is discarded, however I was running these tests in a debugger and the Decode() method always returns null.

I have also integrated ZXing into an Android application, but in that case the application actually shells out to the ZXing program and doesn't incorporate a component into itself. The Android application works very well.
Coordinator
Apr 15, 2014 at 8:13 PM
I tried you samples with the WinRT demo and most of them work.
https://zxingnet.svn.codeplex.com/svn/trunk/Clients/WindowsRTDemo
The demo uses the following code
         var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"Assets\1.jpg");
         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);
         CaptureImage.Source = writeableBmp;
         VideoCapture.Visibility = Visibility.Collapsed;
         CaptureImage.Visibility = Visibility.Visible;

         _result = ScanBitmap(writeableBmp);
         if (_result != null)
         {
            ScanResult.Text += _result.Text;
         }
         return;
      }

      private Result ScanBitmap(WriteableBitmap writeableBmp)
      {
         var barcodeReader = new BarcodeReader
         {
            TryHarder = true,
            AutoRotate = true
         };
         var result = barcodeReader.Decode(writeableBmp);

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

         return result;
      }
The only real big difference is the initialization of br.Options.PossibleFormats.
I don't restrict it.
Your samples are decoded as UPC_A. You should remove the initialization of PossibleFormats
or you add the BarcodeFormat.UPC_A.
Apr 15, 2014 at 9:03 PM
That was only part of my testing (note the comment above). It's not in the code. And in Windows Phone 8 I never even tried this way. A barcode simply never decodes.
Apr 16, 2014 at 7:04 PM
I moved the code in the Windows Store app to a different event on the page. It's in the OnNavigatedTo event now. I got a code to scan finally. I didn't change any other code, so I can't figure out why this worked. I'll mark this as resolved.
Marked as answer by cantak on 4/16/2014 at 12:04 PM
May 2, 2014 at 8:25 AM
This code only decodes the QR code . but for Bar code (ISBN), it always shows null . Please check this issue because for windows phone it is working fine but in WinRt it is giving null always. Please help me in this issue.

Thanks !
May 2, 2014 at 1:08 PM
It doesn't work well. In fact, the Windows Store and Windows Phone version of ZXing work so terribly, I had to pull bar code scanning from my applications. Meanwhile, Android works fine. You need to do the scanning in a loop. I found that only 1 in 10 perfectly focused frames will actually scan. This isn't too noticeable when you're processing 6 frames per second or so. But yes, it is bad. It's not you, it's the component.