【发布时间】: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);
}
【问题讨论】:
-
我没有彩色条码。但是此回复中的示例适用于空白条形码。请检查我之前完成的类似线程。 stackoverflow.com/questions/59705760/…
标签: android xamarin zxing android-bitmap zxing.net