barcodeReader.Decode(writeableBitMap) returning null

安稳与你 提交于 2019-12-11 20:30:12

问题


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.


回答1:


I did it in that way for EAN_13 so I think that for CODE_39 it should work too (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;
    }



回答2:


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.



来源:https://stackoverflow.com/questions/26488036/barcodereader-decodewriteablebitmap-returning-null

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!