将 QRCodeWriter 的 BitMatrix 输出转换为字节数组?
Convert BitMatrix output from QRCodeWriter to byte array?
我有一个 API,前端 (PHP) 调用它来生成二维码。 QR 码作为字节数组 (byte[]
) 发回。目前我们正在使用付费库,它可以将二维码呈现为 OutputStream
,如 ByteArrayOutputStream
。但是这个库不支持一些 UTF-8 字符,这给我们带来了问题。通过 ZXing 的 QR 码生成过程,我们注意到可以在编码包含 QR 码之前在哈希表中指定字符集。
我们希望尽可能少地更改现有功能。因此想知道是否可以将 QRCodeWriter.encode
方法的 BitMatrix 输出转换为字节数组?
您可以通过查看 class com.google.zxing.client.j2se.MatrixToImageWriter
(file on github)
找到一些读取和转换位矩阵的示例代码
这是一个快速实施:
import com.google.zxing.qrcode.QRCodeWriter
import com.google.zxing.client.j2se.MatrixToImageWriter
val bitMatrix = qrCodeGenerator.encode("text", BarcodeFormat.QR_CODE, 300, 300)
val os = OutputStream()
MatrixToImageWriter.writeToStream(bitMatrix, "PNG", os)
// do whatever with `os`
别忘了更新build.gradle
// ...
dependencies {
// ...
implementation 'com.google.zxing:core:3.4.1'
implementation 'com.google.zxing:javase:3.4.1'
}
// ...
我有一个 API,前端 (PHP) 调用它来生成二维码。 QR 码作为字节数组 (byte[]
) 发回。目前我们正在使用付费库,它可以将二维码呈现为 OutputStream
,如 ByteArrayOutputStream
。但是这个库不支持一些 UTF-8 字符,这给我们带来了问题。通过 ZXing 的 QR 码生成过程,我们注意到可以在编码包含 QR 码之前在哈希表中指定字符集。
我们希望尽可能少地更改现有功能。因此想知道是否可以将 QRCodeWriter.encode
方法的 BitMatrix 输出转换为字节数组?
您可以通过查看 class com.google.zxing.client.j2se.MatrixToImageWriter
(file on github)
这是一个快速实施:
import com.google.zxing.qrcode.QRCodeWriter
import com.google.zxing.client.j2se.MatrixToImageWriter
val bitMatrix = qrCodeGenerator.encode("text", BarcodeFormat.QR_CODE, 300, 300)
val os = OutputStream()
MatrixToImageWriter.writeToStream(bitMatrix, "PNG", os)
// do whatever with `os`
别忘了更新build.gradle
// ...
dependencies {
// ...
implementation 'com.google.zxing:core:3.4.1'
implementation 'com.google.zxing:javase:3.4.1'
}
// ...