製作二維碼

1,在project的build.gradle添加如下代碼(如下圖):
allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

這裏寫圖片描述

2,在app/build.gradle文件中添加:

compile 'com.github.open-android:Zxing:v1.0.3'

3,添加相應的權限:

<uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.FLASHLIGHT" 

4,main.xml佈局:

<?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.classical.example_zxing.MainActivity">

    <EditText
        android:id="@+id/edt_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="請輸入內容" />

    <Button
        android:id="@+id/btn_create"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="生成二維碼" />

    <Button
        android:id="@+id/btn_scan"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="掃碼(支持識別相冊二維碼)" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">

        <ImageView
            android:id="@+id/iv_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:visibility="gone" />

        <TextView
            android:id="@+id/tv_hint"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="掃描返回bitmap"
            android:textColor="#f00" />
    </RelativeLayout>

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ffff00"/>
</LinearLayout>

5,如何在代碼中使用:

package com.classical.example_zxing;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.activity.CaptureActivity;
import com.google.zxing.common.BitmapUtils;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText mContent;
    private Button mCreate,mScan;
    private ImageView mImage;
    private final static int REQ_CODE = 1028;
    private TextView mHint;
    private TextView mResult;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        mContent = (EditText)findViewById(R.id.edt_content);
        mCreate = (Button)findViewById(R.id.btn_create);
        mScan = (Button)findViewById(R.id.btn_scan);

        mImage = (ImageView)findViewById(R.id.iv_image);
        mHint = (TextView)findViewById(R.id.tv_hint);
        mResult = (TextView)findViewById(R.id.tv_result);

        mCreate.setOnClickListener(this);
        mScan.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_create :
                //生成二維碼
                String content = mContent.getText().toString().trim();
                Bitmap bitmap = null;
                try {
                    bitmap = BitmapUtils.create2DCode(content);
                    mImage.setVisibility(View.VISIBLE);
                    mHint.setVisibility(View.GONE);
                    mImage.setImageBitmap(bitmap);
                }catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_scan :
                //掃碼
                Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
                startActivityForResult(intent,REQ_CODE);
                break;

            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQ_CODE) {
            mImage.setVisibility(View.VISIBLE);
            mContent.setVisibility(View.GONE);

            String result = data.getStringExtra(CaptureActivity.SCAN_QRCODE_RESULT);
            Bitmap bitmap = data.getParcelableExtra(CaptureActivity.SCAN_QRCODE_BITMAP);

            mResult.setText("掃描結果爲:" + result);
            showToast("掃碼結果:"+result);
            if (bitmap != null) {
                mImage.setImageBitmap(bitmap);
            }

        }
    }

    private void showToast(String msg) {
        Toast.makeText(MainActivity.this, "" + msg, Toast.LENGTH_SHORT).show();
    }
}



發佈了53 篇原創文章 · 獲贊 8 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章