Android studio生成二维码

1.遇到的问题

需要生成一个二维码,可以使用zxing第三方组件,增加依赖。

复制代码
//生成二维码
implementation 'com.google.zxing:core:3.4.1'

2.代码

  • 展示页面
复制代码
<ImageView
    android:id="@+id/qrCodeImageView"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:drawablePadding="16dp"
    android:background="@drawable/button_round_4"
    tools:layout_editor_absoluteX="176dp"
    tools:layout_editor_absoluteY="523dp" />
  • 引用ImageView
复制代码
ImageView qrCodeImageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().getAttributes().systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE;
    setContentView(R.layout.activity_config);
    qrCodeImageView = findViewById(R.id.qrCodeImageView);
}
  • 创建二维码方法

使用了几个方法,会出现乱码,通过最后这这种方式解决

复制代码
private void create_QR_code() throws WriterException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        String text= "成绩:1000 \n姓名:张三 \n姓名:张三 \n姓名:张三 \n姓名:张三 \n姓名:张三 \n姓名:张三 \n姓名:张三 \n姓名:张三 \n姓名:张三 \n姓名:张三";
        // 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
        Hashtable<EncodeHintType, String> hints = new Hashtable<>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        BitMatrix matrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, 150, 150, hints);

        int width = matrix.getWidth();
        int height = matrix.getHeight();
        // 二维矩阵转为一维像素数组,也就是一直横着排了
        int[] pixels = new int[width * height];
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                if (matrix.get(x, y)) {
                    pixels[y * width + x] = 0xff000000;
                }

            }
        }

        Bitmap bitmap = Bitmap.createBitmap(width, height,
                Bitmap.Config.ARGB_8888);
        // 通过像素数组生成bitmap,具体参考api
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        /*byte[] utf8Bytes = text.getBytes(StandardCharsets.UTF_8);
        String utf8BytesString = new String(utf8Bytes, StandardCharsets.UTF_8);
        // 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
        Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        BitMatrix bitMatrix = qrCodeWriter.encode(utf8BytesString, BarcodeFormat.QR_CODE, 150, 150);
        Bitmap bitmap = Bitmap.createBitmap(150, 150, Bitmap.Config.RGB_565);
        for (int x = 0; x < 150; x++) {
            for (int y = 0; y < 150; y++) {
                bitmap.setPixel(x, y, bitMatrix.get(x, y) ? getResources().getColor(R.color.black) : getResources().getColor(R.color.colorAccent));
            }
        }*/
/*        hashMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        //定义二维码的纠错级别,为L
        hashMap.put(EncodeHintType.CHARACTER_SET, "utf-8");
        //设置字符编码为utf-8
        hashMap.put(EncodeHintType.MARGIN, 2);
        //设置margin属性为2,也可以不设置
        String contents = "最简单的Demo"; //定义二维码的内容
        BitMatrix bitMatrix = null;   //这个类是用来描述二维码的,可以看做是个布尔类型的数组
        try {
            bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, 150, 150, hashMap);
            //调用encode()方法,第一次参数是二维码的内容,第二个参数是生二维码的类型,第三个参数是width,第四个参数是height,最后一个参数是hints属性
        } catch (WriterException e) {
            e.printStackTrace();
        }

        int width = bitMatrix.getWidth();//获取width
        int height = bitMatrix.getHeight();//获取height
        int[] pixels = new int[width * height]; //创建一个新的数组,大小是width*height
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                //通过两层循环,为二维码设置颜色
                if (bitMatrix.get(i, j)) {
                    pixels[i * width + j] = Color.BLACK;  //设置为黑色

                } else {
                    pixels[i * width + j] = Color.WHITE; //设置为白色
                }
            }
        }
        //调用Bitmap的createBitmap(),第一个参数是width,第二个参数是height,最后一个是config配置,可以设置成RGB_565
        bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        //调用setPixels(),第一个参数就是上面的那个数组,偏移为0,x,y也都可为0,根据实际需求来,最后是width ,和height
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);*/
        //调用setImageBitmap()方法,将二维码设置到imageview控件里
        qrCodeImageView.setImageBitmap(bitmap);


    }
相关推荐
Libraeking1 小时前
破壁行动:在旧项目中丝滑嵌入 Compose(混合开发实战)
android·经验分享·android jetpack
市场部需要一个软件开发岗位2 小时前
JAVA开发常见安全问题:Cookie 中明文存储用户名、密码
android·java·安全
JMchen1234 小时前
Android后台服务与网络保活:WorkManager的实战应用
android·java·网络·kotlin·php·android-studio
crmscs4 小时前
剪映永久解锁版/电脑版永久会员VIP/安卓SVIP手机永久版下载
android·智能手机·电脑
localbob4 小时前
杀戮尖塔 v6 MOD整合版(Slay the Spire)安卓+PC端免安装中文版分享 卡牌肉鸽神作!杀戮尖塔中文版,电脑和手机都能玩!杀戮尖塔.exe 杀戮尖塔.apk
android·杀戮尖塔apk·杀戮尖塔exe·游戏分享
机建狂魔5 小时前
手机秒变电影机:Blackmagic Camera + LUT滤镜包的专业级视频解决方案
android·拍照·摄影·lut滤镜·拍摄·摄像·录像
hudawei9965 小时前
flutter和Android动画的对比
android·flutter·动画
先跑起来再说6 小时前
Git 入门到实战:一篇搞懂安装、命令、远程仓库与 IDEA 集成
ide·git·后端·elasticsearch·golang·intellij-idea
lxysbly7 小时前
md模拟器安卓版带金手指2026
android
儿歌八万首7 小时前
硬核春节:用 Compose 打造“赛博鞭炮”
android·kotlin·compose·春节