barcodeReader.Decode(writeableBitMap)返回null

时间:2014-10-21 13:34:06

标签: winrt-xaml zxing

当我尝试条形码时,我告诉它是一个CASE39条形码,我总是从解码中返回null

  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;
     }

我最近添加了Options的代码,但似乎没有任何东西改变来自Decode函数的输出。我正在为运行Windows 8.1 metro app xaml的平板电脑上的应用程序编写此代码。

2 个答案:

答案 0 :(得分:0)

这个问题的答案是不使用Zxing条码扫描器sdk并下载Easy Barcode Scanner Free Windows应用程序,因为它会解码条形码并且更容易实现。

答案 1 :(得分:0)

我是以这种方式为EAN_13做的,所以我认为对于CODE_39它也应该工作(Windows RT):

    private async Task<Result> ScanBitmap(WriteableBitmap writeableBmp)
    {
        var barcodeReader = new BarcodeReader
        {
            AutoRotate = true,
            Options =
            {
                TryHarder = true,
                PossibleFormats = new List<BarcodeFormat> {BarcodeFormat.EAN_13}
            }
        };

        var pixels = await ConvertBitmapToByteArray(writeableBmp);

        var result = barcodeReader.Decode(new RGBLuminanceSource(pixels, writeableBmp.PixelWidth, writeableBmp.PixelHeight, RGBLuminanceSource.BitmapFormat.BGRA32));

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

        return result;
    }

    private async Task<byte[]> ConvertBitmapToByteArray(WriteableBitmap bitmap)
    {
        var pixelStream = bitmap.PixelBuffer.AsStream();
        var pixels = new byte[pixelStream.Length];
        await pixelStream.ReadAsync(pixels, 0, pixels.Length);
        return pixels;
    }