編輯:關於 Android 程式設計
在正式開始之前,先簡單介紹一下本文的主角——ZXing 庫。ZXing 是一個開源的、用 Java 實現的多格式 1D/2D 條碼圖像處理庫,同時也包含了通向其他語言的移植版本。它能夠調用手機內置攝像頭完成條形碼的掃描與解碼,支持 UPC-A、UPC-E、EAN-8、EAN-13、Code 39、Code 93 等多種格式。
ZXing 是相當經典的條碼/二維碼識別開源類庫,早在功能機時代就有開發者用 J2ME 調用它,不過要發揮全部威力,需要手機支持 JSR-234 規範(自動對焦)。它同時也是二維碼生成與識別領域公認的基礎庫。
ZXing 的 GitHub 地址可在官網查閱。
效果圖:

第一步,把 libzxing 這個模塊導入到項目中:

需要說明的是,ZXing 的源碼規模龐大、功能繁多,這裡只是抽取了其中一部分代碼整合到一起,形成一個輕量的示例工程。
先在 main_activity 中放置一個 Button 和一個 TextView。點擊 Button 後啟動相機掃描二維碼,TextView 則用來展示掃描結果:
<Button android:text="Strat Scan" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="scan"/> <TextView android:id="@+id/tv_showResult" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!"></TextView>
接著在 MainActivity 中初始化控件,並處理掃描返回的結果:
private TextView mTextView; mTextView= (TextView) this.findViewById(R.id.tv_showResult); //掃描二維碼 public void scan(View view) { startActivityForResult(new Intent(this, CaptureActivity.class),0); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode==RESULT_OK){ Bundle bundle = data.getExtras(); if (bundle != null) { String result=bundle.getString("result"); mTextView.setText(result); } } }
下面把整個項目的佈局文件完整貼出來,其中也包含了前面的掃描相關控件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context="com.example.hfs.zxingdemo.MainActivity"> <Button android:text="Strat Scan" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="scan"/> <TextView android:id="@+id/tv_showResult" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!"/> <EditText android:id="@+id/et_text" android:hint="Imput" android:layout_width="match_parent" android:layout_height="wrap_content"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="make" android:text="Make QRCode"/> <CheckBox android:id="@+id/cb_logo" android:layout_width="wrap_content" android:text="Logo" android:layout_height="wrap_content"/> <ImageView android:id="@+id/img_shouw" android:layout_width="wrap_content" android:layout_gravity="center" android:background="@mipmap/ic_launcher" android:layout_height="wrap_content"/> </LinearLayout>
MainActivity 的完整代碼如下,生成二維碼時還支持選擇是否叠加 Logo:
package com.example.hfs.zxingdemo; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; 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 TextView mTextView; private EditText mEditText; private ImageView mImageView; private CheckBox mCheckBox; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { mTextView= (TextView) this.findViewById(R.id.tv_showResult); mEditText= (EditText) this.findViewById(R.id.et_text); mImageView= (ImageView) this.findViewById(R.id.img_shouw); mCheckBox= (CheckBox) this.findViewById(R.id.cb_logo); } //掃描二維碼 public void scan(View view) { startActivityForResult(new Intent(this, CaptureActivity.class),0); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode==RESULT_OK){ Bundle bundle = data.getExtras(); if (bundle != null) { String result=bundle.getString("result"); mTextView.setText(result); } } } //生成二維碼 可以設置Logo public void make(View view) { String input = mEditText.getText().toString(); if (input.equals("")){ Toast.makeText(this,"輸入不能為空",Toast.LENGTH_SHORT).show(); }else{ Bitmap qrCode = EncodingUtils.createQRCode(input, 500, 500, mCheckBox.isChecked()? BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher):null); mImageView.setImageBitmap(qrCode); } } }
至此,掃描與生成二維碼的功能就全部實現了。
以上所述就是本文介紹的 Android 實現二維碼掃描與生成的簡潔方法,希望對大家有所幫助。如果有任何疑問歡迎留言,我會及時回復,也非常感謝大家對本站的支持!
Android 自定義 SubmitView:提交動畫、進度圈與成功狀態
SubmitView 的互動情境 在提交表單、下單或上傳檔案時,使用者常需要一個能表達等待與完成的元件。SubmitView 可用來呈現提交按鈕、載入進度、成功勾選與重置
Android 專案導入第三方 JAR 包的兩種做法
Android 專案引用外部 JAR 這件事,和純 Java 專案有本質上的差別:最後產出的是 APK,JAR 裡的位元組碼必須一起被打包並完成 dex 轉換,否則執行期
Android 內存優化之 LruCache 詳解
概念 什麼是 LruCache?它的實現原理又是什麼?這兩個問題其實可以合二為一:理解了 LruCache 是什麼,自然也明白了它的實現原理。Lru 的全稱是 Least
Android 仿今日頭條:滑動標籤頁導航效果實作
近期專案需要用到滑動式頁面切換,效果類似目前很紅的「今日頭條」。上網查資料後發現,大多數實作都是採用 ViewPager。最初我嘗試的是 ViewPager + View