zxing QR解码返回null

时间:2016-07-21 08:36:26

标签: unity3d qr-code zxing unity5

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

以下是代码:

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

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

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

1 个答案:

答案 0 :(得分:0)

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

 //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纹理的力量