Android實(shí)現(xiàn)二維碼掃描和生成的簡(jiǎn)單方法
這里簡(jiǎn)單介紹一下ZXing庫(kù)。ZXing是一個(gè)開(kāi)放源碼的,用Java實(shí)現(xiàn)的多種格式的1D/2D條碼圖像處理庫(kù),它包含了聯(lián)系到其他語(yǔ)言的端口。Zxing可以實(shí)現(xiàn)使用手機(jī)的內(nèi)置的攝像頭完成條形碼的掃描及解碼。該項(xiàng)目可實(shí)現(xiàn)的條形碼編碼和解碼。目前支持以下格式:UPC-A,UPC-E、EAN-8,EAN-13、39碼、93碼。ZXing是個(gè)很經(jīng)典的條碼/二維碼識(shí)別的開(kāi)源類庫(kù),以前在功能機(jī)上,就有開(kāi)發(fā)者使用J2ME運(yùn)用ZXing了,不過(guò)要支持JSR-234規(guī)范(自動(dòng)對(duì)焦)的手機(jī)才能發(fā)揮其威力。
效果圖:

主要實(shí)現(xiàn)步驟:
導(dǎo)入libzxing這個(gè)模塊

ZXing源代碼很大,功能也很多,這里只是抽取了其中的一部分代碼整合到了一起
掃描
在main_activity中添加一個(gè)Button和一個(gè)TextView 點(diǎn)擊Button后開(kāi)始調(diào)照相機(jī)功能,掃描二維碼
TextView會(huì)顯示掃描后的結(jié)果
<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!"/>
在ActivityMain中分別初始化這兩個(gè)控件
private TextView mTextView;
mTextView= (TextView) this.findViewById(R.id.tv_showResult);
//掃描二維碼
//https://cli.im/text?2dd0d2b267ea882d797f03abf5b97d88二維碼生成網(wǎng)站
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);
}
}
}
生成二維碼
這里就把整個(gè)項(xiàng)目的XML文件都貼出來(lái)把,加上之前的掃描
<?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: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中代碼
package com.example.hfs.zxingdemo;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.camera2.CaptureRequest;
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);
}
//掃描二維碼
//https://cli.im/text?2dd0d2b267ea882d797f03abf5b97d88二維碼生成網(wǎng)站
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);
}
}
}
//生成二維碼 可以設(shè)置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);//CheckBox選中就設(shè)置Logo
mImageView.setImageBitmap(qrCode);
}
}
}
好了 到這里就寫完了
以上所述是小編給大家介紹的Android實(shí)現(xiàn)二維碼掃描和生成的簡(jiǎn)單方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
用Flutter開(kāi)發(fā)自定義Plugin的方法示例
這篇文章主要介紹了用Flutter開(kāi)發(fā)自定義Plugin的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
Android中使用PopupWindow 仿微信點(diǎn)贊和評(píng)論彈出
微信朋友圈的點(diǎn)贊和評(píng)論功能,有2個(gè)組成部分:左下角的“更多”按鈕;點(diǎn)擊該按鈕后彈出的對(duì)話框。這篇文章主要介紹了Android中使用PopupWindow 仿微信點(diǎn)贊和評(píng)論彈出,需要的朋友可以參考下2017-04-04
Android實(shí)現(xiàn)調(diào)用系統(tǒng)圖庫(kù)與相機(jī)設(shè)置頭像并保存在本地及服務(wù)器
這篇文章主要介紹了Android實(shí)現(xiàn)調(diào)用系統(tǒng)圖庫(kù)與相機(jī)設(shè)置頭像并保存在本地及服務(wù)器 ,需要的朋友可以參考下2017-03-03
詳解Android中Activity的啟動(dòng)模式及應(yīng)用場(chǎng)景
今天給大家介紹下安卓開(kāi)發(fā)中不得不涉及的Activity啟動(dòng)模式及應(yīng)用場(chǎng)景,Activity一共有四種啟動(dòng)模式,分別是Standard模式、SingleTop模式、SingleTask模式以及SingleInstance模式,,需要的朋友可以參考下2023-09-09
Android EditText 監(jiān)聽(tīng)用戶輸入完成的實(shí)例
下面小編就為大家分享一篇Android EditText 監(jiān)聽(tīng)用戶輸入完成的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
android 通知Notification詳解及實(shí)例代碼
這篇文章主要介紹了android 通知Notification詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-12-12
Android編程實(shí)現(xiàn)XML解析與保存的三種方法詳解
這篇文章主要介紹了Android編程實(shí)現(xiàn)XML解析與保存的三種方法,結(jié)合實(shí)例形式詳細(xì)分析了Android實(shí)現(xiàn)xml解析的SAX、DOM、PULL三種方法的相關(guān)操作技巧,需要的朋友可以參考下2017-08-08

