Zxing来实现二维码扫描和生成(PS:超级简单)

首先先要导入第三方库的依赖在build.gradle导入

implementation'cn.yipianfengye.android:zxing-library:2.2'

需要在清单文件中写入权限


    
    
    
    

在XML写布局

 

在主页面中写出功能

也可以用黄油刀省去findviewbyid
//butterknife快速查找资源的依赖
implementation ‘com.jakewharton:butterknife:8.8.1’
annotationProcessor ‘com.jakewharton:butterknife-compiler:8.8.1’

private Button button,btn_create_QRCode;
    private static int REQUEST_CODE=123;
    private EditText et_info;
    private ImageView iv_qrCode;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button)findViewById(R.id.btn);
        et_info = (EditText) findViewById(R.id.et_info);
        iv_qrCode = (ImageView) findViewById(R.id.iv_QRCode);
        btn_create_QRCode= (Button) findViewById(R.id.btn_create_QRCode);
        //这个是写的点击监听
        button.setOnClickListener(this);
        btn_create_QRCode.setOnClickListener(this);
       
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == 123) {
            Log.e("MainActivity", "获取权限成功");
        }
    }

Android6.0以上需要加入动态权限
这是我用的权限,还需要用什么直接写进去就行

 //Android6.0权限配置
        if (Build.VERSION.SDK_INT >= 23) {
            String[] mPermissionList = new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE, Manifest.permission.READ_LOGS, Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.SET_DEBUG_APP, Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.GET_ACCOUNTS, Manifest.permission.WRITE_APN_SETTINGS};
            ActivityCompat.requestPermissions(this, mPermissionList, 123);
        }

点击监听

 public void onClick(View v) {
        switch (v.getId()){
        //这个是调用扫描二维码的界面
            case R.id.btn:
                Intent intent = new Intent(MainActivity.this,CaptureActivity.class);
               startActivityForResult(intent,REQUEST_CODE);
                break;
                //这个是生成二维码的
            case R.id.btn_create_QRCode:
                String textContent = et_info.getText().toString();
                if (TextUtils.isEmpty(textContent)) {
                    Toast.makeText(MainActivity.this, "您的输入为空!", Toast.LENGTH_SHORT).show();
                    return;
                }
               
                Bitmap mBitmap = CodeUtils.createImage(textContent, 400, 400, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
                iv_qrCode.setImageBitmap(mBitmap);
                break;
        }

    }

回传设置

 @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE) {
            //处理扫描结果(在界面上显示)
            if (null != data) {
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                    String result = bundle.getString(CodeUtils.RESULT_STRING);
                    Toast.makeText(this, "解析结果:" + result, Toast.LENGTH_LONG).show();
                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                    Toast.makeText(MainActivity.this, "解析二维码失败", Toast.LENGTH_LONG).show();
                }
            }
        }
    }

还需要创建一个activity来继承application进行初始化

public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        
        ZXingLibrary.initDisplayOpinion(this);
    }
}

下方是我的效果图,有点丑大概是这么个意思

Zxing来实现二维码扫描和生成(PS:超级简单)_第1张图片

Zxing来实现二维码扫描和生成(PS:超级简单)_第2张图片

你可能感兴趣的:(Zxing来实现二维码扫描和生成(PS:超级简单))