【问题标题】:zxing QR decoding returning nullzxing QR解码返回null
【发布时间】:2016-07-21 08:36:25
【问题描述】:

我正在尝试使用统一版本的 zxing 来解码二维码。

代码如下:

texture = gameObject.GetComponent();

    //texture.pixelInset = new Rect(Screen.width / 4, Screen.height / 4, Screen.width / 2, Screen.height / 2);
    int imageSize = 100;

    Texture2D text2D = new Texture2D(imageSize, imageSize);

    text2D = (Texture2D)texture.texture;

    Debug.Log("text2D.GetPixels32().Length " + text2D.GetPixels32().Length);
    //Debug.Log("text2D.GetRawTextureData().Length " + text2D.GetRawTextureData().Length);

    Color32LuminanceSource source = new Color32LuminanceSource(text2D.GetPixels32(), imageSize, imageSize);        
    RGBLuminanceSource source1 = new RGBLuminanceSource(text2D.GetRawTextureData(), imageSize, imageSize);

    Debug.Log(GetString(source.Matrix));

    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

    BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source1));

    QRCodeReader reader = new QRCodeReader();

    BarcodeReader reader1 = new BarcodeReader();

    DecodingOptions options = new DecodingOptions();

    options.TryHarder = true;

    Result resultQRCodeReader = reader.decode(bitmap, options.Hints);
    Result resultQRCodeReader1 = reader.decode(bitmap1, options.Hints);

    Result resultBarcodeReader = reader1.Decode(source);
    Result resultBarcodeReader1 = reader1.Decode(source1);

问题是来自两个阅读器的所有“结果”,两个来源都返回一个空字符串。 我已经尝试了几个二维码的例子,我在 zxing 条码工具-https://zxing.org/w/decode.jspx 中进行了测试,它们都很好。

无法弄清楚我做错了什么。

【问题讨论】:

  • 该脚本附加到哪个对象?尝试从 text2D.GetPixels32() 数组中打印几行,看看它是否具有良好的值。您也可以直接在 reader.decode(color32array,width,height) 中使用 text2D.GetPixels32()

标签: unity3d qr-code zxing unity5


【解决方案1】:

问题是我将纹理大小设置为 100 的任意大小。 传递给亮度源的大小应该是原始纹理的大小。所以正确的代码应该是:

 //texture.pixelInset = new Rect(Screen.width / 4, Screen.height / 4, Screen.width / 2, Screen.height / 2);

Texture2D text2D = new Texture2D(imageSize, imageSize);

int imageSize = text.height;

text2D = (Texture2D)texture.texture;

Debug.Log("text2D.GetPixels32().Length " + text2D.GetPixels32().Length);
//Debug.Log("text2D.GetRawTextureData().Length " + text2D.GetRawTextureData().Length);

Color32LuminanceSource source = new Color32LuminanceSource(text2D.GetPixels32(), imageSize, imageSize);        
RGBLuminanceSource source1 = new RGBLuminanceSource(text2D.GetRawTextureData(), imageSize, imageSize);

Debug.Log(GetString(source.Matrix));

BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source1));

QRCodeReader reader = new QRCodeReader();

BarcodeReader reader1 = new BarcodeReader();

DecodingOptions options = new DecodingOptions();

options.TryHarder = true;

Result resultQRCodeReader = reader.decode(bitmap, options.Hints);
Result resultQRCodeReader1 = reader.decode(bitmap1, options.Hints);

Result resultBarcodeReader = reader1.Decode(source);
Result resultBarcodeReader1 = reader1.Decode(source1);

*假设这是纹理 2 的幂

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-14
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多