Android---快速二维码开发

这还是我的使用记录,原文看链接,这次使用的是SimpleZXing。
快速开发包括五步

1.添加项目依赖

compile 'com.acker:simplezxing:1.5'

2.添加权限


3.动态权限申请

这里要申请Manifest.permission.CAMERA权限。关于动态权限如何申请可以参考前面的文章,动态权限申请

4.调用二维码

调用startCaptureActivityForResult()

private void startCaptureActivityForResult() {
        Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
        Bundle bundle = new Bundle();
        bundle.putBoolean(CaptureActivity.KEY_NEED_BEEP, CaptureActivity.VALUE_BEEP);
        bundle.putBoolean(CaptureActivity.KEY_NEED_VIBRATION, CaptureActivity.VALUE_VIBRATION);
        bundle.putBoolean(CaptureActivity.KEY_NEED_EXPOSURE, CaptureActivity.VALUE_NO_EXPOSURE);
        bundle.putByte(CaptureActivity.KEY_FLASHLIGHT_MODE, CaptureActivity.VALUE_FLASHLIGHT_OFF);
        bundle.putByte(CaptureActivity.KEY_ORIENTATION_MODE, CaptureActivity.VALUE_ORIENTATION_AUTO);
        bundle.putBoolean(CaptureActivity.KEY_SCAN_AREA_FULL_SCREEN, CaptureActivity.VALUE_SCAN_AREA_FULL_SCREEN);
        bundle.putBoolean(CaptureActivity.KEY_NEED_SCAN_HINT_TEXT, CaptureActivity.VALUE_SCAN_HINT_TEXT);
        intent.putExtra(CaptureActivity.EXTRA_SETTING_BUNDLE, bundle);
        startActivityForResult(intent, CaptureActivity.REQ_CODE);
    }

5.接收回调,处理返回的信息

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case CaptureActivity.REQ_CODE:
                switch (resultCode) {
                    case RESULT_OK:
                        //do sth
                        //获取到字符串
                        //data.getStringExtra(CaptureActivity.EXTRA_SCAN_RESULT)
                        break;
                    case RESULT_CANCELED:
                        if (data != null) {
                            // for some reason camera is not working correctly
                        }
                        break;
                }
                break;
        }
    }

总结

总共就这么几步,原理什么都不需要知道。使用还是挺方便的。等以后需要改的时候再来看看实现,挖个坑给自己。

你可能感兴趣的:(Android---快速二维码开发)