Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 二維碼掃描與生成的簡潔實作方法

編輯:關於 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 實現二維碼掃描與生成的簡潔方法,希望對大家有所幫助。如果有任何疑問歡迎留言,我會及時回復,也非常感謝大家對本站的支持!

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved