1. 程式人生 > >掃描+生成二維碼

掃描+生成二維碼

1.佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">
    <Button
        android:id="@+id/scan_qrcode"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="開啟掃描"/>
    <EditText
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="請輸入"/>
    <Button
        android:id="@+id/new_qrcode"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="生成二維碼"/>

    <ImageView
        android:id="@+id/mimage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"/>


</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<cn.bingoogolapple.qrcode.zxing.ZXingView
    android:id="@+id/zxingview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</LinearLayout>

2.主頁面

package com.bw.ymy.qrcode;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.lang.ref.WeakReference;

import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;

public class MainActivity extends AppCompatActivity {

    private ImageView mimage;
    private EditText met;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       initView();
    }

    private void initView() {
        //獲取資源id
        mimage=findViewById(R.id.mimage);
        met=findViewById(R.id.et);
        //點選開啟掃描二維碼
        findViewById(R.id.scan_qrcode).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                check();
            }
        });
            //點選生成二維碼
        findViewById(R.id.new_qrcode).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                newcreate();
            }
        });
    }
            //開啟掃描

    private void check() {
        //第一步,判斷系統版本是否為6.0以上
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            //第二步:checkSelfPermission判斷有沒有此許可權
            //第一個引數:上下文
            //第二個引數:我們想要判斷的許可權,此處為相機許可權
            //PackageManager.PERMISSION_GRANTED 條件,許可權有沒有被授予
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                //如果沒授予,則申請許可權
                //第一個:上下文
                //第二個:要申請的許可權陣列,此處為相機許可權
                //第三個:請求碼,startActivityForResult一樣
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},100);
            }else{
                startActivity(new Intent(MainActivity.this, ScanActivity.class));
            }
        }

    }
//開啟生成

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode==100&&grantResults[0]==PackageManager.PERMISSION_GRANTED)
        {
            startActivity(new Intent(MainActivity.this,ScanActivity.class));
        }
    }

    private  void newcreate()
    {
        QRTask qrTask=new QRTask(this,mimage,met);
        qrTask.execute(met.getText().toString());
    }
    static  class  QRTask extends AsyncTask<String,Void,Bitmap>
    {
        private WeakReference<Context> mContext;
        private WeakReference<ImageView> mimage;
        public  QRTask(Context context,ImageView image,EditText et)
        {
        mContext=new WeakReference<>(context);
        mimage=new WeakReference<>(image);
        }

        @Override
        protected Bitmap doInBackground(String... params) {
            String str= params[0];
            if(TextUtils.isEmpty(str))
            {
                return null;
            }
           int size=100;
            return QRCodeEncoder.syncEncodeQRCode(str,size);
        }
        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            if(bitmap!=null)
            {
                mimage.get().setImageBitmap(bitmap);
            }else
            {
                Toast.makeText(mContext.get(),"失敗",Toast.LENGTH_LONG).show();
            }
        }
    }


}

3.繼承

package com.bw.ymy.qrcode;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.ZXingView;

public class ScanActivity extends AppCompatActivity implements QRCodeView.Delegate {

    private ZXingView mzXingView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan);

        mzXingView=findViewById(R.id.zxingview);

        mzXingView.setDelegate(this);
    }

    //開啟相機
    @Override
    protected void onResume() {
        super.onResume();
        mzXingView.startCamera();
        mzXingView.startSpotAndShowRect();
        //mzXingView.openFlashlight();

    }

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

    //銷燬
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mzXingView.onDestroy();
    }

    //點選吐司
    @Override
    public void onScanQRCodeSuccess(String result) {

        Log.i("dj", "result is " + result);
        Toast.makeText(this,result+"",Toast.LENGTH_LONG).show();

    }

    @Override
    public void onCameraAmbientBrightnessChanged(boolean isDark) {

    }

    @Override
    public void onScanQRCodeOpenCameraError() {

    }

}

依賴:

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

 

 

ZXing詳細網址:

https://github.com/bingoogolapple/BGAQRCode-Android

 

效果圖:

許可權