Android二維碼的生成與掃碼-zxing示例代碼
由于GitHub上面的zxing功能太多,有的用不到,我就抽取了重要的出來使用,這個可以生成二維碼,掃描二維碼和相冊中的二維碼
Demo效果:

1.在project的build.gradle添加如下代碼:
allprojects {
repositories {
maven {
url 'https://jitpack.io'
}
}
}
2.在build.gradle添加依賴:
dependencies {
compile 'com.github.goodboy321:Scan-Zxing:1.0'
}
布局:
<EditText android:id="@+id/et" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="輸入內(nèi)容,生成二維碼" android:text="http://www.baidu.com" /> <Button android:background="@color/colorAccent" android:id="@+id/btn2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="生成二維碼" /> <Button android:layout_marginTop="10dp" android:background="@color/colorPrimaryDark" android:id="@+id/btn1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="掃碼(識別相冊中二維碼)" /> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" /> <ImageView android:id="@+id/image_callback" android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_horizontal" />
主方法:
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn1:
Intent intent = new Intent(mContext, CaptureActivity.class);
startActivityForResult(intent, REQUEST);
break;
case R.id.btn2:
image.setVisibility(View.VISIBLE);
//隱藏掃碼結(jié)果view
imageCallback.setVisibility(View.GONE);
String content = et.getText().toString().trim();
Bitmap bitmap = null;
try {
bitmap = BitmapUtils.create2DCode(content);//根據(jù)內(nèi)容生成二維碼
tvResult.setVisibility(View.GONE);
image.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST) {
image.setVisibility(View.GONE);
imageCallback.setVisibility(View.VISIBLE);
String result = data.getStringExtra(CaptureActivity.SCAN_QRCODE_RESULT);
Bitmap bitmap = data.getParcelableExtra(CaptureActivity.SCAN_QRCODE_BITMAP);
if(bitmap != null){
imageCallback.setImageBitmap(bitmap);//現(xiàn)實掃碼圖片
}
}
具體需求可修改源碼
Demo源碼下載:Zxing_jb51.net.rar
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 圖像處理(類型轉(zhuǎn)換,比例縮放,倒影,圓角)的小例子
Android 圖像處理(類型轉(zhuǎn)換,比例縮放,倒影,圓角)的小例子,需要的朋友可以參考一下2013-05-05
android跑馬燈出現(xiàn)重復(fù)跳動以及不滾動問題的解決方法
這篇文章主要介紹了android跑馬燈出現(xiàn)重復(fù)跳動以及不滾動問題的解決方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09
Android應(yīng)用開發(fā)中自定義ViewGroup的究極攻略
這里我們要演示的自定義ViewGroup中將實現(xiàn)多種方式排列和滑動等效果,并且涵蓋子View之間Touch Event的攔截與處理等問題,完全干貨,下面就為大家送上Android應(yīng)用開發(fā)中自定義ViewGroup的究極實例攻略2016-05-05
詳解Flutter自定義應(yīng)用程序內(nèi)鍵盤的實現(xiàn)方法
本文將展示如何利用Flutter創(chuàng)建自定義鍵盤小部件,用于在自己的應(yīng)用程序中的Flutter TextField中輸入文本,感興趣的小伙伴可以了解一下2022-06-06
Android ViewPager實現(xiàn)圖片輪播效果
這篇文章主要為大家詳細介紹了Android ViewPager實現(xiàn)圖片輪播效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09
android 實現(xiàn)類似微信緩存和即時更新好友頭像示例
本篇文章主要介紹了android 實現(xiàn)類似微信緩存和即時更新好友頭像示例,具有一定的參考價值,有興趣的可以了解一下。2017-01-01
Android中AlertDialog 點擊按鈕后不關(guān)閉對話框的功能
本篇文章主要介紹了Android中AlertDialog 點擊按鈕后不關(guān)閉對話框的功能,非常具有實用價值,需要的朋友可以參考下2017-04-04

