[英]Reading QR code in Xamarin Forms With Zxing
I am trying to import a QR code from a .png file and decode it using Zxing.Net.Mobile and ZXing.Net.Mobile.Forms .我正在尝试从.png 文件导入二维码并使用Zxing.Net.Mobile和ZXing.Net.Mobile.Forms对其进行解码。
If I scan the QR code using the ZXing.Mobile.MobileBarcodeScanner
class, the decoding works as required, however, when importing it from a file, the Qr code reader ( ZXing.QrCode.QRCodeReader()
) decode function always returns null
.如果我使用
ZXing.Mobile.MobileBarcodeScanner
类扫描二维码,解码会按要求工作,但是,从文件导入时, ZXing.QrCode.QRCodeReader()
码阅读器 ( ZXing.QrCode.QRCodeReader()
) 解码函数始终返回null
。
As I'm using Xamarin Forms ;当我使用Xamarin Forms 时; each platform handles the bitmap/image creation and the portable part handle the rest (
Zxing BinaryBitmap
creation and decoding).每个平台处理位图/图像创建,可移植部分处理其余部分(
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
The DependencyService
will call the platform specific function, at the moment I am working with Andorid so, the function is as follows: 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);
}
I have looked through some of the posts on SO that are having the same problem and have tried the following:我浏览了一些关于 SO 的帖子,这些帖子有同样的问题,并尝试了以下方法:
BitmapLuminanceSource
: still returns null
and requires the use of another libraryBitmapLuminanceSource
:仍然返回null
并需要使用另一个库RGBLuminanceSource
: RGB32, BGR32, ARGB32, ABGR32 (each time changing the FileToBitmap function)RGBLuminanceSource
使用不同的位图格式:RGB32、BGR32、ARGB32、ABGR32(每次更改 FileToBitmap 函数)Binarizer
, GlobalHistogramBinarizer()
Binarizer
, GlobalHistogramBinarizer()
MultiFormatReader()
with the Pure barcode and try harder hintsMultiFormatReader()
与 Pure 条码一起使用,并尝试更难的提示 Here is where the return null
is made:这是返回
null
地方:
private FinderPattern[] selectBestPatterns()
{
int startSize = possibleCenters.Count;
if (startSize < 3)
{
// Couldn't find enough finder patterns
return null; // It returns here
}
...
The online Zxing decoder can decode the QR code I'm testing correctly.在线Zxing解码器可以正确解码我正在测试的二维码。 Here is my test QR code:
这是我的测试二维码:
I solved that problem, with this method in the Android implementation to return an RGBLuminanceSource from the path of the image我解决了这个问题,在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.