我正在使用zxing二维码api,并试图从android设备上的二维码中提取二进制数据。但是,在Android上,result.getResultMetadata()并没有通过intent传递给我,所以我尝试使用result.getRawBytes()来检索我的字节数组。但是,getrawbytes()似乎不返回相同的内容。
result.getrawbytes()到底是什么?有人知道如何正确地从zxing二维码中提取字节数组吗?
谢谢

最佳答案

所以我想你想要的是字节数组中的原始解码数据。zxing提供给您的意图源缺少元数据,但它仍被发送到意图过滤器。
在intentintegrator.java内部的parseActivityResult中,可以添加:

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

我修改了intentresult类,以便能够获取这一额外的部分:
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;
}

这个字节数组存储元数据的第一个数组,也就是数据的原始内容(字节)。

07-24 12:59