Android - BGAQRCode 扫描二维码生成二维码

时间:2022-11-17 08:50:35

说明:

       最新有二维码扫描需求,就研究了一下二维码扫描:找了很多项目目前觉得这个还是蛮好用的:https://github.com/bingoogolapple/BGAQRCode-Android

       这里只是用到了ZXingDemo的代码二维码扫描功能


效果图:

        Android - BGAQRCode 扫描二维码生成二维码


自己的代码:

        具体看git的Demo代码,下面是自己写的扫描二维码的代码:

1.gargle引入:

   //二维码扫描
    compile 'com.google.zxing:core:3.2.1'
    compile 'cn.bingoogolapple:bga-qrcodecore:1.1.3@aar'
    compile 'cn.bingoogolapple:bga-zxing:1.1.3@aar'

2.扫码类:

package briefer.pet.ui.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import briefer.pet.R;
import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.ZXingView;

/**
 * 扫码
 * Created by zst on 2017/6/6.
 */

public class ScanCodeActivity extends Activity implements QRCodeView.Delegate {
    private static final int REQUEST_CODE_CAMERA = 999;
    private static final String TAG = ScanCodeActivity.class.getSimpleName();

    private QRCodeView mQRCodeView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_code);

        mQRCodeView = (ZXingView) findViewById(R.id.zxingview);
        mQRCodeView.setDelegate(this);

        Log.e(TAG, "扫码:01");
    }

    @Override
    protected void onStart() {
        super.onStart();
        mQRCodeView.startCamera();
//        mQRCodeView.startCamera(Camera.CameraInfo.CAMERA_FACING_FRONT);
        mQRCodeView.startSpot();

        Log.e(TAG, "扫码:02");
    }

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

        Log.e(TAG, "扫码:03");
    }

    @Override
    protected void onDestroy() {
        mQRCodeView.onDestroy();
        super.onDestroy();
        Log.e(TAG, "扫码:04");
    }

    //震动器
    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(100);

        Log.e(TAG, "扫码:05");
    }

    @Override
    public void onScanQRCodeSuccess(String result) {
        Log.i(TAG, "result:" + result);
        //Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
        vibrate();//震动
        mQRCodeView.stopSpot();

        if (!TextUtils.isEmpty(result)) {
            mQRCodeView.stopCamera();
            mQRCodeView.onDestroy();

            Intent intent = new Intent(ScanCodeActivity.this, ServiceManagerDianZhuNewActivity.class);
            intent.putExtra("url", result);
            //intent.setData(Uri.parse(result));
            startActivity(intent);
            finish();
        } else {
            Toast.makeText(this, "链接无效,请重新扫描", Toast.LENGTH_SHORT).show();
            mQRCodeView.startSpot();
        }
        Log.e(TAG, "扫码:06");
    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        Log.e(TAG, "无相机权限,打开相机出错");

        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.CAMERA}, REQUEST_CODE_CAMERA);
        Log.e(TAG, "扫码:07");
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start_spot:
                mQRCodeView.startSpot();
                break;
            case R.id.stop_spot:
                mQRCodeView.stopSpot();
                break;
            case R.id.open_flashlight:
                mQRCodeView.openFlashlight();
                break;
            case R.id.close_flashlight:
                mQRCodeView.closeFlashlight();
                break;
            case R.id.rl_back:
                //onDestroy();
                mQRCodeView.stopCamera();
                mQRCodeView.onDestroy();
                finish();
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == REQUEST_CODE_CAMERA) {
            mQRCodeView.startCamera();
            mQRCodeView.startSpot();
        }
    }
}

3.布局

activity_scan_code.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout style="@style/MatchMatch"
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto">

    <cn.bingoogolapple.qrcode.zxing.ZXingView
        android:id="@+id/zxingview"
        style="@style/MatchMatch"
        app:qrcv_animTime="2000"
        app:qrcv_barCodeTipText="将条码放入框内,即可自动扫描"
        app:qrcv_borderColor="@android:color/white"
        app:qrcv_borderSize="1dp"
        app:qrcv_cornerColor="#f57257"
        app:qrcv_cornerLength="20dp"
        app:qrcv_cornerSize="3dp"
        app:qrcv_isBarcode="false"
        app:qrcv_isOnlyDecodeScanBoxArea="true"
        app:qrcv_isScanLineReverse="true"
        app:qrcv_isShowDefaultGridScanLineDrawable="true"
        app:qrcv_isShowDefaultScanLineDrawable="false"
        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="#f57257"
        app:qrcv_scanLineMargin="0dp"
        app:qrcv_scanLineSize="1dp"
        app:qrcv_tipTextColor="@android:color/white"
        app:qrcv_tipTextSize="12sp"
        app:qrcv_toolbarHeight="0dp"
        app:qrcv_topOffset="180dp"/>

    <LinearLayout
        style="@style/MatchWrap.Vertical"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="50dp">

        <LinearLayout style="@style/MatchWrap.Horizontal">

            <TextView
                android:id="@+id/start_spot"
                style="@style/AutoWrapTv"
                android:text="开始识别"
                android:visibility="gone"/>

            <TextView
                android:id="@+id/stop_spot"
                style="@style/AutoWrapTv"
                android:text="暂停识别"
                android:visibility="gone"/>

            <TextView
                android:id="@+id/open_flashlight"
                style="@style/AutoWrapTv"
                android:text="开灯"/>

            <TextView
                android:id="@+id/close_flashlight"
                style="@style/AutoWrapTv"
                android:text="关灯"/>

        </LinearLayout>

    </LinearLayout>

    <RelativeLayout
        android:id="@+id/rl_back"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginTop="30dp"
        android:onClick="onClick"
        android:layout_marginLeft="10dp">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_back"/>
    </RelativeLayout>
</RelativeLayout>

4.Style.xml

<!-- 扫描样式 -->
    <style name="MatchMatch">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">match_parent</item>
    </style>

    <style name="WrapMatch">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">match_parent</item>
    </style>

    <style name="MatchWrap">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
    </style>

    <style name="WrapMatch.Vertical">
        <item name="android:orientation">vertical</item>
    </style>

    <style name="MatchWrap.Vertical">
        <item name="android:orientation">vertical</item>
    </style>

    <style name="MatchWrap.Horizontal">
        <item name="android:orientation">horizontal</item>
    </style>

    <style name="AutoWrap">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_weight">1</item>
        <item name="android:layout_height">wrap_content</item>
    </style>

    <style name="AutoWrapTv" parent="AutoWrap">
        <item name="android:textSize">12sp</item>
        <item name="android:textColor">@android:color/white</item>
        <item name="android:gravity">center</item>
        <item name="android:background">@drawable/selector_btn_orange</item>
        <item name="android:layout_margin">45dp</item>
        <item name="android:onClick">onClick</item>
        <item name="android:paddingLeft">4dp</item>
        <item name="android:paddingRight">4dp</item>
        <item name="android:paddingTop">8dp</item>
        <item name="android:paddingBottom">8dp</item>
    </style>

5.selector_btn_orange.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="#CCf57257" />
            <corners android:radius="5dp" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#f57257" />
            <corners android:radius="5dp" />
        </shape>
    </item>

</selector>

6.权限

<!-- 扫码 -->
<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" />

遇到的问题:

1.使用gardle导入jar包的时候,

compile 'cn.bingoogolapple:bga-qrcodecore:1.1.3@aar'
1.1.3的版本号一定要输入对,否则导入不成功。

2.Demo中没有考虑那么全对于摄像头打开和关闭,这里需要自己打Log来测试一下扫码的顺序,这样好开发。

3.Demo中是按钮启动扫码的,不会打开Activity就会扫码的,所以自己打开Activity的时候要自己启动一下扫码:

mQRCodeView.startSpot();