简单的二维码实现

依赖文件

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

添加name

<application
        android:name=".zxing.Zxing"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

清单文件权限

  <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.FLASHLIGHT" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

单机监听

 @Override
    public void onClick(View v) {
        switch (v.getId()) {
       
      
            //生成事件
        case R.id.erweima:
            String erweimashuruString = mErweimashuru.getText().toString().trim();
            if (TextUtils.isEmpty(erweimashuruString)) {
                Toast.makeText(this, "请输入要生成的内容", Toast.LENGTH_SHORT).show();
                return;
            }

            /**
             * 生成不带logo的二维码图片
             */

            mErweimashuru.setText("");
            mBitmap = CodeUtils.createImage(erweimashuruString, 400, 400, null);
            mImageView.setImageBitmap(mBitmap);

            break;
        case R.id.erweimaimage:
            String erweimashuruString1 = mErweimashuru.getText().toString().trim();
            if (TextUtils.isEmpty(erweimashuruString1)) {
                Toast.makeText(this, "请输入要生成的内容", Toast.LENGTH_SHORT).show();
            }
                mErweimashuru.setText("");
                mbitmap = CodeUtils.createImage(erweimashuruString1, 400, 400, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
                mImageView.setImageBitmap(mbitmap);

                break;

这里是扫描

          case R.id.saomiao:
                Intent intent = new Intent(ChenggongActivity.this, CaptureActivity.class);
                startActivityForResult(intent, 1);
                break;
        }
}

这里是回调方法

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /**
         * 处理二维码扫描结果
         */
        if (requestCode == 1) {
            //处理扫描结果(在界面上显示)
            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(ChenggongActivity.this, "解析二维码失败", Toast.LENGTH_LONG).show();
                }

            }
        }

    }

自定义类继承调用

public class Zxing extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        ZXingLibrary.initDisplayOpinion(this);
    } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值