二维码生成扫描
程序员文章站
2022-07-15 14:59:36
...
implementation 'cn.yipianfengye.android:zxing-library:2.2'
初始化
ZXingLibrary.initDisplayOpinion(this);
权限
<!--二维码扫描必须加震动权限-->
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
<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"/>
代码
/*扫描二维码*/
public void sao(View view) {
Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}
//二维码重写onActivityResult
@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_SHORT).show();
}else if (bundle.getInt(CodeUtils.RESULT_TYPE)==CodeUtils.RESULT_FAILED){
Toast.makeText(this, "解析二维码失败", Toast.LENGTH_SHORT).show();
}
}
}
}
/*生成带login二维码*/
public void sheng(View view) {
//得到edittext的我文本内容
String s = mEdit_text.getText().toString();
if (TextUtils.isEmpty(s)) {
Toast.makeText(this, "你输入的内容为空", Toast.LENGTH_SHORT).show();
}
mEdit_text.setText("");
Bitmap image = CodeUtils.createImage(s, 400, 400, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
mImg1.setImageBitmap(image);
}
上一篇: 广播监听网络