目录:andorid jar/库源码解析

Zxing:

  作用:

    生成和识别,二维码,条形码。

  栗子:

  生成二维码,赋值到ImageView上

     QRCodeWriter qrCodeWriter = new QRCodeWriter();
Map<EncodeHintType, String> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); //记得要自定义长宽
BitMatrix encode = null;
try {
encode = qrCodeWriter.encode("hello,world!", BarcodeFormat.QR_CODE, width, height, hints);
} catch (WriterException e) {
e.printStackTrace();
}
int[] colors = new int[width * height];
//利用for循环将要表示的信息写出来
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (encode.get(i, j)) {
colors[i * width + j] = Color.BLACK;
} else {
colors[i * width + j] = Color.WHITE;
}
}
} Bitmap bit = Bitmap.createBitmap(colors, width, height, Bitmap.Config.RGB_565);
imageView.setImageBitmap(bit);

  生成条形码,赋值到ImageView上

     final int WHITE = 0xFFFFFFFF;
final int BLACK = 0xFF000000;
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix result = null;
try {
BarcodeFormat barcodeFormat = BarcodeFormat.CODE_128;
result = writer.encode("1234567123456", barcodeFormat, 400,
100, null);
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} int width = result.getWidth();
int height = result.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] = result.get(x, y) ? BLACK : WHITE;
}
} Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height); imageView2.setImageBitmap(bitmap);

  识别二维码

   private void Test3() {
recogQRcode(imageView);
} public void recogQRcode(ImageView imageView){
Bitmap QRbmp = ((BitmapDrawable) (imageView).getDrawable()).getBitmap(); //将图片bitmap化
int width = QRbmp.getWidth();
int height = QRbmp.getHeight();
int[] data = new int[width * height];
QRbmp.getPixels(data, 0, width, 0, 0, width, height); //得到像素
RGBLuminanceSource source = new RGBLuminanceSource(width, height, data);
BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader = new QRCodeReader();
Result re = null;
try {
//得到结果
re = reader.decode(bitmap1);
} catch (NotFoundException e) {
e.printStackTrace();
} catch (ChecksumException e) {
e.printStackTrace();
} catch (FormatException e) {
e.printStackTrace();
} lblMsg1.setText(re.getText());
}

  识别条形码

     Bitmap QRbmp = ((BitmapDrawable) (imageView2).getDrawable()).getBitmap();   //将图片bitmap化
int width = QRbmp.getWidth();
int height = QRbmp.getHeight();
int[] data = new int[width * height];
QRbmp.getPixels(data, 0, width, 0, 0, width, height); //得到像素
RGBLuminanceSource source = new RGBLuminanceSource(width, height, data); //二进制图片转换成bitmap对象(说明:创建HybridBinarizer对象,需要传入LuminanceSource,所以传入source(二进制的图片),并且通过BinaryBitmap转换成bitmap对象)
BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
//CodaBarReader codaBarReader= new CodaBarReader(); //codaBarReader 二维码
try {
//MultiFormatReader是读取图像的类(在core包)
Result result = new MultiFormatReader().decode(bitmap1); //识别条形码,和二维码(说明:获取到我们需要的信息)
lblMsg2.setText(result.getText());
} catch (NotFoundException e) {
e.printStackTrace();
lblMsg2.setText("ex " + e.getMessage());
}

  源码解读:

  A:生成二维码

    1、创建一个 QRCodeWriter对象,调用 encode,传入需要生成二维码的数据,格式化参数,大小,和编码信息

    2、调用Encoder的encode方法,a,确定当前使用的编码。b,判断生成二维码数据的编码模式(数字,byte,和 0x00-0x5f)。c,附加ECI部分(二维码格式中的一部分)。d,填充 FNC1信息到头部中。e,写入模式标记到头部中。

    3、把二维码文本数据,按照不同的模式,写入到不同的结果bit集合中。

    4、判断版本和容量,是否在范围内。

    5、合并头部,数据长度,数据内容到一个bit集合。

    6、填充数据,使bit数据时8的整数倍。

    7、把位数据信息和纠错码,交织在一起,存入新的数据(算法复杂,具体参考二维码生成算法)。

    8、调用 MatrixUtil.buildMatrix,构建二维码矩阵,并返回二维码信息。 QRCode中 使用 ByteMatrix 存储矩阵,内部是一个二维的bytes数组。 private final byte[][] bytes;

    9、转换矩阵颜色,从 输入矩阵使用0==白色,1==黑色,而输出矩阵使用 0==黑色,255==白色(即8位灰度位图)。

    10、创建一个color数组,把颜色数据存入。调用   Bitmap.createBitmap ,把color集合信息,传递给他,然后返回一个Bitmap就是二维码数据了。

  B:生成条形码

    1、创建一个 MultiFormatWriter 对象,并调用他的 encode方法,传入条形码数据,和编码信息,和条形码结果大小。

    2、根据不同的编码,选择了不同的写入器,这里采用了 Code128Writer

    3、a,判断条形码内容,长度限制[1,80]。...略

    4、使用二进制,把二进制转换成图片。就是最后的结果了。。

  C:识别二维码。(反向解析)

  D:识别条形码。(反向解析)

  源码:https://github.com/zxing/zxing

  引入:

api 'com.google.zxing:android-core:3.3.0'
api 'com.google.zxing:core:3.3.2'

  注意:(如果无法从google加载,就需要换一个来源,下面代码和图)

maven{ url'http://maven.aliyun.com/nexus/content/groups/public/' }
maven{ url'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
  

andorid jar/库源码解析之zxing的更多相关文章

  1. andorid jar/库源码解析之Bolts

    目录:andorid jar/库源码解析 Bolts: 作用: 用于链式执行跨线程代码,且传递数据 栗子: Task.call(new Callable<Boolean>() { @Ove ...

  2. andorid jar/库源码解析之EventBus

    目录:andorid jar/库源码解析 EventBus: 作用: 用于不同Activity,Service等之间传递消息(数据). 栗子: A页面:onCreate定义   EventBus.ge ...

  3. andorid jar/库源码解析之Dagger/Dagger2

    目录:andorid jar/库源码解析 Dagger.Dagger2: 作用: 1.用于解耦Activity和业务逻辑 2.在使用业务的时候,不需要重复编写new代码. 3.当业务变化的时候,不需要 ...

  4. andorid jar/库源码解析之okhttp3

    目录:andorid jar/库源码解析 Okhttp3: 作用: 用于网络编程(http,https)的快速开发. 栗子: // okHttpClient定义成全局静态,或者单例,不然重复new可能 ...

  5. andorid jar/库源码解析之okio

    目录:andorid jar/库源码解析 Okio: 作用: 说白了,就是一个IO库,基于java原生io.来进行操作,内部做了优化,简洁,高效.所以受到了一部分人的喜欢和使用 栗子: 读写文件. p ...

  6. andorid jar/库源码解析之retrofit2

    目录:andorid jar/库源码解析 Retrofit2: 作用: 通过封装okhttp库,来进行web通讯,并且使用动态代理的方式,来调用接口地址,通过回调赋值结果. 栗子: 定义一个接口,用于 ...

  7. andorid jar/库源码解析之Butterknife

    目录:andorid jar/库源码解析 Butterknife: 作用: 用于初始化界面控件,控件方法,通过注释进行绑定控件和控件方法 栗子: public class MainActivity e ...

  8. andorid jar/库源码解析之错误提示

    目录:andorid jar/库源码解析 错误: 错误1: Error: Static interface methods are only supported starting with Andro ...

  9. andorid jar/库源码解析

    前言 本篇作为开篇,会大体上说明,需要解读源码的,类库,或者jar. 序 原本,类库和jar的系列准备写到逆向系列课程的,但是那个东西,在写了两篇,就没有后续了,现在也不知道从哪里开始了, 只能等后期 ...

随机推荐

  1. Go语言 命令行解析(一)

    命令行启动服务的方式,在后端使用非常广泛,如果有写过C语言的同学相信不难理解这一点!在C语言中,我们可以根据argc和argv来获取和解析命令行的参数,从而通过不同的参数调取不同的方法,同时也可以用U ...

  2. 通过Java HTTP连接将网络图片下载到本地

    通过Java HTTP连接将网络图片下载到本地   只知道浏览器使用的是HTTP协议,那么如何将网络资源使用JavaHTTP下载下来呢! 这只是一个非常简单的小示例,只是不想每次碰到关于此方面的内容忘 ...

  3. 数据结构和算法(Golang实现)(7)简单入门Golang-标准库

    使用标准库 一.避免重复造轮子 官方提供了很多库给我们用,是封装好的轮子,比如包fmt,我们多次使用它来打印数据. 我们可以查看到其里面的实现: package fmt func Println(a ...

  4. uni-app同步缓存值 设置 读取 删除

    A页面 <view class="go-to-tab" @tap="gotologin"> 去login页面 </view> msg : ...

  5. 第一章:shell脚本初入门

    1.shell脚本中的source或者.空格再加上文件,表示加载文件中的命令及语句(困惑多时终于解开^-^) 2.脚本开头书写好作者版本等信息,方便维护:流程语句提前把格式写好,防止遗漏 3.定义字符 ...

  6. Daily Scrum 1/4/2015

    Process: After New year's Day, we start our project in plan. Zhanyang: Add some useful UI in the IOS ...

  7. C. Standard Free2play --div

    https://codeforces.com/contest/1238/problem/C 题意:下台阶的时候只有一种方式,拉动当前台阶x的 level,然后当前的台阶关闭,调到下边的台阶x-1,如果 ...

  8. 1. git 本地给远程仓库创建分支 三步法

    命令如下: 1:本地创建分支dev 1 2 Peg@PEG-PC /D/home/myself/Symfony (master) $ git branch dev 2:下面是把本地分支提交到远程仓库 ...

  9. Python快速编程入门,打牢基础必须知道的11个知识点 !

    Python被誉为全世界高效的编程语言,同时也被称作是“胶水语言”,那它为何能如此受欢迎,下面我们就来说说Python入门学习的必备11个知识点,也就是它为何能够如此受欢迎的原因. Python 简介 ...

  10. 作业十一——LL(1)文法的判断,递归下降分析程序

    作业十一——LL(1)文法的判断,递归下降分析程序 判断是否为LL(1)文法 选取有多个产生式的求select,只有一条产生式的无需求select 同一个非终结符之间求交集,全部判断为空后则为LL(1 ...