【问题标题】:Unity QRCode decode results return nullunity QRCode 解码结果返回 null
【发布时间】:2018-10-30 22:34:48
【问题描述】:

我有一个 C#/.Net 应用程序,可以从磁盘上的图像中解码多个 QR 码。我想在 Unity 中执行此操作,因此我导入了 zxing 插件并修改了代码,使其现在使用 Color32[],而不是 Bitmap 类(因为 Unity 不支持 System.Drawing.Bitmap) .以下是我在 Unity 中的内容:

    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);
        }
    }

现在的问题是,与以前相反,结果数组总是返回空值,即使对于我已经测试过的 .Net 应用程序的非空值也是如此。任何建议将不胜感激,谢谢!

【问题讨论】:

  • 尝试一步步调试你的一段代码,看看什么时候出错
  • 你可以找到你需要的here。那是一个真正适用于 Unity 的二维码 API
  • 想通了!

标签: c# unity3d zxing


【解决方案1】:

一种选择是使用 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);
}

【讨论】:

  • 感谢您的建议!但我实际上是在尝试将 .Net 项目迁移到 Unity,以避免假设用户在他们的 PC 上下载了 .Net。我正在按照上面的建议通过调试器,我意识到 Color32LuminanceSource() 返回的高度和宽度为 8,而我的原始图像尺寸是 640x480,所以我将研究如何解决这个问题。
【解决方案2】:

如果您正在加载 jpeg 或 png,它应该可以正常工作。 但是,bmp 的LoadImage 无法正常工作,它会为您返回 null。 为了正确加载 bmp 类型的文件,您可以使用

private static readonly B83.Image.BMP.BMPLoader bmpImageLoader = new B83.Image.BMP.BMPLoader ();

然后通过以下方式加载它:

texture = bmpImageLoader.LoadBMP(filePath).ToTexture2D();

【讨论】:

  • 我之前已经解决了这个问题,但仍然是正确的。谢谢。
猜你喜欢
  • 1970-01-01
  • 2020-04-23
  • 1970-01-01
  • 1970-01-01
  • 2016-07-14
  • 2022-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多