decoding random byte array failed

55 views
Skip to first unread message

junfei wang

unread,
Jan 15, 2013, 4:45:04 PM1/15/13
to zx...@googlegroups.com
I want to transfer binary data with QR, so I tested the following: I generated random byte array, and convert it to string with ISO-8859-1, which does not lose data while transferring.
  byte []buffer = new byte[50];
  Random randomGenerator = new Random();
   for(int i=0;i<=49;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();
}
  But when i receive the encoded QR, its result is null, which means my input cannot be decoded. Is there any way for me to encode and decode QR in byte mode?
  Thank you!

Sean Owen

unread,
Jan 16, 2013, 12:14:05 AM1/16/13
to zx...@googlegroups.com
What result is null? I assume you mean WriterException was thrown. What's the exception?

junfei wang

unread,
Jan 16, 2013, 2:01:37 PM1/16/13
to zx...@googlegroups.com
LuminanceSource source = new PlanarYUVLuminanceSource(data,640,480,0,0,640,480,false);
bmtobedecoded = new BinaryBitmap(new HybridBinarizer(source));
result= qrr.decode(bmtobedecoded);
 the result is null, it rises notfound exception

junfei wang

unread,
Jan 16, 2013, 2:08:26 PM1/16/13
to zx...@googlegroups.com
no, it is the reader exception, I tried to decode the generated QR with camera, but failed.


On Wednesday, January 16, 2013 12:14:05 AM UTC-5, Sean Owen wrote:

Sean Owen

unread,
Jan 16, 2013, 9:12:50 PM1/16/13
to zx...@googlegroups.com
Why not post the image?

junfei wang

unread,
Jan 16, 2013, 9:21:11 PM1/16/13
to zx...@googlegroups.com
https://docs.google.com/file/d/0B6mDg0MzJ8fpVGlCWmx0aktqeEE/edit

On Wednesday, January 16, 2013 9:12:50 PM UTC-5, Sean Owen wrote:
Why not post the image?

Lachezar Dobrev

unread,
Jan 18, 2013, 10:45:17 AM1/18/13
to junfei wang, zx...@googlegroups.com
Other than the QR code stuff there is this: do NOT use character
encoding to encode binary data. That does not work (not 100% anyway).
There are bytes, that may not be a result from correct character
encoding, so decoders may cry foul when they see NIL, 0x7F, 0x8? or
0x9?. See http://wikipedia.org/wiki/ISO/IEC_8859-1 and note the great
grey areas that are not legitimate encodings and may result in invalid
decoding (rarely for the 0x00-0x2F, but commonly for the 0x7F-0x9F
range).

Use an encoding that is specialised for the task like Base16 (a.k.a.
HEX), Base36 (a.k.a. Alpha-Numeric), or Base64 (a.k.a. Base64 :)).

The other way is to encode the QR code with Binary mode and Binary
data and avoid any confusion whatsoever.

The QR code you supplied reads A-OK on my phone, and shows garbage.

2013/1/15 junfei wang <pier...@gmail.com>:

junfei wang

unread,
Jan 18, 2013, 2:03:47 PM1/18/13
to zx...@googlegroups.com, junfei wang

Actually it is exactly what I want, if I get these garbage string I can convert them back to byte array.

Lachezar Dobrev

unread,
Jan 18, 2013, 4:42:58 PM1/18/13
to junfei wang, zx...@googlegroups.com
No, you can't, at least not reliably!
To make sure you get this right use an encoding that guarantees
content reconstruction.
ISO*, UTF*, CP* and (possibly) all others are used to encode
characters as bytes, and back, not bytes as characters and back.

I can successfully read the *garbage*, so the QR reading part obviously works.

2013/1/18 junfei wang <pier...@gmail.com>:

junfei wang

unread,
Jan 18, 2013, 7:41:49 PM1/18/13
to zx...@googlegroups.com, junfei wang
But it returns notfound exception with my decoder, I can decode it with provided Barcode Scanner app, so weird

junfei wang

unread,
Jan 19, 2013, 11:45:53 PM1/19/13
to zx...@googlegroups.com, junfei wang
 I solved this problem by myself, I accidently tampered the YUV data before decoding process, sorry for the trouble, and thanks everyone for helping me.


On Friday, January 18, 2013 4:42:58 PM UTC-5, Lachezar Dobrev wrote:
Reply all
Reply to author
Forward
0 new messages