Coding minion
Coding minion

Reputation: 587

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

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?

Upvotes: 18

Views: 21356

Answers (3)

Amos
Amos

Reputation: 2650

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();

Upvotes: 1

Landys
Landys

Reputation: 7737

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");

Upvotes: 9

Mister Smith
Mister Smith

Reputation: 28168

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)

Upvotes: 24

Related Questions