【发布时间】:2013-01-16 20:01:38
【问题描述】:
我正在尝试使用 QR 码传输字节数组,因此为了测试,我决定生成一个随机字节数组,将其编码为 QR 码,然后对其进行解码。我使用 ISO-8859-1 将字节数组转换为字符串 s.t 它在传输时不会丢失数据:
编码器端:
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));
如果你运行这个,你可以很容易地看到你最终可以得到完全相同的数组。我对此没有任何问题。
Update 3:我也尝试过使用 UTF-8 对其进行编码,但它会丢失数据,因此不能用于编码器。
更新 4:刚刚添加:
Map<DecodeHintType,Object> mp=new HashMap<DecodeHintType, Object>();
mp.put(DecodeHintType.CHARACTER_SET, "ISO-8859-1");
在解码器中,仍然抛出异常。
【问题讨论】:
-
我使用 ISO-8859-1 将字节数组转换为字符串,因此在传输时不会丢失数据。抱歉,这没有任何意义。
-
对不起,我的意思是我可以将随机字节数组转换为字符串,然后再转换回来,我仍然可以获得相同的字节数组。我的更新#2 显示了详细信息
-
您说过它在一条消息中有效。您还没有发布任何失败的二维码。有什么问题?
-
如果你像我的更新 2 那样生成一个随机字节数组,那么它肯定会失败。但是,如果我只输入一个字符串并进行编码,它就可以正常工作。
-
这已经正确解码了。但是尝试 PURE_BARCODE 模式
标签: java android qr-code zxing