当前位置: 首页 > news >正文

Android studio生成二维码

1.遇到的问题

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

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

2.代码 

  • 展示页面
<ImageViewandroid: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,具体参考apibitmap.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);//定义二维码的纠错级别,为LhashMap.put(EncodeHintType.CHARACTER_SET, "utf-8");//设置字符编码为utf-8hashMap.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();//获取widthint height = bitMatrix.getHeight();//获取heightint[] pixels = new int[width * height]; //创建一个新的数组,大小是width*heightfor (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_565bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);//调用setPixels(),第一个参数就是上面的那个数组,偏移为0,x,y也都可为0,根据实际需求来,最后是width ,和heightbitmap.setPixels(pixels, 0, width, 0, 0, width, height);*///调用setImageBitmap()方法,将二维码设置到imageview控件里qrCodeImageView.setImageBitmap(bitmap);}

http://www.lryc.cn/news/258214.html

相关文章:

  • python——第十六天
  • JWT介绍及演示
  • Android Studio新版UI介绍
  • 基于ssm应急资源管理系统论文
  • K8S学习指南(9)-k8s核心对象init pod
  • 以太坊:前世今生与未来
  • vue3若依框架,在页面中点击新增按钮跳转到新的页面,不是弹框,如何实现
  • 【大模型】800万纯AI战士年末大集结,硬核干货与音乐美食12月28日准时开炫
  • linux配置python环境
  • 【教程】app备案流程简单三部曲即可完成
  • C++使用vector创建二维数组并指定大小
  • Spring支持哪几种事务管理类型,Spring 的事务实现方式和实现原理是?
  • 书-二分查找找某个数字p155
  • 【NLP】RAG 应用中的调优策略
  • Android-Framework 默认隐藏导航栏,添加控制显示属性
  • 【AIGC】Midjourney高级进阶版
  • C语言学习----指针和数组
  • 学习Node.js与Webpack总结
  • JAVA基础知识:泛型
  • 【WinRAR】为什么右键没有压缩选项?
  • 数据云:数据基础设施的一小步,数字经济的一大步
  • 极兔速递查询,极兔速递单号查询,筛选出指定派件员的单号
  • 条款25:考虑写出一个不抛出异常的swap函数
  • linux 中crontab 定时任务计划创建时间文件夹示例
  • 欣赏动态之美,不如欣赏C语言实现动态内存管理之美 ! ! !
  • from pycocotools.coco import COCO报错
  • CentOS服务自启权威指南:手动启动变为开机自启动(以Jenkins服务为例)
  • 第二百零一回 介绍一个三方包open_settings
  • iview Table实现跨页勾选记忆功能以及利用ES6的Map数据结构实现根据id进行对象数组的去重
  • 【Spring 源码】 贯穿 Bean 生命周期的核心类之 AbstractAutowireCapableBeanFactory