国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android實現二維碼掃描和生成的簡單方法

2019-12-12 05:53:23
字體:
來源:轉載
供稿:網友

這里簡單介紹一下ZXing庫。ZXing是一個開放源碼的,用Java實現的多種格式的1D/2D條碼圖像處理庫,它包含了聯系到其他語言的端口。Zxing可以實現使用手機的內置的攝像頭完成條形碼的掃描及解碼。該項目可實現的條形碼編碼和解碼。目前支持以下格式:UPC-A,UPC-E、EAN-8,EAN-13、39碼、93碼。ZXing是個很經典的條碼/二維碼識別的開源類庫,以前在功能機上,就有開發者使用J2ME運用ZXing了,不過要支持JSR-234規范(自動對焦)的手機才能發揮其威力。

ZXingGitHub地址

效果圖:

這里寫圖片描述

主要實現步驟:

導入libzxing這個模塊

這里寫圖片描述

ZXing源代碼很大,功能也很多,這里只是抽取了其中的一部分代碼整合到了一起

掃描

在main_activity中添加一個Button和一個TextView 點擊Button后開始調照相機功能,掃描二維碼
TextView會顯示掃描后的結果

<Buttonandroid:text="Strat Scan"android:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="scan"/><TextViewandroid:id="@+id/tv_showResult"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello World!"/>

在ActivityMain中分別初始化這兩個控件

private TextView mTextView;mTextView= (TextView) this.findViewById(R.id.tv_showResult);//掃描二維碼//https://cli.im/text?2dd0d2b267ea882d797f03abf5b97d88二維碼生成網站public void scan(View view) {startActivityForResult(new Intent(this, CaptureActivity.class),0);}@Overrideprotected 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文件都貼出來把,加上之前的掃描

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns: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"><Buttonandroid:text="Strat Scan"android:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="scan"/><TextViewandroid:id="@+id/tv_showResult"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello World!"/><EditTextandroid:id="@+id/et_text"android:hint="Imput"android:layout_width="match_parent"android:layout_height="wrap_content"/><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="make"android:text="Make QRCode"/><CheckBoxandroid:id="@+id/cb_logo"android:layout_width="wrap_content"android:text="Logo"android:layout_height="wrap_content"/><ImageViewandroid: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;@Overrideprotected 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二維碼生成網站public void scan(View view) {startActivityForResult(new Intent(this, CaptureActivity.class),0);}@Overrideprotected 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);}}}//生成二維碼 可以設置Logopublic 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選中就設置LogomImageView.setImageBitmap(qrCode);}}}

好了 到這里就寫完了

項目地址

以上所述是小編給大家介紹的Android實現二維碼掃描和生成的簡單方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 辰溪县| 沈阳市| 双峰县| 东宁县| 海淀区| 灵山县| 得荣县| 西盟| 文成县| 峨眉山市| 南康市| 龙门县| 拜城县| 莫力| 滁州市| 静安区| 高阳县| 平度市| 延寿县| 长汀县| 刚察县| 石林| 柳州市| 博野县| 遂溪县| 新龙县| 呼图壁县| 界首市| 凌源市| 长兴县| 晋州市| 灵丘县| 新沂市| 将乐县| 阿拉善左旗| 常山县| 南川市| 格尔木市| 吐鲁番市| 明水县| 长宁区|