zxing实现二维码的扫描 和 二维码的生成

倒依赖

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

权限

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />

布局文件中得代码

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editText03"
    android:hint="请输入想要生成的二维码的信息"/>
<Button
    android:id="@+id/btn03"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="点击生成二维码"/>
<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:id="@+id/imageView"/>

MainActivity中的代码

 生成二维码

private Button btn03,btn04,btn05;
private EditText editText03;
private ImageView imageView;
editText03 = findViewById(R.id.editText03);
imageView = findViewById(R.id.imageView);
btn03 = findViewById(R.id.btn03);
private int REQUEST_CODE = 0x1000;
ZXingLibrary.initDisplayOpinion(this);
btn03.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String string = editText03.getText().toString();
        bitmap = CodeUtils.createImage(string,400, 400, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
        imageView.setImageBitmap(bitmap);
    }
});

二维码扫描布局文件中的代码

<Button
    android:id="@+id/btn05"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="点击扫描二维码"/>

二维码扫描activity文件中的代码

btn05.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
        startActivityForResult(intent, REQUEST_CODE);
    }
});
@Override
protected void onActivityResult(int requestCode, int resultCode, 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();
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/bai1452789545/article/details/82632238