17

Zxing Project is a famous open-source, multi-format 1D/2D barcode image processing library implemented in Java, with ports to other languages. But I believe there are somebody have the same problem just like me: I can't Encode UTF-8 characters in a Qrcode.

How do I encode characters using UTF-8 in a QR code using Zxing project?

Coding minion
  • 527
  • 2
  • 4
  • 12
  • 1
    Please change this to a *question*, ideally one which is more technically accurate (There aren't "UTF-8 characters" there are "characters which are then encoded in UTF-8 to get a binary representation".) Then answer your own question. Currently this "question" is not a good fit for the site. – Jon Skeet Mar 23 '12 at 08:09
  • Brilliant, a well-formulated, insightful, deeply researched answer. What was the questions? – Walter K Mar 23 '12 at 08:10
  • Sorry, this is not a question, I just want to share how solve this problem for others people....what's should I do properly? – Coding minion Mar 23 '12 at 08:17
  • @Bluegray - Edit your questions so it is a question. For example "How do I encode characters using UTF-8 in a QR code using Zxing project?" Next, add your description of how to do this as an answer below. This allows people to comment and vote on your answer and also add their own answer if they know of a better/different way to do this. I could do this for you but the the answer would be in my name and I don't want to take your Rep points! – David Webb Mar 23 '12 at 08:23
  • @DaveWebb : thank you very much, I'll revise my question. – Coding minion Mar 23 '12 at 08:25
  • Is the problem to have an UTF-8 encoded string in what is scanned, or to put Unicode characters in a QR? – Thorbjørn Ravn Andersen Mar 23 '12 at 08:38

3 Answers3

23

The proper way of doing this is using hints:

  Hashtable hints = new Hashtable();
  hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

Then call this version of encode in QRCodeWriter class:

  encode(String contents, BarcodeFormat format, int width, int height,Hashtable hints)
Mister Smith
  • 27,417
  • 21
  • 110
  • 193
  • I use the Core.jar of Zxing project, and use your codes correctly, although Qrcode could be generated, but if I use "Qr Droid" or "QuickMark" apps to decode this Qrcode, it can't be decoded. anyway, thanks your help and answer.:) – Coding minion Mar 23 '12 at 10:04
  • That's because these apps are not using hints for decoding in UTF-8, so it is the expected behavior. If you were writing your decoding app, you could pass the `MultiFormatReader` a hashtable of hints where you have put the desired charset with `DecodeHintType.CHARACTER_SET` as key. – Mister Smith Mar 23 '12 at 10:23
9

Mister Smith's answer is quite right. But somehow you need to use the lowercase utf-8 instead of uppercase UTF-8 when encoding with ZXing. Or some scanners such as Alipay cannot read it.

Hashtable hints = new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
Landys
  • 7,169
  • 3
  • 25
  • 34
1

I found there is an API which is easier:

.withCharset("utf-8")

Example:

Bitmap bitmap = QRCode.from([string])
                   .withSize([width], [height])
                   .withCharset("utf-8")
                   .bitmap();
Amos
  • 2,222
  • 1
  • 26
  • 42