使用**zxing
生成二维码后,如果文本里面有中文,再次解析二维码图片时会造成中文乱码,需要把生成二维码的方法换一下。
使用MultiFormatWriter
替换QrCodeWriter
并且指定CHARACTER_SET
为UTF-8
**
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| private Bitmap createQRCode(String text) {
MultiFormatWriter writer = new MultiFormatWriter();
ViewGroup.LayoutParams params = imageView.getLayoutParams();
int width = params.width;
int height = params.height;
Log.d("TAG", "createQRCode: width=" + width + " height=" + height);
try {
//使用hints解决中文乱码问题
Hashtable<EncodeHintType, String> hints = new Hashtable<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix matrix = writer.encode(text, BarcodeFormat.QR_CODE, width, height, hints);
int[] pixels = new int[width * height];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
pixels[i * width + j] += matrix.get(i, j) ? 0x000000 : 0xffffff;
}
}
return Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565);
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}
|