Can I just encode a byte array?

2,008 views
Skip to first unread message

zfk...@gmail.com

unread,
May 26, 2009, 4:12:48 AM5/26/09
to zxing
I have to encode a String which contains Chinese characters into
QRcode.
I know the zxing can do that, but after two days of looking through
the zxing issues and discussion group, I still can not find a
solution.

Can I encode a string into a byte array by using certain character
set, and the pass this byte array to the Encoder? In this way, after
decode the image, I can use the result byte array to build the
original string.
How can I do that? or the QRcode encoding/decoding isn`t work this way?

srowen

unread,
May 26, 2009, 4:46:31 AM5/26/09
to zxing
Theoretically, QR Codes do not really encode bytes -- they encode
strings. But we all know that internally, this reduces to an array of
bytes.

The current code does not let you encode a byte[] directly since that
is sort of wrong from an API standpoint. I could see domain-specific
use cases where you wanted to encode raw binary data into a QR Code
though. For that reason it could be added.

But in your case you really are trying to encode a string. The code is
prepared to encode a string according to a character encoding you
specify, though I warn you that if you are not using ISO-8859-1, it
may not work on all readers. Here you have no choice if you wish to
encode Chinese characters though. The code will try to put in an ECI
segment to specify your custom encoding.

Does this not work for you?

Hashtable hints = new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
QRCode qrCode = new QRCode();
Encoder.encode(yourString, ErrorCorrectionLevel.M, hints, qrCode);

zfk...@gmail.com

unread,
May 26, 2009, 6:11:38 AM5/26/09
to zxing
The codes:
-------------------------------------------------------
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
QRCode qrCode = new QRCode();
Encoder.encode(yourString, ErrorCorrectionLevel.M, hints, qrCode);
-------------------------------------------------------------------------------------------------
is not work for me, I try that before I post my question.
The generated QRcode image looks like a QRcode, with black and white
blocks, but it can not be decoded by zxing, the code throw an
Exception.

It seems in the Encoder.encode(), the append ECI code has been
commented. I uncomment the code, still not working.

zfk...@gmail.com

unread,
May 26, 2009, 10:03:07 PM5/26/09
to zxing
I use the following code to generate a QR code image.
----------------------------------------------------------------
String content = "test123中文" ;

Hashtable hints = new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
QRCode qrCode = new QRCode();
Encoder.encode(content, ErrorCorrectionLevel.M, hints, qrCode);

ByteMatrix matrix = renderResult(qrCode, 256, 256) ; //this method
is copy from the QRCodeWriter class

BufferedImage image = createImageFromMatrix(matrix) ; // this method
is definded as below
File imageFile = new File("F:\\temp\\test.png") ;
ImageIO.write(image,"PNG",imageFile) ;
----------------------------------------------------------------
private static BufferedImage createImageFromMatrix(ByteMatrix matrix)
throws Exception {
int width = matrix.width() ;
int hight = matrix.height() ;
BufferedImage image = new BufferedImage(width, hight,
BufferedImage.TYPE_INT_RGB) ;
for(int x=0;x<width;x++){
for(int y=0;y<hight;y++){
image.setRGB(x,y,matrix.get(y,x));
}
}

return image ;
}

----------------------------------------------------------------
And I got this image:
http://zfkhrl.googlepages.com/test.png


=========================================
When I use the following code to decode this image:
-------------------------------------------
QRCodeReader reader = new QRCodeReader() ;
MonochromeBitmapSource image = new
BufferedImageMonochromeBitmapSource(ImageIO.read(new File("F:\\temp\
\test.png"))) ;
Result result = reader.decode(image) ;
String decodeString = result.getText() ;
System.out.println(decodeString);
--------------------------------------------
The code throw a ReaderException, and the image can not be read by
other desktop QR code decode software too.

I appriciate any help you can provide to my problem. Thank you.


On 5月26日, 下午7时09分, srowen <sro...@gmail.com> wrote:
> Can you send the QR Code so I can debug? it should work -- if not
> there is a bug somewhere.
>
> On May 26, 11:11 am, zfk...@gmail.com wrote:
>
>
>
> > The codes:
> > -------------------------------------------------------
> > Hashtable hints = new Hashtable();
> > hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
> > QRCode qrCode = new QRCode();
> > Encoder.encode(yourString, ErrorCorrectionLevel.M, hints, qrCode);
> > --------------------------------------------------------------------------------------------------
> > is not work for me, I try that before I post my question.
> > The generated QRcode image looks like a QRcode, with black and white
> > blocks, but it can not be decoded by zxing, the code throw an
> > Exception.
>
> > It seems in the Encoder.encode(), the append ECI code has been
> > commented. I uncomment the code, still not working.- 隐藏被引用文字 -
>
> - 显示引用的文字 -

srowen

unread,
May 27, 2009, 5:17:00 AM5/27/09
to zxing
Hmm, when I run this code, I get a different QR Code image, that does
decode. I can email it to you perhaps. Are you using the latest code
from subversion? perhaps there is some difference there.

Actually, defining the character set here has no effect,
unfortunately, since the code doesn't know the ECI code for UTF-8 -- I
cannot find this documented anywhere. But, it is good that your code
does specify this anyway, so leave it.

It does seem to correctly encode the string as UTF-8.

However then the decoder guesses the encoding incorrectly from these
bytes -- it guesses Shift_JIS.

But your example helped me figure out an additional change to the
encoding guesser that then enables this string to be properly detected
as UTF-8.

Now it works for me -- decodes correctly.

So start by getting the latest code from Subversion and perhaps you
will find it works for you too.

srowen

unread,
May 26, 2009, 7:09:09 AM5/26/09
to zxing
Can you send the QR Code so I can debug? it should work -- if not
there is a bug somewhere.

On May 26, 11:11 am, zfk...@gmail.com wrote:
Reply all
Reply to author
Forward
0 new messages