C# Unity QRCode解码结果返回null
我有一个C#/.Net应用程序,可以从磁盘上的图像解码多个QR码。我想在Unity内部执行此操作,因此我导入了zxing插件并修改了代码,使其现在使用Color32[],而不是Bitmap类(因为Unity不支持System.Drawing.Bitmap)。以下是我内心的团结:C# Unity QRCode解码结果返回null,c#,unity3d,zxing,C#,Unity3d,Zxing,我有一个C#/.Net应用程序,可以从磁盘上的图像解码多个QR码。我想在Unity内部执行此操作,因此我导入了zxing插件并修改了代码,使其现在使用Color32[],而不是Bitmap类(因为Unity不支持System.Drawing.Bitmap)。以下是我内心的团结: var fileData = File.ReadAllBytes(filePath); Texture2D texture = new Texture2D(2,2); texture.LoadIm
var fileData = File.ReadAllBytes(filePath);
Texture2D texture = new Texture2D(2,2);
texture.LoadImage(fileData);
var barcodeBitmap = texture.GetPixels32 ();
LuminanceSource source = new Color32LuminanceSource (barcodeBitmap, texture.width, texture.height);
IBarcodeReader reader = new BarcodeReader ();
Result[] results = reader.DecodeMultiple (source);
if (results != null)
{
foreach (Result result in results)
{
Debug.Log(result.Text);
}
}
现在的问题是,与以前相反,results数组总是返回null,即使对于我已经测试过的.Net应用程序来说不是null的数组也是如此。如有任何建议,将不胜感激,谢谢 一个选项是使用ZXing.NET NuGet
using ZXing;
using ZXing.Client.Result;
using ZXing.Common;
using ZXing.QrCode;
var QRreader= new ZXing.QrCode.QRCodeReader();
var barcodeBitmap = (Bitmap)Bitmap.FromFile(filePath);
var result = QRreader.decode(barcodeBitmap);
if (result != null)
{
Debug.Log(result.Text);
}
如果您正在加载jpeg或png,它应该可以正常工作。 但是,bmp的LoadImage无法正常工作,这将为您返回null。 为了正确加载bmp类型的文件,可以使用
private static readonly B83.Image.BMP.BMPLoader bmpImageLoader=new B83.Image.BMP.BMPLoader()代码>
然后通过以下方式加载:
texture = bmpImageLoader.LoadBMP(filePath).ToTexture2D();
试着一步一步地调试你的代码,看看什么地方出错了,你就能找到你需要的。这是一个qrcode API,它实际上与UnityFigure一起工作!谢谢你的建议!但实际上,我正在尝试将.Net项目迁移到Unity,以避免假设用户的PC上有.Net下载。我正在按照上面的建议进行调试,我意识到Color32LuminanceSource()返回的高度和宽度为8,而我的原始图像大小为640x480,因此,我将研究如何解决这个问题。正如我前面所说,我以前已经解决了这个问题,但这是正确的。谢谢