Xamarin.Forms ZXing BarcodeReaderGeneric returns 扫描图库中的图像时为空

Xamarin.Forms ZXing BarcodeReaderGeneric returns null when scanning an image in the gallery

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

首先,我 select 使用 Xamarin.Essentials MediaPicker 从图库中获取图像,并从该图像的路径中获得具有依赖性 class.

的 RGBLuminance

然后我尝试使用 ZXing BarcodeReaderGeneric class 的 Decode() 方法解码此 RGBLuminance。但是,结果总是 returns null.

这是我的演示项目:https://onedrive.live.com/?authkey=%21AFLjjM91wgZkBGU&cid=58BE2C2C3447FFA2&id=58BE2C2C3447FFA2%219829&parId=root&action=locate

ViewModel 中的命令class:

        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); // result always null.
        }

Android中的依赖方法class:

        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.Height, bitmap.Width, RGBLuminanceSource.BitmapFormat.RGB32);
            }
            return null;
        }

你应该换行

return new RGBLuminanceSource(rgbBytes, bitmap.Height, bitmap.Width, RGBLuminanceSource.BitmapFormat.RGB32);

return new RGBLuminanceSource(rgbBytes, bitmap.Width, bitmap.Height, RGBLuminanceSource.BitmapFormat.RGB32);

为了更准确地使用 RGB 格式,您应该

  • 将RGBLuminanceSource.BitmapFormat.RGB32改为RGBLuminanceSource.BitmapFormat.ARGB32
  • 或更改 rgbBytesList.AddRange(new[] { c.A, c.R, c.G, c.B });到 rgbBytesList.AddRange(new[] { c.R, c.G, c.B, c.A });

你能试试这张图片吗?

Picture One Picture Two