BGAQRCode-Android的简单使用

[url]https://github.com/bingoogolapple/BGAQRCode-Android

依赖文件(demo中用到的有些可根据需求改变)

    compile 'com.google.zxing:core:3.2.1'
    compile 'cn.bingoogolapple:bga-qrcodecore:1.1.7@aar'
    compile 'cn.bingoogolapple:bga-zxing:1.1.7@aar'

根据需求加入对应的权限(动态获取权限请根据easyPermission文档操作)

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    //这句是使用图片选择器框架时用到的
    xmlns:tools="http://schemas.android.com/tools"
    package="com.huaqiang.zxingdemo">
    //图片选择框架用到
    <uses-sdk tools:overrideLibrary="cn.bingoogolapple.photopicker,cn.bingoogolapple.androidcommon.adapter" />

    <uses-permission android:name="android.permission.CAMERA" />
    
    <uses-permission android:name="android.permission.VIBRATE" />
    
    <uses-permission android:name="android.permission.FLASHLIGHT" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

布局中使用


    <cn.bingoogolapple.qrcode.zxing.ZXingView
        android:id="@+id/zxingview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:qrcv_animTime="1000"
        app:qrcv_barCodeTipText="将条码放入框内,即可自动扫描"
        app:qrcv_barcodeRectHeight="140dp"
        app:qrcv_borderColor="@android:color/white"
        app:qrcv_borderSize="1dp"
        app:qrcv_cornerColor="@color/colorPrimaryDark"
        app:qrcv_cornerLength="20dp"
        app:qrcv_cornerSize="3dp"
        app:qrcv_customScanLineDrawable="@drawable/scan_icon_scanline"
        app:qrcv_isBarcode="false"
        app:qrcv_isCenterVertical="false"
        app:qrcv_isOnlyDecodeScanBoxArea="false"
        app:qrcv_isScanLineReverse="true"
        app:qrcv_isShowDefaultGridScanLineDrawable="false"
        app:qrcv_isShowDefaultScanLineDrawable="true"
        app:qrcv_isShowTipBackground="true"
        app:qrcv_isShowTipTextAsSingleLine="false"
        app:qrcv_isTipTextBelowRect="false"
        app:qrcv_maskColor="#33FFFFFF"
        app:qrcv_qrCodeTipText="将二维码/条码放入框内,即可自动扫描"
        app:qrcv_rectWidth="200dp"
        app:qrcv_scanLineColor="@color/colorPrimaryDark"
        app:qrcv_scanLineMargin="0dp"
        app:qrcv_scanLineSize="0.5dp"
        app:qrcv_tipTextColor="@android:color/white"
        app:qrcv_tipTextSize="12sp"
        app:qrcv_toolbarHeight="56dp"
        app:qrcv_topOffset="90dp"/>

代码中应用实现QRCodeView.Delegate接口

public class ScannerActivity extends AppCompatActivity implements QRCodeView.Delegate {

    @BindView(R.id.zxingview)
    ZXingView zxingview;
    @BindView(R.id.scanner)
    ImageView scanner;
    @BindView(R.id.btn_photo)
    Button btnPhoto;

    private static final int REQUEST_CODE_CHOOSE_QRCODE_FROM_GALLERY = 666;
    @BindView(R.id.btn_openligtht)
    Button btnOpenligtht;
    @BindView(R.id.btn_closeligtht)
    Button btnCloseligtht;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.btn_openBarcode)
    Button btnOpenBarcode;
    @BindView(R.id.btn_openQRcode)
    Button btnOpenQRcode;
    @BindView(R.id.activity_scanner)
    RelativeLayout activityScanner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scanner);
        ButterKnife.bind(this);
        initView();

    }

    @Override
    protected void onStart() {
        super.onStart();
        //打开后置摄像头预览,但并没有开始扫描
        zxingview.startCamera();
        //开启扫描框
        zxingview.showScanRect();
        zxingview.startSpot();
    }

    @Override
    protected void onStop() {
        zxingview.stopCamera();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        zxingview.onDestroy();
        super.onDestroy();
    }

    private void initView() {
        //隐藏toolbar上面的二维码图标
        scanner.setVisibility(View.GONE);
        btnPhoto.setVisibility(View.VISIBLE);
        //设置二维码的代理
        zxingview.setDelegate(this);
    }
    //扫描成功解析二维码成功后调用,可做对应的操作
    @Override
    public void onScanQRCodeSuccess(String result) {
        //扫描成功后调用震动器
        vibrator();
        //显示扫描结果
        Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
        //再次延时1.5秒后启动
        zxingview.startSpot();
    }

    private void vibrator() {
        //获取系统震动服务
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

    //扫描失败后调用的方法
    @Override
    public void onScanQRCodeOpenCameraError() {
        Toast.makeText(this, "相机打开失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        zxingview.showScanRect();
        if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE_CHOOSE_QRCODE_FROM_GALLERY) {
            final String picturePath = BGAPhotoPickerActivity.getSelectedImages(data).get(0);

            /*
            这里为了偷懒,就没有处理匿名 AsyncTask 内部类导致 Activity 泄漏的问题
            请开发在使用时自行处理匿名内部类导致Activity内存泄漏的问题,处理方式可参考 https://github.com/GeniusVJR/LearningNotes/blob/master/Part1/Android/Android%E5%86%85%E5%AD%98%E6%B3%84%E6%BC%8F%E6%80%BB%E7%BB%93.md
             */
            new AsyncTask() {
                @Override
                protected String doInBackground(Void... params) {
                    return QRCodeDecoder.syncDecodeQRCode(picturePath);
                }

                @Override
                protected void onPostExecute(String result) {
                    if (TextUtils.isEmpty(result)) {
                        Toast.makeText(ScannerActivity.this, "未发现二维码", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(ScannerActivity.this, result, Toast.LENGTH_SHORT).show();
                    }
                }
            }.execute();
        }
    }


    @OnClick({R.id.btn_openligtht, R.id.btn_closeligtht, R.id.btn_photo,R.id.btn_openBarcode, R.id.btn_openQRcode})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_openligtht:
                //打开闪关灯
                zxingview.openFlashlight();
                break;
            case R.id.btn_closeligtht:
                //关闭闪光灯
                zxingview.closeFlashlight();
                break;
            case R.id.btn_photo:
                //参数1 应用程序上下文
                //参数2 拍照后图片保存的目录。如果传null表示没有拍照功能,如果不为null则具有拍照功能,
                //参数3 图片选择张数的最大值
                //参数4 当前已选中的图片路径集合,可以传null
                //参数5 滚动列表时是否暂停加载图片
                startActivityForResult(BGAPhotoPickerActivity.newIntent(this, null, 1, null, false), REQUEST_CODE_CHOOSE_QRCODE_FROM_GALLERY);
                break;
            case R.id.btn_openBarcode:
                //切换到条形码扫描
                zxingview.changeToScanBarcodeStyle();
                break;
            case R.id.btn_openQRcode:
                //切换到二维码扫描
                zxingview.changeToScanQRCodeStyle();
                break;

        }
    }

}

你可能感兴趣的:(android)