Android應(yīng)用--QR的生成(二維碼)
二維碼的定義:
二維碼(2-dimensional bar code),是用某種特定的幾何圖形按一定規(guī)律在平面(二維方向上)分布的黑白相間的圖形記錄數(shù)據(jù)符號(hào)信息的。
在許多種類的二維條碼中,常用的碼制有:Data Matrix, Maxi Code, Aztec, QR Code, Vericode, PDF417, Ultracode, Code 49, Code 16K等。
1.堆疊式/行排式二維條碼,如,Code 16K、Code 49、PDF417(如下圖)等。
2.矩陣式二維碼,***莫過(guò)于QR CODE
二維碼的名稱是相對(duì)與一維碼來(lái)說(shuō)的,比如以前的條形碼就是一個(gè)“一維碼”,它的優(yōu)點(diǎn)有:二維碼存儲(chǔ)的數(shù)據(jù)量更大;可以包含數(shù)字、字符,及中文文本等混合內(nèi)容;有一定的容錯(cuò)性(在部分損壞以后可以正常讀?。豢臻g利用率高等。
二維碼原理介紹:
QR(Quick-Response) code是被廣泛使用的一種二維碼,解碼速度快。
它可以存儲(chǔ)多用類型
如上圖時(shí)一個(gè)qrcode的基本結(jié)構(gòu),其中:
位置探測(cè)圖形、位置探測(cè)圖形分隔符、定位圖形:用于對(duì)二維碼的定位,對(duì)每個(gè)QR碼來(lái)說(shuō),位置都是固定存在的,只是大小規(guī)格會(huì)有所差異;
校正圖形:規(guī)格確定,校正圖形的數(shù)量和位置也就確定了;
格式信息:表示改二維碼的糾錯(cuò)級(jí)別,分為L(zhǎng)、M、Q、H;
版本信息:即二維碼的規(guī)格,QR碼符號(hào)共有40種規(guī)格的矩陣(一般為黑白色),從21x21(版本1),到177x177(版本40),每一版本符號(hào)比前一版本 每邊增加4個(gè)模塊;
數(shù)據(jù)和糾錯(cuò)碼字:實(shí)際保存的二維碼信息,和糾錯(cuò)碼字(用于修正二維碼損壞帶來(lái)的錯(cuò)誤)。
簡(jiǎn)要的編碼過(guò)程:
1. 數(shù)據(jù)分析:確定編碼的字符類型,按相應(yīng)的字符集轉(zhuǎn)換成符號(hào)字符; 選擇糾錯(cuò)等級(jí),在規(guī)格一定的條件下,糾錯(cuò)等級(jí)越高其真實(shí)數(shù)據(jù)的容量越小。
2. 數(shù)據(jù)編碼:將數(shù)據(jù)字符轉(zhuǎn)換為位流,每8位一個(gè)碼字,整體構(gòu)成一個(gè)數(shù)據(jù)的碼字序列。其實(shí)知道這個(gè)數(shù)據(jù)碼字序列就知道了二維碼的數(shù)據(jù)內(nèi)容。
數(shù)據(jù)可以按照一種模式進(jìn)行編碼,以便進(jìn)行更高效的解碼,例如:對(duì)數(shù)據(jù):01234567編碼(版本1-H),
1)分組:012 345 67
2)轉(zhuǎn)成二進(jìn)制:
012→0000001100
345→0101011001
67 →1000011
3)轉(zhuǎn)成序列:0000001100 0101011001 1000011
4)字符數(shù) 轉(zhuǎn)成二進(jìn)制:8→0000001000
5)加入模式指示符(上圖數(shù)字)0001:0001 0000001000 0000001100 0101011001 1000011
對(duì)于字母、中文、日文等只是分組的方式、模式等內(nèi)容有所區(qū)別?;痉椒ㄊ且恢碌?br />
3. 糾錯(cuò)編碼:按需要將上面的碼字序列分塊,并根據(jù)糾錯(cuò)等級(jí)和分塊的碼字,產(chǎn)生糾錯(cuò)碼字,并把糾錯(cuò)碼字加入到數(shù)據(jù)碼字序列后面,成為一個(gè)新的序列。
在二維碼規(guī)格和糾錯(cuò)等級(jí)確定的情況下,其實(shí)它所能容納的碼字總數(shù)和糾錯(cuò)碼字?jǐn)?shù)也就確定了,比如:版本10,糾錯(cuò)等級(jí)時(shí)H時(shí),總共能容納346個(gè)碼字,其中224個(gè)糾錯(cuò)碼字。
就是說(shuō)二維碼區(qū)域中大約1/3的碼字時(shí)冗余的。對(duì)于這224個(gè)糾錯(cuò)碼字,它能夠糾正112個(gè)替代錯(cuò)誤(如黑白顛倒)或者224個(gè)據(jù)讀錯(cuò)誤(無(wú)法讀到或者無(wú)法譯碼),
這樣糾錯(cuò)容量為:112/346=32.4%
4. 構(gòu)造最終數(shù)據(jù)信息:在規(guī)格確定的條件下,將上面產(chǎn)生的序列按次序放如分塊中
按規(guī)定把數(shù)據(jù)分塊,然后對(duì)每一塊進(jìn)行計(jì)算,得出相應(yīng)的糾錯(cuò)碼字區(qū)塊,把糾錯(cuò)碼字區(qū)塊 按順序構(gòu)成一個(gè)序列,添加到原先的數(shù)據(jù)碼字序列后面。
如:D1, D12, D23, D35, D2, D13, D24, D36, ... D11, D22, D33, D45, D34, D46, E1, E23,E45, E67, E2, E24, E46, E68,...
構(gòu)造矩陣:將探測(cè)圖形、分隔符、定位圖形、校正圖形和碼字模塊放入矩陣中。
把上面的完整序列填充到相應(yīng)規(guī)格的二維碼矩陣的區(qū)域中
6. 掩摸:將掩摸圖形用于符號(hào)的編碼區(qū)域,使得二維碼圖形中的深色和淺色(黑色和白色)區(qū)域能夠比率***的分布。
7. 格式和版本信息:生成格式和版本信息放入相應(yīng)區(qū)域內(nèi)。
版本7-40都包含了版本信息,沒(méi)有版本信息的全為0。二維碼上兩個(gè)位置包含了版本信息,它們是冗余的。
版本信息共18位,6X3的矩陣,其中6位時(shí)數(shù)據(jù)為,如版本號(hào)8,數(shù)據(jù)位的信息時(shí) 001000,后面的12位是糾錯(cuò)位。
二維碼現(xiàn)在隨處可見(jiàn),使用Android代碼根據(jù)輸入的字符串生成二維碼其實(shí)也很簡(jiǎn)單,其中需要引用一個(gè)Google開(kāi)源的包--ZXing。
下面這個(gè)例子里包含條形碼和QR碼的生成和解析,下面講解二維碼的生成。
首先,給出實(shí)現(xiàn)的截圖:
生成二維碼的步驟如下:
1.首先用戶在編輯框中輸入需要生成的字符串內(nèi)容
2.點(diǎn)擊下方的按鈕
3.按鈕下方的ImageView控件顯示生成的二維碼
下頁(yè)將為你展示具體代碼和發(fā)放源代碼
#p#
下面給出實(shí)現(xiàn)的具體代碼:
1.界面的布局
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@android:color/white"
- android:orientation="vertical" >
- <Button
- android:id="@+id/btn_scan_barcode"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="30dp"
- android:text="Open camera" />
- <LinearLayout
- android:orientation="horizontal"
- android:layout_marginTop="10dp"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="@android:color/black"
- android:textSize="18sp"
- android:text="Scan result:" />
- <TextView
- android:id="@+id/tv_scan_result"
- android:layout_width="fill_parent"
- android:textSize="18sp"
- android:textColor="@android:color/black"
- android:layout_height="wrap_content" />
- </LinearLayout>
- <EditText
- android:id="@+id/et_qr_string"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="30dp"
- android:hint="Input the text"/>
- <Button
- android:id="@+id/btn_add_qrcode"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Generate QRcode" />
- <ImageView
- android:id="@+id/iv_qr_image"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dp"
- android:layout_gravity="center"/>
- nearLayout>
2.生成二維碼的代碼
- package com.zxing.encoding;
- import java.util.Hashtable;
- import android.graphics.Bitmap;
- import com.google.zxing.BarcodeFormat;
- import com.google.zxing.EncodeHintType;
- import com.google.zxing.MultiFormatWriter;
- import com.google.zxing.WriterException;
- import com.google.zxing.common.BitMatrix;
- /**
- * @author Ryan Tang
- *
- */
- public final class EncodingHandler {
- private static final int BLACK = 0xff000000;
- public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException {
- Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
- hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
- BitMatrix matrix = new MultiFormatWriter().encode(str,
- BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
- 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] = BLACK;
- }
- }
- }
- Bitmap bitmap = Bitmap.createBitmap(width, height,
- Bitmap.Config.ARGB_8888);
- bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
- return bitmap;
- }
- }
3.Activity上的操作實(shí)現(xiàn)
- package com.ericssonlabs;
- import com.google.zxing.WriterException;
- import com.zxing.activity.CaptureActivity;
- import com.zxing.encoding.EncodingHandler;
- import android.app.Activity;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.Toast;
- public class BarCodeTestActivity extends Activity {
- /** Called when the activity is first created. */
- private TextView resultTextView;
- private EditText qrStrEditText;
- private ImageView qrImgImageView;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- resultTextView = (TextView) this.findViewById(R.id.tv_scan_result);
- qrStrEditText = (EditText) this.findViewById(R.id.et_qr_string);
- qrImgImageView = (ImageView) this.findViewById(R.id.iv_qr_image);
- Button scanBarCodeButton = (Button) this.findViewById(R.id.btn_scan_barcode);
- scanBarCodeButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent openCameraIntent = new Intent(BarCodeTestActivity.this,CaptureActivity.class);
- startActivityForResult(openCameraIntent, 0);
- }
- });
- Button generateQRCodeButton = (Button) this.findViewById(R.id.btn_add_qrcode);
- generateQRCodeButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- try {
- String contentString = qrStrEditText.getText().toString();
- if (!contentString.equals("")) {
- Bitmap qrCodeBitmap = EncodingHandler.createQRCode(contentString, 350);
- qrImgImageView.setImageBitmap(qrCodeBitmap);
- }else {
- Toast.makeText(BarCodeTestActivity.this, "Text can not be empty", Toast.LENGTH_SHORT).show();
- }
- } catch (WriterException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- });
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if (resultCode == RESULT_OK) {
- Bundle bundle = data.getExtras();
- String scanResult = bundle.getString("result");
- resultTextView.setText(scanResult);
- }
- }
- }
下面給出Demo的下載地址(注:下載前需注冊(cè)第三方網(wǎng)站會(huì)員):
http://download.csdn.net/detail/dlutbrucezhang/5066053
或
下載源代碼:點(diǎn)擊這里http://code.google.com/p/zxing/downloads/detail?name=ZXing-1.6.zip&can=2&q=