第三方ZXing库zxing-android-embedded使用及自定义

一、关于ZXing

现在一维码二维码在我们的日常生活中使用如此的广泛,所以拥有扫码功能的APP变得非常普遍,一个安卓APP需要扫码功能就要用到zxing了,zxing是谷歌开源的让开发者更方便使用摄像头的库,而我们常用的扫码功能就是其中之一。

github地址:https://github.com/zxing/zxing

但是因为zxing的功能太强大了,包含了很多我们用不上的功能,所以一般都会抽取其中的扫码功能单独使用,这个抽取的过程还是有点麻烦的,但是已经有很多开发者为我们省去了这个过程,现在就来介绍一个很棒的第三方zxing库:zxing-android-embedded

二、第三方zxing库zxing-android-embedded

github地址:https://github.com/journeyapps/zxing-android-embedded

使用方式也很简单,首先在gradle中添加依赖

compile 'com.journeyapps:zxing-android-embedded:3.5.0'

然后在Activity或fragment中调用即可:

new IntentIntegrator(this)
        .setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES)// 扫码的类型,可选:一维码,二维码,一/二维码
        .setPrompt("请对准二维码")// 设置提示语
        .setCameraId(0)// 选择摄像头,可使用前置或者后置
        .setBeepEnabled(false)// 是否开启声音,扫完码之后会"哔"的一声
        .setBarcodeImageEnabled(true)// 扫完码之后生成二维码的图片
        .initiateScan();// 初始化扫码

扫完码之后会在onActivityResult方法中回调结果

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if(result != null) {
        if(result.getContents() == null) {
            Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

以上就是简单的使用方式了

三、自定义界面

我们开发的时候可能会想要自定义界面,那么就需要我们修改代码了,自定义界面可以实现的:
1. 默认的的扫码是横屏的,自定义后可以竖屏扫码
2. 修改扫码框布局,可以添加其他View

其实在设置IntentIntegrator时会有一个方法:setCaptureActivity(Activity),这个方法就是用来设置扫码界面的Activity,当不设置的时候会默认调用库作者自己写的CaptureActivity,我们可以一起看一下这个Activity里面做了什么:

public class CaptureActivity extends Activity {
   
    private CaptureManager capture;
    private DecoratedBarcodeView barcodeScannerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        barcodeScannerView = initializeContent();

        capture = new CaptureManager(this, barcodeScannerView);
        capture.initializeFromIntent(getIntent(), savedInstanceState);
        capture.decode();
    }

    ...省略代码
}

这里有2个很重要的成员变量:CaptureManager和DecoratedBarcodeView,从他们的名字可以看出:
1. CaptureManager是用来拉起扫码和处理扫码结果的类
2. DecoratedBarcodeView则是一个显示扫码界面的自定义View

有了这个了解之后我们要自定义扫码界面就很明了了,再一起简单看一下DecoratedBarcodeView是个啥:

public class DecoratedBarcodeView extends FrameLayout {
   
    private BarcodeView barcodeView;
    private ViewfinderView viewFinder;
    private TextView statusView;

    ...省略代码
}
  1. BarcodeView就是背景
  2. ViewfinderView就是扫描框
  3. TextView为下方提示文字

上个图:
这里写图片描述

了解了这三个View的作用之后我们就可以开始我们的自定义了,而这三个View具体怎么去扫码与解析并不是我们关心的重点我们直接跳过。自定义界面的步骤:

  1. 新建一个Activity
  2. 把CaptureManager和DecoratedBarcodeView复制到我们自定义的Activity中
  3. 设置setCaptureActivity(CustomCaptureActivity.class)为我们自己的Activity
  4. 别忘了把自定义的Activity加入到AndroidManifest.xml中注册

上代码,我自定义的activity:

public class CustomCaptureActivity extends Activity {
   
    private CaptureManager capture;
    private DecoratedBarcodeView barcodeScannerV
  • 16
    点赞
  • 61
    收藏
    觉得还不错? 一键收藏
  • 21
    评论
Android’s growth is phenomenal. In a very short time span, it has succeeded in becoming one of the top mobile platforms in the market. Clearly, the unique combination of open source licensing, aggressive go-to-market, and trendy interface is bearing fruit for Google’s Android team. Needless to say, the massive user uptake generated by Android has not gone unnoticed by handset manufacturers, mobile network operators, silicon manufacturers, and app developers. Products, apps, and devices “for,” “compatible with,” or “based on” Android seem to be coming out ever so fast. Beyond its mobile success, however, Android is also attracting the attention of yet another, unintended crowd: embedded systems developers. While a large number of embedded devices have little to no human interface, a substantial number of devices that would traditionally be considered “embedded” do have user interfaces. For a goodly number of modern machines, in addition to pure technical functionality, developers creating user-facing devices must also contend with human-computer interaction (HCI) factors. Therefore, designers must either present users with an experience they are already familiar with or risk alienating users by requiring them to learn a lesserknown or entirely new user experience. Before Android, the user interface choices available to the developers of such devices were fairly limited and limiting. Clearly, embedded developers prefer to offer users an interface they are already familiar with. Although that interface might have been window-based in the past—and hence a lot of embedded devices were based on classic window-centric, desktop-like, or desktopbased interfaces—Apple’s iOS and Google’s Android have forever democratized the use of touch-based, iPhone-like graphical interfaces. This shift in user paradigms and expectations, combined with Android’s open source licensing, have created a groundswell of interest about Android within the embedded world.
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值