扫一扫与二维码

//1.首先导入依赖

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

//2.在布局文件中

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

<EditText
    android:id="@+id/edit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />
<Button
    android:id="@+id/button_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="生成不带logo的二维码"
    />
<Button
    android:id="@+id/button_3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="生成带logo的二维码"
    />

<ImageView
    android:id="@+id/img"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@mipmap/ic_launcher"
    />

//3.在主页面中重写onactivityresult的方法(扫描二维码时需要写)
(request_code)是自己定义的int常量

@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_SHORT).show();
            }if (bundle.getInt(CodeUtils.RESULT_TYPE)==CodeUtils.RESULT_FAILED){
                Toast.makeText(this,"解析失败",Toast.LENGTH_SHORT).show();
            }
        }
    }
}

//4.扫描二维码,如果不出来就在6.0动态权限中Manifest.permission.CAMERA

Intent intent=new Intent(MainActivity.this,CaptureActivity.class);
            startActivityForResult(intent,request_code);

//生成不带logo的二维码

String text = edittext.getText().toString();
            if (TextUtils.isEmpty(text)){
                Toast.makeText(this,"您输入的为空",Toast.LENGTH_SHORT).show();
                return;
            }
            edittext.setText("");
            Bitmap bitmap = CodeUtils.createImage(text, 400, 400, null);
            img.setImageBitmap(bitmap);

//生成带logo的二维码

 text2 = edittext.getText().toString();
            if (TextUtils.isEmpty(text2)) {
                Toast.makeText(this, "您的输入为空!", Toast.LENGTH_SHORT).show();
                return;
            }

            bitmap = CodeUtils.createImage(text2, 400, 400, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
            img.setImageBitmap(bitmap);

//定义全局配置类

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

//清单文件中配置权限

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

//以及application中配置name

android:name=".App"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值