简体   繁体   中英

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

I am developing a barcode reader with Xamarin.Forms. And I'm trying to scan the image on Android device.

First I select the image from the gallery with Xamarin.Essentials MediaPicker and from the path of this image I get an RGBLuminance with the Dependency class.

Then I am trying to decode this RGBLuminance with the Decode() method of the ZXing BarcodeReaderGeneric class. However, the result always returns null.

It is my demo project: https://onedrive.live.com/?authkey=%21AFLjjM91wgZkBGU&cid=58BE2C2C3447FFA2&id=58BE2C2C3447FFA2%219829&parId=root&action=locate

Command in the 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.
        }

Method of Dependency class in Android:

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

You should change the line

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

to

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

And to be more accurate with the RGB format you should

  • change RGBLuminanceSource.BitmapFormat.RGB32 to RGBLuminanceSource.BitmapFormat.ARGB32
  • or change rgbBytesList.AddRange(new[] { c.A, c.R, c.G, c.B }); to rgbBytesList.AddRange(new[] { c.R, c.G, c.B, c.A });

Can you try with this pictures?

Picture One Picture Two

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM