zxing-android-embedded的使用及自定义样式仿微信的
2016-11-25 08:56
323 查看
前言
最近项目要添加扫描二维码,之前也做过效果不是很好(预览变形,识别率太差)所以这次考虑要解决这些bug,经过一番百度找到一个比较好用的第三方库zxing-android-embedded(因为自己水平太菜,自己写还是算了)
项目地址:https://github.com/journeyapps/zxing-android-embedded
效果图如下:
备注:
由于项目原始的UI,跟我想要的还不一样,所以这里看到的已经是被修改过的,仿微信的
经过测试效果很好,所以决定使用一下
使用步骤如下:
1.配置项目依赖
在module的builde.gradle 添加依赖包compile 'com.google.zxing:core:3.2.1'
compile(name: 'zxing-android-embedded-release', ext: 'aar')
dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.0.0' compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha6' compile 'com.android.support:support-v4:25.0.1' testCompile 'junit:junit:4.12' compile 'com.google.zxing:core:3.2.1' compile(name: 'zxing-android-embedded-release', ext: 'aar') compile 'cn.jiguang:jpush:2.2.0' }
2.添加预览的activity
布局如下:<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" > <com.journeyapps.barcodescanner.DecoratedBarcodeView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/dbv_custom" app:zxing_preview_scaling_strategy="fitXY" app:zxing_use_texture_view="true" app:zxing_frame_color="@android:color/white"> </com.journeyapps.barcodescanner.DecoratedBarcodeView> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#202020" android:id="@+id/action_bar_id" android:layout_weight="0"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center" android:layout_centerVertical="true" android:id="@+id/back_btn_id"> <ImageView android:id="@+id/main_person_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:paddingLeft="12dp" android:paddingTop="14dp" android:paddingBottom="14dp" android:paddingRight="4dp" android:src="@drawable/ico_back" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/white" android:text="返回"/> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:id="@+id/main_title_id" android:text="扫描二维码" android:textColor="@android:color/white" android:textSize="18sp" android:textStyle="bold"/> </RelativeLayout> </RelativeLayout> </FrameLayout>
代码如下:
import android.app.Activity; import android.os.Bundle; import android.os.PersistableBundle; import android.view.KeyEvent; import com.journeyapps.barcodescanner.CaptureManager; import com.journeyapps.barcodescanner.DecoratedBarcodeView; public class MipcaActivityCapture extends Activity implements DecoratedBarcodeView.TorchListener { private CaptureManager captureManager; private DecoratedBarcodeView mDBV; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_capture); mDBV = (DecoratedBarcodeView) findViewById(R.id.dbv_custom); mDBV.setTorchListener(this); //重要代码,初始化捕获 captureManager = new CaptureManager(this, mDBV); captureManager.initializeFromIntent(getIntent(),savedInstanceState); captureManager.decode(); } @Override protected void onPause() { super.onPause(); captureManager.onPause(); } @Override protected void onResume() { super.onResume(); captureManager.onResume(); } @Override protected void onDestroy() { super.onDestroy(); captureManager.onDestroy(); } @Override public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { super.onSaveInstanceState(outState, outPersistentState); captureManager.onSaveInstanceState(outState); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { return mDBV.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event); } @Override public void onTorchOn() { } @Override public void onTorchOff() { } }
3.配置启动和回调
启动代码:new IntentIntegrator(context) .setOrientationLocked(false) .setCaptureActivity(MipcaActivityCapture.class) // 设置自定义的activity是CustomActivity .initiateScan(); // 初始化扫描
这里设置很多参数,具体可以看github上面的文档
回调代码:
// 通过 onActivityResult的方法获取 扫描回来的 值 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode,resultCode,data); if(intentResult != null) { if(intentResult.getContents() == null) { Toast.makeText(this,"内容为空",Toast.LENGTH_LONG).show(); } else { // ScanResult 为 获取到的字符串 String ScanResult = intentResult.getContents(); Toast.makeText(this, ScanResult,Toast.LENGTH_LONG).show(); } } else { super.onActivityResult(requestCode,resultCode,data); } }
代码下载:http://download.csdn.net/detail/hu285279904/9754405
相关文章推荐
- 第三方ZXing库zxing-android-embedded使用及自定义
- Android:使用自定义透明Dialog样式的Activity
- Android自定义View时使用TypedArray配置样式属性
- [Android Exercise]仿微信游戏界面PART.2—自定义样式shape和selector的应用
- Android中使用自定义selector来实现view的点击效果以及view的显示样式
- android AlertBuilder创建的dialog,使用自定义样式
- Android自定义图片选取器,类似微信样式,自带裁剪功能,适配Android7.0
- android SeekBar自定义样式滑动条的使用
- android的Spinner控件的自定义样式设置以及ArrayAdapter的使用
- Android 通知栏Notification,NotificationManager,RemoteViews使用流程,自定义通知栏样式简介
- 使用 ZXing Android Embedded 实现二维码扫码
- android使用xml自定义button样式
- android UI学习 -- 设置界面的布局(包括style的使用,selector的使用,Checkbox自定义样式,菜单项的样式)
- android UI学习 -- 设置界面的布局(包括style的使用,selector的使用,Checkbox自定义样式,菜单项的样式)
- 自定义android EditText样式及使用方式
- Android实现可使用自定义透明Dialog样式的Activity完整实例
- Android使用zxing-android-embedded(由zxing开发)实现二维码生成和扫描
- android SeekBar自定义样式滑动条的使用
- Android Appcompat样式使用自定义Dialog
- Android中通知的简单使用和自定义通知样式