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

【Android QR Code】开源项目:ZXing(二)二维码编码

准备工作:添加依赖库core.jar

在Package Explorer选择导入的项目,右键 -> Build Path -> Add External Archives...

选择zxing/core目录下的core.jar

 

1、设置编码内容使用的字符集

Map<EncodeHintType,Object> hints = new EnumMap<EncodeHintType,Object>(EncodeHintType.class);
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
message = convertToUTF8(message);

 

2、编码

BitMatrix lBitMatrix = new MultiFormatWriter().encode(message, BarcodeFormat.QR_CODE, 200, 200, hints);

类MultiFormatWriter:This is a factory class which finds the appropriate Writer subclass for the BarcodeFormat requested and encodes the barcode with the supplied contents.

调用encode方法编码,返回一个BitMatrix对象。

类BitMatrix:Represents a 2D matrix of bits. In function arguments below, and throughout the common module, x is the column position, and y is the row position. The ordering is always x, y. The origin is at the top-left. Internally the bits are represented in a 1-D array of 32-bit ints. However, each row begins with a new int. This is done intentionally so that we can copy out a row into a BitArray very efficiently. The ordering of bits is row-major. Within each int, the least significant bits are used first, meaning they represent lower x values. This is compatible with BitArray's implementation.

lBitMatrix仅仅是通过bit来存储二维码数据。如果要更直观地观察编码的结果,我们需要利用lBitMatrix来生成图片。

 

3、生成图片

首先,我们定义黑色和白色的像素值

    private static final int WHITE = 0xFFFFFFFF;
    private static final int BLACK = 0xFF000000;

然后,根据BitMatrix对象,二维矩阵的值,生成图片

private Bitmap toBitmap(BitMatrix bitMatrix) {
    int width = bitMatrix.getWidth();
    int height = bitMatrix.getHeight();
    int[] pixels = new int[width * height];
    for (int y = 0; y < height; y++) {
      int offset = y * width;
      for (int x = 0; x < width; x++) {
        pixels[offset + x] = bitMatrix.get(x, y) ? BLACK : WHITE;
      }
    }
    
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}

 

范例项目运行结果:

 

 

相关文章:

  • 使用CountDownTimer实现倒计时功能
  • 看图学维修mp3之电源篇65Z8\65Z5
  • CentOS 7.X 安全手记
  • 《More Effective C++:35个改善编程与设计的有效方法》(中文版)
  • POJ 1635 Subway tree systems(树同构)
  • Merkle Tree算法详解
  • 数字证书
  • 400多位云计算专家和开发者,加入了同一个组织 ...
  • java字符数组char[]和字符串String之间的转换
  • .\OBJ\test1.axf: Error: L6230W: Ignoring --entry command. Cannot find argumen 'Reset_Handler'
  • js猜数字小游戏——原创
  • 戒游戏
  • STM32学习2 GPIO学习
  • 最近新上的电子商务网站
  • Cocoa Touch揭秘
  • 【399天】跃迁之路——程序员高效学习方法论探索系列(实验阶段156-2018.03.11)...
  • Android组件 - 收藏集 - 掘金
  • Django 博客开发教程 16 - 统计文章阅读量
  • Electron入门介绍
  • flutter的key在widget list的作用以及必要性
  • JavaScript 基本功--面试宝典
  • JavaScript 无符号位移运算符 三个大于号 的使用方法
  • Mysql5.6主从复制
  • Service Worker
  • underscore源码剖析之整体架构
  • 聊聊directory traversal attack
  • 漂亮刷新控件-iOS
  • 前端面试总结(at, md)
  • 前端知识点整理(待续)
  • 使用 Docker 部署 Spring Boot项目
  • 手写双向链表LinkedList的几个常用功能
  • 小程序滚动组件,左边导航栏与右边内容联动效果实现
  • 学习ES6 变量的解构赋值
  • ​flutter 代码混淆
  • # Apache SeaTunnel 究竟是什么?
  • #[Composer学习笔记]Part1:安装composer并通过composer创建一个项目
  • (ZT) 理解系统底层的概念是多么重要(by趋势科技邹飞)
  • (二)hibernate配置管理
  • (附源码)springboot电竞专题网站 毕业设计 641314
  • (免费领源码)python#django#mysql校园校园宿舍管理系统84831-计算机毕业设计项目选题推荐
  • (一)C语言之入门:使用Visual Studio Community 2022运行hello world
  • (一)pytest自动化测试框架之生成测试报告(mac系统)
  • (一)基于IDEA的JAVA基础1
  • (转载)虚函数剖析
  • .net core 实现redis分片_基于 Redis 的分布式任务调度框架 earth-frost
  • .net 调用php,php 调用.net com组件 --
  • .net 发送邮件
  • .NET/C# 使用反射注册事件
  • .Net程序帮助文档制作
  • .net开发时的诡异问题,button的onclick事件无效
  • .NET下的多线程编程—1-线程机制概述
  • .set 数据导入matlab,设置变量导入选项 - MATLAB setvaropts - MathWorks 中国
  • @Documented注解的作用
  • @value 静态变量_Python彻底搞懂:变量、对象、赋值、引用、拷贝
  • []sim300 GPRS数据收发程序