生成二维码和扫描二维码
1.依赖:
implementation 'cn.yipianfengye.android:zxing-library:2.2'
2.权限:
<!--网络权限-->
<uses-permission android:name="android.permission.INTERNET"/>
<!--震动权限-->
<uses-permission android:name="android.permission.VIBRATE"/>
<!--摄像头权限-->
<uses-permission android:name="android.permission.CAMERA"/>
<!--自动聚焦权限-->
<uses-feature android:name="android.hardware.camera.autofocus"/>
3.动态权限:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.CAMERA,Manifest.permission.VIBRATE,Manifest.permission.WRITE_EXTERNAL_STORAGE}, 200);
}
4.application初始化ZXing:
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
//初始化Zxing库
ZXingLibrary.initDisplayOpinion(this);
}
}
5.清单文件:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ce03b045d71da329f389b0026502f765.png#pic_center)
6.生成二维码:
//生成二维码
private void create() {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.love);
Bitmap code = CodeUtils.createImage("安卓3哈哈哈", 400, 400, bitmap);
iv.setImageBitmap(code);
}
7.扫描二维码:
//扫描二维码
private void sao() {
Intent intent = new Intent(this, CaptureActivity.class);
startActivityForResult(intent,101);
}
//处理结果
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 101 && resultCode == Activity.RESULT_OK &&data != null){
Bundle bundle = data.getExtras();
if(bundle == null){
return;
}
if(bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS){
String string = bundle.getString(CodeUtils.RESULT_STRING);
Toast.makeText(this, "扫描结果"+string, Toast.LENGTH_SHORT).show();
}
}
}
全
package com.example.android3_demo.zxing;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.example.android3_demo.R;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
/**
1。倒入依赖: implementation 'cn.yipianfengye.android:zxing-library:2.2'
2。权限:
<!--网络权限-->
<uses-permission android:name="android.permission.INTERNET"/>
<!--震动权限-->
<uses-permission android:name="android.permission.VIBRATE"/>
<!--摄像头权限-->
<uses-permission android:name="android.permission.CAMERA"/>
<!--自动聚焦权限-->
<uses-feature android:name="android.hardware.camera.autofocus"/>
3.生成二维码
*/
public class ZXingActivity extends AppCompatActivity implements View.OnClickListener {
private Button bt_create;
private Button bt_sao;
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zxing);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.CAMERA,Manifest.permission.VIBRATE,Manifest.permission.WRITE_EXTERNAL_STORAGE}, 200);
}
initView();
}
private void initView() {
bt_create = (Button) findViewById(R.id.bt_create);
bt_sao = (Button) findViewById(R.id.bt_sao);
iv = (ImageView) findViewById(R.id.iv);
bt_create.setOnClickListener(this);
bt_sao.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_create:
create();
break;
case R.id.bt_sao:
sao();
break;
}
}
//扫描二维码
private void sao() {
Intent intent = new Intent(this, CaptureActivity.class);
startActivityForResult(intent,101);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 101 && resultCode == Activity.RESULT_OK &&data != null){
Bundle bundle = data.getExtras();//获得扫描的数据
if(bundle == null){
return;
}
if(bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS){//判断是否成功
String string = bundle.getString(CodeUtils.RESULT_STRING);//获得二维码字符串
Toast.makeText(this, "扫描结果"+string, Toast.LENGTH_SHORT).show();
}
}
}
//生成二维码
private void create() {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.love);
Bitmap code = CodeUtils.createImage("安卓3哈哈哈", 400, 400, bitmap);
iv.setImageBitmap(code);
}
}
效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b45f1db6925d89642e8b7ba3af3a3629.png#pic_center)