Android ZXing二维码包含中文时乱码解决

Android ZXing二维码包含中文时乱码解决

一、前言

ZXing就不过多介绍了,就是可以可以扫描二维码获取里面字符串和把字符串生成二维码的小框架。

本文记录一下源码编译中,ZXing 在源码编译后,包含中文字符串的二维码出现问号乱码解决。

Android Studio中运行该app是没有这个问题的,那么有可能就是编译环境不同导致的。

二、ZXing 把中文字符串生成二维码扫描后显示问号解决

1、Zxing 的简单使用

 ImageView imageView; //要显示二维码的控件
 String string = "XXX"; // 二维码的字符串

//Zxing 工具类
MultiFormatWriter formatWriter = new MultiFormatWriter()
BitMatrix matrix = formatWriter.encode(string, BarcodeFormat.QR_CODE, viewWidth, viewHeight);

Bitmap bitmap = bitMatrix2Bitmap(string, matrix);
imageView.setImageBitmap(bitmap); //设置二维码图片显示

//像素点确认的方法
public static Bitmap bitMatrix2Bitmap(BitMatrix matrix) {
    int w = matrix.getWidth();
    int h = matrix.getHeight();
    int[] rawData = new int[w * h];
    for (int i = 0; i < w; i++) {
        for (int j = 0; j < h; j++) {
            int color = Color.WHITE;
            if (matrix.get(i, j)) {
                color = Color.BLACK;
            }
            rawData[i + (j * w)] = color;
        }
    }

    Bitmap bitmap = Bitmap.createBitmap(w, h, Config.RGB_565);
    bitmap.setPixels(rawData, 0, w, 0, 0, w, h);
    return bitmap;
}

2、源码上的编译的项目二维码乱码修改

网上很多说把上面的string 字符串设置一下UTF-8 编码格式就行了,但是我在源码中编译的项目还是不行。

其实有很多的解决办法的,可以在生成二维码的时候,对它进行修改,或者生成二维码之后,对其修改。

今天我就只是在生成二维码的时候对其进行修改。

 ImageView imageView; //要显示二维码的控件
 String string = "XXX"; // 二维码的字符串

//规避中文字符串,二维码乱码问题
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

BitMatrix bitMatrix = new MultiFormatWriter().encode(string, BarcodeFormat.QR_CODE, 800, 800, hints);

Bitmap bitmap = bitMatrix2Bitmap(string, matrix);
imageView.setImageBitmap(bitmap); //设置二维码图片显示

其实就是 MultiFormatWriter.encode 的两个不同方法,基本使用的代码未设置编码格式,

后面的是在二维码生成过程设置了"UTF-8"格式。

二、其他

1、查看字符串编码:

String string = "XXX";
InputStream inputStream = new ByteArrayInputStream(string.getBytes());
//编码格式:UTF-8,GBK,ISO-8859-1, ...
String encodingTypeString = new InputStreamReader(inputStream).getEncoding();

2、字符串设置编码:

//api:
String string = "XXX";
String encodeString = new String(string.getBytes("oldType"), "newType");

//示例:
try {
    String encodeString = new String(string.getBytes("UTF-8"), "UTF-8");
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}

3、是否utf-8字符串

    public static boolean isUtf8(byte[] buffer) {
        boolean isUtf8 = true;
        int end = buffer.length;
        for (int i = 0; i < end; i++) {
            byte temp = buffer[i];
            if ((temp & 0x80) == 0) {// 0xxxxxxx
                continue;
            } else if ((temp & 0xC0) == 0xC0 && (temp & 0x20) == 0) {// 110xxxxx 10xxxxxx
                if (i + 1 < end && (buffer[i + 1] & 0x80) == 0x80 && (buffer[i + 1] & 0x40) == 0) {
                    i = i + 1;
                    continue;
                }
            } else if ((temp & 0xE0) == 0xE0 && (temp & 0x10) == 0) {// 1110xxxx 10xxxxxx 10xxxxxx
                if (i + 2 < end && (buffer[i + 1] & 0x80) == 0x80 && (buffer[i + 1] & 0x40) == 0
                        && (buffer[i + 2] & 0x80) == 0x80 && (buffer[i + 2] & 0x40) == 0) {
                    i = i + 2;
                    continue;
                }
            } else if ((temp & 0xF0) == 0xF0 && (temp & 0x08) == 0) {// 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
                if (i + 3 < end && (buffer[i + 1] & 0x80) == 0x80 && (buffer[i + 1] & 0x40) == 0
                        && (buffer[i + 2] & 0x80) == 0x80 && (buffer[i + 2] & 0x40) == 0
                        && (buffer[i + 3] & 0x80) == 0x80 && (buffer[i + 3] & 0x40) == 0) {
                    i = i + 3;
                    continue;
                }
            }
            isUtf8 = false;
            break;
        }
        return isUtf8;
    }

4、网上其他的处理方式

Android Zxing 扫码中文乱码解决:

判断是否utf-8,设置一次字符串是GBK或者UTF-8

https://blog.csdn.net/qq_25148525/article/details/124472345

Android ZXing生成中文二维码,扫描后出现问号、乱码问题解决:

https://blog.csdn.net/PenTablet/article/details/80325741

本文的处理也是参考这个文章的。

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
如果使用zxing生成的二维码包含中文字符,那么在扫描二维码可能会出现识别不了中文的情况。这是因为zxing默认使用ISO-8859-1编码,而中文字符需要使用UTF-8编码。 为了解决这个问题,您可以在生成二维码将数据字符串转换为UTF-8编码。例如: ``` String data = "你好,世界!"; int width = 500; int height = 500; BitMatrix bitMatrix = new MultiFormatWriter().encode(new String(data.getBytes("UTF-8"), "ISO-8859-1"), BarcodeFormat.QR_CODE, width, height); BarcodeEncoder barcodeEncoder = new BarcodeEncoder(); Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix); ``` 在这个例子中,我们将数据字符串从UTF-8编码转换为ISO-8859-1编码,然后再生成二维码。这样就可以在扫描二维码正确识别中文字符了。 同样,在扫描二维码,您需要确保使用UTF-8编码来解码二维码数据。例如: ``` @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); if (result != null) { if (result.getContents() == null) { Log.d(TAG, "Cancelled scan"); } else { try { String utf8String = new String(result.getContents().getBytes("ISO-8859-1"), "UTF-8"); Log.d(TAG, "Scanned: " + utf8String); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } } else { super.onActivityResult(requestCode, resultCode, data); } } ``` 在这个例子中,我们首先将二维码数据从ISO-8859-1编码转换为UTF-8编码,然后再处理扫描结果。这样就可以正确识别包含中文字符的二维码了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

峥嵘life

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值