2016-07-21 130 views
1

我想用统一版本的zxing来解码QR码。zxing QR解码返回null

这里是代码:

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条码工具中测试过,它们都很好。

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

+0

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

回答

0

问题是我将纹理大小设置为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质地