1. 程式人生 > >二維碼的掃描和生成二維碼

二維碼的掃描和生成二維碼

前言

之前自己一直想要去實現一個二維碼的掃描和生成,但是一直拖到現在,今天趁著夜色落幕,氣氛還算可以(各種聲音的夾雜中),完成了這個掃描和生成二維碼的工具,在這裡總結一下。  首先普及一下什麼是二維碼和二維碼開源庫

QR Code

  • QRCode簡介: 
    • QRCode全稱Quick Response Code
    • 通過在一個矩形區域內使用黑白畫素來進行編碼
    • 高糾錯性、高可用性、高識別性

ZXing簡介:

  • ZXing是一個開放原始碼的,用Java實現的多種格式的1D/2D條形影象處理庫,它包含了聯絡到其他語言的埠
  • ZXing可以實現使用手機的內建的攝像頭完成條形碼的掃描和解碼

專案開始前

我們可以看下這個zxing,我們不可能去下載整個專案然後讓我們的專案去依賴他,因為他足足有125MB左右啊,再說我們只需要實現可以在手機上實現掃描和生成二維碼就行了,所以就有大神們從裡面抽取了部分有關這方面的類和方法,這裡我們可以找一下資料,這樣就會大大減少,不到1MB。  這裡先上專案地址(在這裡可以看到這個依賴庫):https://github.com/wuyinlei/LearnZxing

專案第一步:掃描二維碼功能實現

  • 完成工作的其實就是依賴庫中的CaptureActivity.java,這裡面已經為我們封裝好了呼叫相機,解析二維碼資訊的類,並提供了返回值,我們只需要去呼叫,使用
       startActivityForResult(new Intent(MainActivity.this, CaptureActivity.class), 0);

    然後重寫onActivityResult(int requestCode, int resultCode, Intent data)這個方法,在這個方法中我們獲取到掃描後獲得的資料

if (resultCode == RESULT_OK) {
            //接受返回的二維碼資料
            Bundle bundle = data.getExtras();
            //這個key是在CaptureActivity中的this.setResult(RESULT_OK, resultIntent)查到的
            String result = bundle.getString("result");
            tvResult.setText(result);
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

這個時候我們來看下實現的效果圖:  這個是我用線上二維碼生成器生成的一個二維碼,這個時候我們看下掃描的結果。   掃描之後的結果: 

好了,這個時候掃描的功能實現了

專案第二步:生成二維碼功能實現

其實這個也是很簡單的,因為library裡面已經提供了一個工具類EncodingUtils.java(二維碼生成工具),這裡面提供的這個方法

EncodingUtils.createQRCode(input, 500, 500, mCheckBox.isChecked() ? BitmapFactory.decodeResource(getResources(), R.mipmap.kenan) : null)

這個方法傳入四個引數
/**
     * 建立二維碼
     *
     * @param content   content     建立的二維碼中包含的資訊
     * @param widthPix  widthPix   寬度
     * @param heightPix heightPix  高度
     * @param logoBm    logoBm    是否在二維碼中間加入自定義的圖片
     * @return 二維碼
     */
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

我們來看下佈局檔案吧,也是很簡答的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.wuyin.learnzxing.MainActivity">

    <Button
        android:id="@+id/btn_start_scan"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="開始掃描"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Result : "
        android:textSize="16sp"/>

    <TextView
        android:id="@+id/result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:textSize="20sp"/>

    <EditText
        android:id="@+id/et_zxing"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/make_qrcode"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="建立二維碼"/>

    <ImageView
        android:id="@+id/showQrcode"
        android:layout_gravity="center"
        android:background="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <CheckBox
        android:id="@+id/checkbox"
        android:text="是否在二維碼中間生成圖片"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

因為就一個方法呼叫,這裡就不多做介紹了,大家有興趣可以下載原始碼看下,這裡不多做解釋,直接來看下生成的二維碼:  不需要生成中間自定義圖片的二維碼:   生成中間自定義圖片的二維碼: 

好了,整個專案到這就完事了,是不是很簡單,這個也要歸功於大牛們的很好的封裝哈,有了他們的封裝和開源,我們才能用簡單的幾行程式碼實現比較困難的需求。MainActivity.java中的程式碼也不是很多,這裡就附上了。

package com.example.wuyin.learnzxing;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ButtonBarLayout;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.xys.libzxing.zxing.activity.CaptureActivity;
import com.xys.libzxing.zxing.encoding.EncodingUtils;

public class MainActivity extends AppCompatActivity {

    private Button btnStartScan, btnMakeQrcode;

    private TextView tvResult,etZxing;

    private ImageView showQrcode;

    private CheckBox mCheckBox;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //掃描二維碼
        btnStartScan = (Button) findViewById(R.id.btn_start_scan);
        btnStartScan.setOnClickListener(new MyClickListener());
        tvResult = (TextView) findViewById(R.id.result);

        //生成二維碼
        btnMakeQrcode = (Button) findViewById(R.id.make_qrcode);
        btnMakeQrcode.setOnClickListener(new MyClickListener());
        etZxing = (EditText) findViewById(R.id.et_zxing);
        showQrcode = (ImageView) findViewById(R.id.showQrcode);
        mCheckBox = (CheckBox) findViewById(R.id.checkbox);
    }


    /**
     * 自定義的OnClickListener
     */
    class MyClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.btn_start_scan) {
                startActivityForResult(new Intent(MainActivity.this, CaptureActivity.class), 0);
            } else if (v.getId() == R.id.make_qrcode) {
                makeQrcode();
            }
        }
    }

    /**
     * 生成二維碼
     */
    private void makeQrcode() {
        String input = etZxing.getText().toString();
        if (!TextUtils.isEmpty(input)) {
            //二維碼生成工具類
            Bitmap bitmap = EncodingUtils.createQRCode(input, 500, 500, mCheckBox.isChecked() ? BitmapFactory.decodeResource(getResources(), R.mipmap.kenan) : null);
            showQrcode.setImageBitmap(bitmap);
        } else {
            Toast.makeText(this, "輸入不能為空", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            //接受返回的二維碼資料
            Bundle bundle = data.getExtras();
            //這個key是在CaptureActivity中的this.setResult(RESULT_OK, resultIntent)查到的
            String result = bundle.getString("result");
            tvResult.setText(result);
        }
    }
}