https://github.com/jenly1314/ZXingLite
ZXingLite for Android 是ZXing的精简版,基于ZXing库优化扫码和生成二维码/条形码功能,扫码界面完全支持自定义,也可一行代码使用默认实现的扫码功能。总之你想要的都在这里。
<dependency>
<groupId>com.king.zxing</groupId>
<artifactId>zxing-lite</artifactId>
<version>1.1.1</version>
<type>pom</type>
</dependency>
implementation ‘com.king.zxing:zxing-lite:1.1.1‘
<dependency org=‘com.king.zxing‘ name=‘zxing-lite‘ rev=‘1.1.1‘>
<artifact name=‘$AID‘ ext=‘pom‘></artifact>
</dependency>
allprojects {
repositories {
maven { url ‘https://dl.bintray.com/jenly/maven‘ }
}
}
compileOnly ‘com.android.support:appcompat-v7:28.0.0‘
api ‘com.google.zxing:core:3.3.3‘
布局示例 (可自定义布局,布局内至少要保证有SurfaceView和ViewfinderView,控件id可根据重写CaptureActivity 的 getPreviewViewId 和 getViewFinderViewId方法自定义)
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.king.zxing.ViewfinderView
android:id="@+id/viewfinderView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
代码示例 (二维码/条形码)
//跳转的默认扫码界面
startActivityForResult(new Intent(context,CaptureActivity.class),requestCode);
//生成二维码
CodeUtils.createQRCode(content,600,logo);
//生成条形码
CodeUtils.createBarCode(content, BarcodeFormat.CODE_128,800,200);
如果直接使用CaptureActivity需在您项目的AndroidManifest中添加如下配置
<activity
android:name="com.king.zxing.CaptureActivity"
android:screenOrientation="portrait"/>
1、直接使用CaptureActivity或者CaptureFragment。(纯洁的扫码,无任何添加剂)
2、通过继承CaptureActivity或者CaptureFragment并自定义布局。(适用于大多场景,并无需关心扫码相关逻辑)
3、在你项目的Activity或者Fragment中创建创建一个CaptureHelper并在相应的生命周期中调用CaptureHelper的周期。(适用于想在扫码界面写交互逻辑,又因为项目架构或其它原因,无法直接或间接继承CaptureActivity或CaptureFragment时使用)
4、参照CaptureHelper写一个自定义的扫码帮助类,其它步骤同方式3。(扩展高级用法,谨慎使用)
更多使用详情,请查看app中的源码使用示例
原文:https://www.cnblogs.com/zhangqingquan/p/10956879.html