2016-07-21 175 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質地