安卓使用Zxing创建二维码

转载自:https://blog.csdn.net/zz880329/article/details/6684025

  1. 导入Zxing(可以直接下载jar导入,也可以在添加gradle)

  2. 创建一个ImageView用于显示二维码

  3. 在代码中设置二维码数据

  

1、导入Zxing

1 compile group: 'com.google.zxing', name: 'core', version: '3.3.0'

2、创建一个ImageView  

 <ImageView               
          android:id="@+id/iv_37_01_ewm"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
                />

3、在代码中使用api创建一个BitMap,然后将这个BitMap设置到ImageView中

int width = 300;
int height = 300;
/*
生成二维码*/ private void createEwm() throws WriterException { /*生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败 */ BitMatrix bitMatrix = new MultiFormatWriter().encode("汽车编号:1,充值价格:150", BarcodeFormat.QR_CODE, width, height); /*转为一维数组*/ int[] pixels = new int[width * height]; for (int i = 0; i < height; i++) { for (int y = 0; y < width; y++) { if (bitMatrix.get(y, i)) { pixels[y * width + i] = 0xff000000; } } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); iv_37_01_ewm.setImageBitmap(bitmap);      // iv_37_01_ewm为ImageView }

4、效果:

  

原文地址:https://www.cnblogs.com/daihang2366/p/12656600.html