我的Android进阶之旅------>QR的生成(二维码)

简介: 本文转载于:http://blog.csdn.net/dlutbrucezhang/article/details/8582839 二维码的定义: 二维码 (2-dimensional bar code),是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的。

本文转载于:http://blog.csdn.net/dlutbrucezhang/article/details/8582839


二维码的定义:

二维码 (2-dimensional bar code),是用某种特定的几何图形按一定规律在平面(二维方向上)
分布的黑白相间的图形记录数据符号信息的。

在许多种类的二维条码中,常用的码制有:Data Matrix, Maxi Code, Aztec, QR Code, Vericode, PDF417, Ultracode, Code 49, Code 16K等。
  1.堆叠式/行排式二维条码,如,Code 16K、Code 49、PDF417(如下图)等


 
    2.矩阵式二维码,最流行莫过于QR CODE
二维码的名称是相对与一维码来说的,比如以前的条形码就是一个“一维码”,
它的优点有:二维码存储的数据量更大;可以包含数字、字符,及中文文本等混合内容;有一定的容错性(在部分损坏以后可以正常读取);空间利用率高等。

二维码原理介绍:

QR(Quick-Response) code是被广泛使用的一种二维码,解码速度快。
它可以存储多用类型




 
如上图时一个qrcode的基本结构,其中:
位置探测图形、位置探测图形分隔符、定位图形:用于对二维码的定位,对每个QR码来说,位置都是固定存在的,只是大小规格会有所差异;
校正图形:规格确定,校正图形的数量和位置也就确定了;
格式信息:表示改二维码的纠错级别,分为L、M、Q、H;

版本信息:即二维码的规格,QR码符号共有40种规格的矩阵(一般为黑白色),从21x21(版本1),到177x177(版本40),每一版本符号比前一版本 每边增加4个模块。
数据和纠错码字:实际保存的二维码信息,和纠错码字(用于修正二维码损坏带来的错误)。

简要的编码过程:
    1. 数据分析:确定编码的字符类型,按相应的字符集转换成符号字符; 选择纠错等级,在规格一定的条件下,纠错等级越高其真实数据的容量越小。

    2. 数据编码:将数据字符转换为位流,每8位一个码字,整体构成一个数据的码字序列。其实知道这个数据码字序列就知道了二维码的数据内容。
          
 

 
 
            数据可以按照一种模式进行编码,以便进行更高效的解码,例如:对数据:01234567编码(版本1-H),
            1)分组:012 345 67

            2)转成二进制:

012→0000001100

                345→0101011001
                67 →1000011
            3)转成序列:0000001100 0101011001 1000011
            4)字符数 转成二进制:8→0000001000
            5)加入模式指示符(上图数字)0001:0001 0000001000 0000001100 0101011001 1000011
           对于字母、中文、日文等只是分组的方式、模式等内容有所区别。基本方法是一致的

    3. 纠错编码:按需要将上面的码字序列分块,并根据纠错等级和分块的码字,产生纠错码字,并把纠错码字加入到数据码字序列后面,成为一个新的序列。
             

 
        在二维码规格和纠错等级确定的情况下,其实它所能容纳的码字总数和纠错码字数也就确定了,比如:版本10,纠错等级时H时,总共能容纳346个码字,其中224个纠错码字。
        就是说二维码区域中大约1/3的码字时冗余的。对于这224个纠错码字,它能够纠正112个替代错误(如黑白颠倒)或者224个据读错误(无法读到或者无法译码),
        这样纠错容量为:112/346=32.4%
       
    4. 构造最终数据信息:在规格确定的条件下,将上面产生的序列按次序放如分块中
        按规定把数据分块,然后对每一块进行计算,得出相应的纠错码字区块,把纠错码字区块 按顺序构成一个序列,添加到原先的数据码字序列后面。
        如:D1, D12, D23, D35, D2, D13, D24, D36, ... D11, D22, D33, D45, D34, D46, E1, E23,E45, E67, E2, E24, E46, E68,...

构造矩阵:将探测图形、分隔符、定位图形、校正图形和码字模块放入矩阵中。
         

 把上面的完整序列填充到相应规格的二维码矩阵的区域中


    6. 掩摸:将掩摸图形用于符号的编码区域,使得二维码图形中的深色和浅色(黑色和白色)区域能够比率最优的分布。
    7. 格式和版本信息:生成格式和版本信息放入相应区域内。
        版本7-40都包含了版本信息,没有版本信息的全为0。二维码上两个位置包含了版本信息,它们是冗余的。
        版本信息共18位,6X3的矩阵,其中6位时数据为,如版本号8,数据位的信息时 001000,后面的12位是纠错位。
     

二维码现在随处可见,使用Android代码根据输入的字符串生成二维码其实也很简单,其中需要引用一个Google开源的包--ZXing。

下面这个例子里包含条形码和QR码的生成和解析,下面讲解二维码的生成。

首先,给出实现的截图:



生成二维码的步骤如下:

1.首先用户在编辑框中输入需要生成的字符串内容

2.点击下方的按钮

3.按钮下方的ImageView控件显示生成的二维码


下面给出实现的具体代码:

1.界面的布局

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="@android:color/white"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <Button  
  9.         android:id="@+id/btn_scan_barcode"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_marginTop="30dp"  
  13.         android:text="Open camera" />  
  14.       
  15.     <LinearLayout   
  16.         android:orientation="horizontal"  
  17.         android:layout_marginTop="10dp"  
  18.         android:layout_width="fill_parent"  
  19.         android:layout_height="wrap_content">  
  20.           
  21.         <TextView   
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content"  
  24.         android:textColor="@android:color/black"  
  25.         android:textSize="18sp"  
  26.         android:text="Scan result:" />  
  27.           
  28.         <TextView   
  29.         android:id="@+id/tv_scan_result"  
  30.         android:layout_width="fill_parent"  
  31.         android:textSize="18sp"  
  32.         android:textColor="@android:color/black"  
  33.         android:layout_height="wrap_content" />  
  34.     </LinearLayout>  
  35.       
  36.     <EditText   
  37.         android:id="@+id/et_qr_string"  
  38.         android:layout_width="fill_parent"  
  39.         android:layout_height="wrap_content"  
  40.         android:layout_marginTop="30dp"  
  41.         android:hint="Input the text"/>  
  42.       
  43.     <Button  
  44.         android:id="@+id/btn_add_qrcode"  
  45.         android:layout_width="fill_parent"  
  46.         android:layout_height="wrap_content"  
  47.         android:text="Generate QRcode" />  
  48.       
  49.     <ImageView   
  50.         android:id="@+id/iv_qr_image"  
  51.         android:layout_width="wrap_content"  
  52.         android:layout_height="wrap_content"  
  53.         android:layout_marginTop="10dp"  
  54.         android:layout_gravity="center"/>  
  55.   
  56. </LinearLayout>  

2.生成二维码的代码

[java]  view plain copy
  1. package com.zxing.encoding;  
  2.   
  3. import java.util.Hashtable;  
  4.   
  5. import android.graphics.Bitmap;  
  6.   
  7. import com.google.zxing.BarcodeFormat;  
  8. import com.google.zxing.EncodeHintType;  
  9. import com.google.zxing.MultiFormatWriter;  
  10. import com.google.zxing.WriterException;  
  11. import com.google.zxing.common.BitMatrix;  
  12. /** 
  13.  * @author Ryan Tang 
  14.  * 
  15.  */  
  16. public final class EncodingHandler {  
  17.     private static final int BLACK = 0xff000000;  
  18.       
  19.     public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException {  
  20.         Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();    
  21.         hints.put(EncodeHintType.CHARACTER_SET, "utf-8");   
  22.         BitMatrix matrix = new MultiFormatWriter().encode(str,  
  23.                 BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);  
  24.         int width = matrix.getWidth();  
  25.         int height = matrix.getHeight();  
  26.         int[] pixels = new int[width * height];  
  27.           
  28.         for (int y = 0; y < height; y++) {  
  29.             for (int x = 0; x < width; x++) {  
  30.                 if (matrix.get(x, y)) {  
  31.                     pixels[y * width + x] = BLACK;  
  32.                 }  
  33.             }  
  34.         }  
  35.         Bitmap bitmap = Bitmap.createBitmap(width, height,  
  36.                 Bitmap.Config.ARGB_8888);  
  37.         bitmap.setPixels(pixels, 0, width, 00, width, height);  
  38.         return bitmap;  
  39.     }  
  40. }  

3.Activity上的操作实现

[java]  view plain copy
  1. package com.ericssonlabs;  
  2.   
  3. import com.google.zxing.WriterException;  
  4. import com.zxing.activity.CaptureActivity;  
  5. import com.zxing.encoding.EncodingHandler;  
  6.   
  7. import android.app.Activity;  
  8. import android.content.Intent;  
  9. import android.graphics.Bitmap;  
  10. import android.os.Bundle;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.Button;  
  14. import android.widget.EditText;  
  15. import android.widget.ImageView;  
  16. import android.widget.TextView;  
  17. import android.widget.Toast;  
  18.   
  19. public class BarCodeTestActivity extends Activity {  
  20.     /** Called when the activity is first created. */  
  21.     private TextView resultTextView;  
  22.     private EditText qrStrEditText;  
  23.     private ImageView qrImgImageView;  
  24.       
  25.     @Override  
  26.     public void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.main);  
  29.           
  30.         resultTextView = (TextView) this.findViewById(R.id.tv_scan_result);  
  31.         qrStrEditText = (EditText) this.findViewById(R.id.et_qr_string);  
  32.         qrImgImageView = (ImageView) this.findViewById(R.id.iv_qr_image);  
  33.           
  34.         Button scanBarCodeButton = (Button) this.findViewById(R.id.btn_scan_barcode);  
  35.         scanBarCodeButton.setOnClickListener(new OnClickListener() {  
  36.               
  37.             @Override  
  38.             public void onClick(View v) {  
  39.                 Intent openCameraIntent = new Intent(BarCodeTestActivity.this,CaptureActivity.class);  
  40.                 startActivityForResult(openCameraIntent, 0);  
  41.             }  
  42.         });  
  43.           
  44.         Button generateQRCodeButton = (Button) this.findViewById(R.id.btn_add_qrcode);  
  45.         generateQRCodeButton.setOnClickListener(new OnClickListener() {  
  46.               
  47.             @Override  
  48.             public void onClick(View v) {  
  49.                 try {  
  50.                     String contentString = qrStrEditText.getText().toString();  
  51.                     if (!contentString.equals("")) {  
  52.                         Bitmap qrCodeBitmap = EncodingHandler.createQRCode(contentString, 350);  
  53.                         qrImgImageView.setImageBitmap(qrCodeBitmap);  
  54.                     }else {  
  55.                         Toast.makeText(BarCodeTestActivity.this"Text can not be empty", Toast.LENGTH_SHORT).show();  
  56.                     }  
  57.                       
  58.                 } catch (WriterException e) {  
  59.                     // TODO Auto-generated catch block  
  60.                     e.printStackTrace();  
  61.                 }  
  62.             }  
  63.         });  
  64.     }  
  65.   
  66.     @Override  
  67.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  68.         super.onActivityResult(requestCode, resultCode, data);  
  69.         if (resultCode == RESULT_OK) {  
  70.             Bundle bundle = data.getExtras();  
  71.             String scanResult = bundle.getString("result");  
  72.             resultTextView.setText(scanResult);  
  73.         }  
  74.     }  
  75. }  

下面给出Demo的下载地址:

http://download.csdn.net/detail/dlutbrucezhang/5066053

相关文章
|
9月前
|
Android开发
Android中实现获取相册中的图片扫描二维码的功能
Android中实现获取相册中的图片扫描二维码的功能
187 0
|
2天前
|
API Android开发
Android高手进阶教程(十五)之---通过Location获取Address的使用!
Android高手进阶教程(十五)之---通过Location获取Address的使用!
|
5月前
|
算法 Android开发
【Android App】二维码的讲解及生成属于自己的二维码实战(附源码和演示 超详细必看)
【Android App】二维码的讲解及生成属于自己的二维码实战(附源码和演示 超详细必看)
94 0
|
8月前
|
编解码 网络协议 Android开发
Android平台RTMP|RTSP直播播放器功能进阶探讨
很多开发者在跟我聊天的时候,经常问我,为什么一个RTMP或RTSP播放器,你们需要设计那么多的接口,真的有必要吗?带着这样的疑惑,我们今天聊聊Android平台RTMP、RTSP播放器常规功能,如软硬解码设置、实时音量调节、实时快照、实时录像、视频view翻转和旋转、画面填充模式设定、解码后YUV、RGB数据回调等:
111 0
|
11月前
|
Java Shell Android开发
支付宝二维码脱机认证库在android的app下测试过程记录
支付宝二维码脱机认证库在android的app下测试过程记录
|
Web App开发 编解码 前端开发
Android | 音视频方向进阶路线及资源合集
但是系统相机和系统控件VideoView的局限性都是可定制型太差,系统相机的图像分辨率,视频码率以及VideoView的进度条等.
165 0
|
Java Android开发 Kotlin
一些实用的Android进阶小技巧
主要是利用application获取唯一的全局实例context,使得我们在任何场景都可以获取context
78 0
一些实用的Android进阶小技巧
|
前端开发 Android开发
Android进阶之绘制-自定义View完全掌握(四)
Android进阶之绘制-自定义View完全掌握(四)
87 0
Android进阶之绘制-自定义View完全掌握(四)