Search code examples
javaandroidzxing

ZXing Result.getRawBytes(), what exactly is it?


I'm working with the zxing QR code APIs, and I'm trying to extract binary data from a QR code on an android device. However, on android, Result.getResultMetadata() isn't passed through to me through the Intent, so I tried to use Result.getRawBytes() to retrieve my byte array. However, getRawBytes() does not seem to return the same thing.

What exactly is Result.getRawBytes() and does anyone know how to extract byte arrays from zxing QR codes properly?

Thanks


Solution

  • So what i think you want is the raw, decoded data in a byte array. The intent source that zxing gives you is missing the metadata, but it's still being sent to the intent filter.

    In parseActivityResult inside of IntentIntegrator.java, you can add:

    byte[] dataBytes = intent.getByteArrayExtra("SCAN_RESULT_BYTE_SEGMENTS_0");
    return new IntentResult(contents,
                            formatName,
                            rawBytes,
                            orientation,
                            errorCorrectionLevel,
                            dataBytes);
    

    I modified the IntentResult class to be able to take this extra piece:

    private final byte[] dataBytes;
    
    IntentResult() {
        this(null, null, null, null, null, null);
    }
    
    IntentResult(String contents,
               String formatName,
               byte[] rawBytes,
               Integer orientation,
               String errorCorrectionLevel,
               byte[] dataBytes) {
        this.contents = contents;
        this.formatName = formatName;
        this.rawBytes = rawBytes;
        this.orientation = orientation;
        this.errorCorrectionLevel = errorCorrectionLevel;
        this.dataBytes = dataBytes;
    }
    
    
    /**
    * @return raw content of barcode in bytes
    */
    
    public byte [] getDataBytes() {
      return dataBytes;
    }
    

    This byte array stores the first array of the metadata, AKA the raw contents of your data in bytes.