implementation 'cn.yipianfengye.android:zxing-library: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();
}
}
}
}
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"