[英]Reading QR code in Xamarin Forms With Zxing
我正在尝试从.png 文件导入二维码并使用Zxing.Net.Mobile和ZXing.Net.Mobile.Forms对其进行解码。
如果我使用ZXing.Mobile.MobileBarcodeScanner
类扫描二维码,解码会按要求工作,但是,从文件导入时, ZXing.QrCode.QRCodeReader()
码阅读器 ( ZXing.QrCode.QRCodeReader()
) 解码函数始终返回null
。
当我使用Xamarin Forms 时; 每个平台处理位图/图像创建,可移植部分处理其余部分( Zxing BinaryBitmap
创建和解码)。
//Store rawBytes and image demensions
PotableBitmap bMap = DependencyService.Get<IBitmap>().FileToBitmap(filePath);
RGBLuminanceSource source = new RGBLuminanceSource(bMap.RgbBytes, bMap.Width, bMap.Height, RGBLuminanceSource.BitmapFormat.RGB32);
HybridBinarizer binarized = new HybridBinarizer(source);
BinaryBitmap bitmap = new BinaryBitmap(binarized);
var reader = new ZXing.QrCode.QRCodeReader();
data = reader.decode(qrCodeBitmap); // This is always null
DependencyService
会调用平台特定的函数,目前我正在使用 Andorid 所以,函数如下:
public PortableBitmap FileToBitmap(string ms)
{
var bytes = File.ReadAllBytes(ms);
Android.Graphics.Bitmap bMap = BitmapFactory.DecodeByteArray(bytes, 0, bytes.Length);
int[] intArray = new int[bMap.Width * bMap.Height];
bMap.GetPixels(intArray, 0, bMap.Width, 0, 0, bMap.Width, bMap.Height)
List<byte> result = new List<byte>();
foreach (int intRgb in intArray)
{
Color pixelColor = new Color(intRgb);
result.Add(pixelColor.R);
result.Add(pixelColor.G);
result.Add(pixelColor.B);
}
return new PortableBitmap(result.ToArray(), bMap.Width, bMap.Height);
}
我浏览了一些关于 SO 的帖子,这些帖子有同样的问题,并尝试了以下方法:
BitmapLuminanceSource
:仍然返回null
并需要使用另一个库RGBLuminanceSource
使用不同的位图格式:RGB32、BGR32、ARGB32、ABGR32(每次更改 FileToBitmap 函数)Binarizer
, GlobalHistogramBinarizer()
MultiFormatReader()
与 Pure 条码一起使用,并尝试更难的提示 这是返回null
地方:
private FinderPattern[] selectBestPatterns()
{
int startSize = possibleCenters.Count;
if (startSize < 3)
{
// Couldn't find enough finder patterns
return null; // It returns here
}
...
在线Zxing解码器可以正确解码我正在测试的二维码。 这是我的测试二维码:
我解决了这个问题,在Android实现中使用这个方法从图像的路径返回一个RGBLuminanceSource
public RGBLuminanceSource GetRGBLuminanceSource(string imagePath)
{
if (File.Exists(imagePath))
{
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 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.ARGB32);
}
return null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.