1. 程式人生 > >二維碼掃描 ZXing 基礎

二維碼掃描 ZXing 基礎

一、新增依賴

	implementation 'cn.bingoogolapple:bga-qrcode-zxing:1.2.1'

二、佈局

	<?xml version="1.0" encoding="utf-8"?>
	<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
	    xmlns:app="http://schemas.android.com/apk/res-auto"
	    xmlns:tools="http://schemas.android.com/tools"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    tools:context=".MainActivity">
	
	    <cn.bingoogolapple.qrcode.zxing.ZXingView
	        android:id="@+id/zxingview"
	        app:qrcv_animTime="1000"
	        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_maskColor="#33FFFFFF"
	        app:qrcv_rectWidth="200dp"
	        app:qrcv_scanLineColor="@color/colorPrimaryDark"
	        app:qrcv_scanLineSize="1dp"
	        app:qrcv_topOffset="90dp"
	        android:layout_height="wrap_content"
	        android:layout_width="wrap_content" />
	
	</android.support.constraint.ConstraintLayout>	

三、Main程式碼

		package com.gj.qrcodezxing;
	
		import android.Manifest;
		import android.content.pm.PackageManager;
		import android.support.annotation.NonNull;
		import android.support.v4.app.ActivityCompat;
		import android.support.v7.app.AppCompatActivity;
		import android.os.Bundle;
		import android.view.View;
		import android.widget.Toast;
		
		import cn.bingoogolapple.qrcode.core.QRCodeView;
		import cn.bingoogolapple.qrcode.zxing.ZXingView;
		
		public class MainActivity extends AppCompatActivity implements QRCodeView.Delegate {
		
		    private ZXingView mZxingview;
		
		    @Override
		    protected void onCreate(Bundle savedInstanceState) {
		        super.onCreate(savedInstanceState);
		        setContentView(R.layout.activity_main);
		        initView();
		    }
		
		    private void initView() {
		        mZxingview = findViewById(R.id.zxingview);
		        mZxingview.setDelegate(this);
		
		        //動態許可權
		        String[] p = new String[]{Manifest.permission.CAMERA};
		        ActivityCompat.requestPermissions(MainActivity.this,p,1);
		    }
		
		
		    @Override
		    public void onScanQRCodeSuccess(String result) {
		        Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
		    }
		
		
		    @Override
		    public void onScanQRCodeOpenCameraError() {
		
		    }
		
		    @Override
		    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
		        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
		        if(grantResults.length>0){
		            int result = grantResults[0];
		            if(result==PackageManager.PERMISSION_GRANTED){
		                //許可權同意
		                mZxingview.startSpot();
		            }else{
		                //許可權拒絕
		                Toast.makeText(MainActivity.this, "請同意開啟攝像頭許可權", Toast.LENGTH_SHORT).show();
		            }
		        }
		    }
		}

四、注意加許可權

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