我正在尝试从一个.png文件中导入一个QR码并使用Zxing.Net.Mobile和ZXing.Net.Mobile.Forms进行解码。
如果我使用
由于我正在使用Xamarin Forms,因此每个平台都处理位图/图像的创建,而可移植部分则处理其余部分(
我查看了一些在SO上遇到相同问题的帖子,并尝试了以下方法:
如果我使用
ZXing.Mobile.MobileBarcodeScanner
类扫描QR码,则解码按要求工作,但是,当从文件导入时,QR码阅读器(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
< p > DependencyService
将调用平台特定的函数,目前我正在使用 Android,因此该函数如下:
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()
- 我还调试了库源代码,从我的理解来看,它只是找不到导入图像中的QR码。没有抛出异常。
这里是返回null
的地方:
private FinderPattern[] selectBestPatterns()
{
int startSize = possibleCenters.Count;
if (startSize < 3)
{
// Couldn't find enough finder patterns
return null; // It returns here
}
...
这个在线Zxing解码器可以正确地解码我正在测试的QR码。这是我的测试QR码: