1
我需要从编码为BitMatrix
的QR码中获得byte[] array
。这里是我的代码:从QR码获取原始字节与zxing lib(或从BitMatrix转换)
// imports
import com.google.zxing.BarcodeFormat;
import com.google.zxing.ChecksumException;
import com.google.zxing.FormatException;
import com.google.zxing.Writer;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.DecoderResult;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.datamatrix.decoder.Decoder;
生成QR码的功能:
public byte[] createQRCode() {
String qrCodeData = "Hello world";
String charset = "UTF-8";
BitMatrix matrix = null;
Writer writer = new QRCodeWriter();
try {
matrix = writer.encode(new String(qrCodeData.getBytes(charset), charset), BarcodeFormat.QR_CODE, qrCodeheight, qrCodewidth);
} catch (UnsupportedEncodingException e) {
return;
}
catch (WriterException e) {
return;
}
DecoderResult decoderResult = null;
try {
decoderResult = new Decoder().decode(matrix);
} catch (ChecksumException e) {
return;
} catch (FormatException e) {
// Always this exception is throwed
}
byte[] cmd = decoderResult.getRawBytes();`
return cmd;
}
总是在FormatException
的执行停止,甚至在Decode().decode()
请求的参数是BitMatrix
。
有人可以告诉我什么是错的或让我以其他方式获取QR码byte
数组?