Java 如何使用 Zxing 项目在二维码中使用 UTF-8 编码字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9835774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:22:45  来源:igfitidea点击:

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

javaandroidqr-codezxing

提问by Laurence

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.

Zxing Project 是一个著名的开源、多格式一维/二维条码图像处理库,用 Java 实现,可移植到其他语言。但我相信有人和我一样有同样的问题:我无法在二维码中编码 UTF-8 字符。

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

如何使用 Zxing 项目在二维码中使用 UTF-8 编码字符?

回答by Mister Smith

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 encodein QRCodeWriterclass:

然后encodeQRCodeWriter课堂上调用这个版本:

  encode(String contents, BarcodeFormat format, int width, int height,Hashtable hints)

回答by Landys

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

史密斯先生的回答非常正确。但不知何故,你需要使用小写字母utf-8而不是大写的UTF-8用编码时ZXing。或者支付宝等一些扫描仪无法读取。

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

回答by Amos

I found there is an API which is easier:

我发现有一个更简单的 API:

.withCharset("utf-8")

Example:

例子:

Bitmap bitmap = QRCode.from([string])
                   .withSize([width], [height])
                   .withCharset("utf-8")
                   .bitmap();