編輯:關於 Android 開發
二維碼的掃描與生成是許多App的常見需求,本文利用開源庫ZXing來實現這兩項功能。ZXing二維碼功能的lib可從以下地址下載:https://github.com/xuyisheng/ZXingLib
掃描功能需要用到CaptureActivity類別,把上述地址中的libzxing作為Module導入專案即可:


首先加上相機與振動權限:
<!-- 相機 --> <uses-permission android:name="android.permission.CAMERA" /> <!-- 振動 --> <uses-permission android:name="android.permission.VIBRATE" />
由於已將其作為Module引入,可以把CaptureActivity的AndroidManifest.xml註冊資訊拷貝到本專案中:
<activity android:name="com.xys.libzxing.zxing.activity.CaptureActivity" android:configChanges="orientation|keyboardHidden" android:screenOrientation="portrait" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:windowSoftInputMode="stateAlwaysHidden"> </activity>
在activity_main.xml中聲明一個Button:
<Button android:id="@+id/btnSan" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="掃描二維碼" />
在Java程式碼中初始化後加上點擊事件:
findViewById(R.id.btnSan).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivityForResult(new Intent(MainActivity.this, CaptureActivity.class), 0); } });
若要查看返回結果,可在activity_main.xml中添加一個TextView:
<TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" />
並在Java程式碼中處理返回結果:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { String result = data.getExtras().getString("result"); tv_content.setText(result); } }
這樣就能看到掃描返回的內容了,以微信二維碼為例:


生成二維碼需要三個要素:要生成的內容、生成的按鈕,以及存放生成結果的容器。因此在佈局檔中加入以下元件:
<EditText android:id="@+id/et_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/tv_content" android:layout_marginTop="10dp" android:hint="請輸入要生成的二維碼文字" /> <Button android:id="@+id/btn_generate" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/et_input" android:layout_marginTop="10dp" android:text="生成二維碼" /> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btn_generate" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" />
初始化這些元件後,在Button的點擊事件中撰寫:
findViewById(R.id.btn_generate).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String str = et_input.getText().toString(); if (str.equals("")) { Toast.makeText(MainActivity.this, "不能為空", Toast.LENGTH_SHORT).show(); } else { try { /** 參數:1.文本 2 3.二維碼的寬高 4.二維碼中間的那個logo */ Bitmap bitmap = EncodingUtils.createQRCode(str, 500, 500, null); img.setImageBitmap(bitmap); } catch (Exception e) { e.printStackTrace(); } } } });
運行後即可簡單地生成二維碼:

以上生成的二維碼不含logo,若需要加入logo,只需把createQRCode方法最後一個參數null改為自己需要的logo即可。
以下是完整的程式碼:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.zzw.testerweima.MainActivity"> <Button android:id="@+id/btnSan" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="掃描二維碼" /> <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btnSan" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" /> <EditText android:id="@+id/et_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/tv_content" android:layout_marginTop="10dp" android:hint="請輸入要生成的二維碼文字" /> <Button android:id="@+id/btn_generate" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/et_input" android:layout_marginTop="10dp" android:text="生成二維碼" /> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btn_generate" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" /> </RelativeLayout>
package com.zzw.testerweima; import android.content.Intent; import android.graphics.Bitmap; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; 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 TextView tv_content; private EditText et_input; private ImageView img; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_content = (TextView) findViewById(R.id.tv_content); et_input = (EditText) findViewById(R.id.et_input); img = (ImageView) findViewById(R.id.img); findViewById(R.id.btnSan).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivityForResult(new Intent(MainActivity.this, CaptureActivity.class), 0); } }); findViewById(R.id.btn_generate).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String str = et_input.getText().toString(); if (str.equals("")) { Toast.makeText(MainActivity.this, "不能為空", Toast.LENGTH_SHORT).show(); } else { try { Bitmap bitmap = EncodingUtils.createQRCode(str, 500, 500, null); img.setImageBitmap(bitmap); } catch (Exception e) { e.printStackTrace(); } } } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { String result = data.getExtras().getString("result"); Log.d("Main", result); tv_content.setText(result); } } }
RxJava 筆記(二):merge 運算符如何合併多個 Observable
RxJava 筆記(二):merge 運算符如何合併多個 Observable 前言:對 Rx 還不熟悉的朋友,可以先看第一篇〈RxJava 和 RxAndroid〉,那
Android 特效專輯(八)——打造心型氣泡飛舞特效,讓你的 App 瞬間暖心
Android 特效專輯(八)——實現心型氣泡飛舞的特效,讓你的 APP 瞬間暖心 馬上也要放年假了,家裡估計會沒網,更完這篇可能要到年後了。
TickPlusDrawable:讓「加號/勾選」切換帶上動畫的開源 Drawable
TickPlusDrawable:讓「加號/勾選」切換帶上動畫的開源 Drawable 在開發清單、收藏、關注這類功能時,我們常常需要一個可以在「加號」與「打勾」之間切
Android 仿即刻首頁垂直滾動圖:自訂 ViewGroup 動畫
Android 仿即刻首頁垂直滾動圖:自訂 ViewGroup 動畫 首頁頂部垂直輪播圖片是常見的視覺效果。本文以仿即刻 App 的垂直滾動圖為例,說明如何透過自訂 Vi