我正在尝试使用 QR 码传输字节数组,因此为了测试,我决定生成一个随机字节数组,将其编码为 QR 码,然后对其进行解码。我使用 ISO-8859-1 将字节数组转换为字符串 st 它在传输时不会丢失数据:
对于编码器端:
byte []buffer = new byte[11];
com.google.zxing.Writer writer = new QRCodeWriter();
Random randomGenerator = new Random();
for(int i=0;i<=10;i++){
buffer[i]=(byte) randomGenerator.nextInt(254);
}
// Log.i("time1","original: "+Arrays.toString(buffer));
String decoded = null;
try {
decoded = new String(buffer, "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
result=writer.encode(decoded, BarcodeFormat.QR_CODE, 500, 500);
} catch (WriterException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
这样我已经将字节数组转换为二维码,没有问题。但对于接收方:
LuminanceSource source = new PlanarYUVLuminanceSource(data,640,480,0,0,640,480,false);
bmtobedecoded = new BinaryBitmap(new HybridBinarizer(source));
Map<DecodeHintType,Object> mp=new HashMap<DecodeHintType, Object>();
mp.put(DecodeHintType.TRY_HARDER, true);
try {
result= qrr.decode(bmtobedecoded,mp);
} catch (NotFoundException e) {
Log.i("123","not found");
e.printStackTrace();
} catch (ChecksumException e) {
Log.i("123","checksum");
e.printStackTrace();
} catch (FormatException e) {
Log.i("123","format");
e.printStackTrace();
}
我试图解码生成的二维码,但它抛出了 NotFoundException。有人可以帮我解决这个问题吗?
更新 1:我确认解码器与普通 QR 完美配合,我还添加了 DecodeHintType.try_harder 但仍然不好。
更新 2:为了澄清,下面是我在字节数组和字符串之间进行转换的操作:
Random randomGenerator = new Random();
for(int i=0;i<=10;i++){
buffer[i]=(byte) randomGenerator.nextInt(254);
}
Log.i("time1","original: "+Arrays.toString(buffer));
String decoded = null;
try {
decoded = new String(buffer, "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("time1","encoded string:" + decoded);
BitMatrix result=null;
try {
result=qw.encode(decoded, BarcodeFormat.QR_CODE, 500, 500);
} catch (WriterException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
iv.setImageBitmap(encodematrix(result));
byte[] encoded = null;
try {
encoded = decoded.getBytes("ISO-8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("time1","result byte array:" + java.util.Arrays.toString(encoded));
如果你运行这个,你可以很容易地看到你最终可以得到完全相同的数组。我对此没有任何问题。
更新 3:我也尝试使用 UTF-8 对其进行编码,但它会丢失数据,因此它不能用于编码器。
更新 4:刚刚添加:
Map<DecodeHintType,Object> mp=new HashMap<DecodeHintType, Object>();
mp.put(DecodeHintType.CHARACTER_SET, "ISO-8859-1");
在解码器中,仍然抛出异常。