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
效果图:
权限