【问题标题】:ZXing BarcodeReader Don't Decode Some BarcodesZXing BarcodeReader 不解码某些条码
【发布时间】:2021-04-16 06:43:31
【问题描述】:

我正在使用 Xamarin.Forms 开发条形码阅读器。我正在尝试在 Android 设备上扫描图像。

首先,我使用 Xamarin.Essentials MediaPicker 从图库中选择图像,并从该图像的路径中获得一个带有 Dependency 类的 RGBLuminance。

然后我尝试使用 ZXing BarcodeReaderGeneric 类的 Decode() 方法解码这个 RGBLuminance。

应用程序成功解码了某些图像中的条形码。但是,有时它在解码时返回 null。在将图像转换为位图或创建 RGBLuminanceSource 时,我可能犯了一个错误。

我想知道一个可以解码彩色、黑白和灰度图像的类应该是怎样的。

        public RGBLuminanceSource GetRGBLuminanceSource(string imagePath)
        {
            
            if (File.Exists(imagePath))
            {
                Android.Graphics.Bitmap bitmap = BitmapFactory.DecodeFile(imagePath);
                List<byte> rgbBytesList = new List<byte>();
                for (int y = 0; y < bitmap.Height; y++)
                {
                    for (int x = 0; x < bitmap.Width; x++)
                    {
                        var c = new Android.Graphics.Color(bitmap.GetPixel(x, y));
                        rgbBytesList.AddRange(new[] { c.A, c.R, c.G, c.B });
                    }
                }
                byte[] rgbBytes = rgbBytesList.ToArray();
                return new RGBLuminanceSource(rgbBytes, bitmap.Width, bitmap.Height, RGBLuminanceSource.BitmapFormat.RGB32);
            }
            return null;
        }

ViewModel 类中的命令:

        public ICommand PickCommand => new Command(PickImage);
        private async void PickImage()
        {
            var pickResult = await MediaPicker.PickPhotoAsync(new MediaPickerOptions
            {
                Title = "Select a barcode."
            });
            var path = pickResult.FullPath;

            var RGBLuminance = DependencyService.Get<ILuminance>().GetRGBLuminanceSource(path);
            var reader = new BarcodeReaderGeneric();
            var result = reader.Decode(RGBLuminance); 
        }

【问题讨论】:

标签: android xamarin zxing android-bitmap zxing.net


【解决方案1】:

我在 xamarin.android 中使用此代码,但从未遇到过问题:

        var scanner = new MobileBarcodeScanner();
        var result = await scanner.Scan(_context, MobileBarcodeScanningOptions.Default);

它打开相机,用户拍摄条码照片,result.Text 包含扫描的条码。

【讨论】:

  • 我想解码从图库中选择的图像。不是来自相机。
猜你喜欢
  • 1970-01-01
  • 2014-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-15
  • 1970-01-01
  • 2016-04-09
  • 2019-08-19
相关资源
最近更新 更多