如何用Java中的QR码解码字节?

问题描述 投票:1回答:1

想象一下,我有这个示例节点缓冲区Buffer.from([1,2,3]),我已经将其编码为qrcode npm软件包文档中指定的QR码。

const buffer = Buffer.from([1, 2, 3]);

QRCode.toFile(`public/images/${product.name}.png`, [{ data: buffer, mode: 'byte' }], {
  color: { light: '#0000' },
}, (err) => { if (err) throw err; });)

这是输出:

enter image description here

现在,我必须在我的Android应用程序中恢复这些编码的字节。我目前正在使用Zxing,输出看起来像这样。不幸的是,在开头和结尾(ec 11 ec 11 ...)中添加了一堆格式化字节。

enter image description here

更复杂的示例得出相同的结果。对于缓冲区16 76 20 42 3f 5d 4d d5 82 9a 45 19 32 90 19 53 00 00 00 02 00 00 00 63 0e 46 72 6f 7a 65 6e 20 4c 61 73 61 67 6e 65,以下是Zxing解码:

enter image description here

427开头的值是什么,我如何摆脱它?因为否则数据看起来不错。我该如何找回我的ByteBuffer?

谢谢!

java node.js buffer qr-code bytebuffer
1个回答
0
投票

花了太多时间后,我感到很reveal愧,但我找到了解决问题的方法。

解决方案将缓冲区编码为Base64。

const buffer = Buffer.from([1, 2, 3]);

QRCode.toFile(`public/images/${product.name}.png`, buffer.toString('base64'), {
  color: { light: '#0000' },
}, (err) => { if (err) throw err; });)

然后在Java上,对其进行解码。

byte[] res = Base64.getDecoder().decode(rawResult.getText());

结果字节不再像以前的方法那样包含格式。

© www.soinside.com 2019 - 2024. All rights reserved.