一个简单的布局
第一步:添加依赖
//二维码依赖
implementation 'cn.yipianfengye.android:zxing-library:2.1'
1
这时候你要添加进去的话会有一个版本冲突,下面解决V4、V7版本冲突
//解决V4、V7版本冲突
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support') {
if (!requested.name.startsWith("multidex")) {
details.useVersion '27.1.1'
}
}
}
}
第二步添加权限
<!-- 二维码需要的权限 -->
<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" />
<uses-permission android:name="android.permission.INTERNET" />
第三步在类继承Application中初始化二维码工具
//需要在清单文件注册
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//初始化二维码工具类
ZXingLibrary.initDisplayOpinion(this);
}
}
第四步:布局,很简单就2个按钮
第五步:开始写MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//相应状态码
private static final int RESPON_CODE = 5 ;
//点击事件
public void co(View view) {
//跳转到扫描二维码界面操作
Intent intent = new Intent(MainActivity.this,CaptureActivity.class);
startActivityForResult(intent,RESPON_CODE);
}
//为了方便接收所以需要onActivityResult分别进行处理
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//处理二维码扫描结果,对状态码进行判断
if(requestCode == RESPON_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(MainActivity.this , "解析二维码失败", Toast.LENGTH_SHORT).show();
}
}
}
}
/*
* 跳到生成二维码图片的地方
* */
public void cod(View view) {
startActivity(new Intent(MainActivity.this,QRCodeActivity.class));
finish();
}
}
第六步:跳转到生成二维码的Actvity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qrcode);
//找控件
mEditText = findViewById(R.id.editText);
mLogBtn = findViewById(R.id.logBtn);
mPtBtn = findViewById(R.id.ptBtn);
mImageView = findViewById(R.id.imageView);
}
//带log二维码,两个点击事件
public void logB(View view) {
//获取输入框的内容
String textContent = mEditText.getText().toString();
//判断是否为空
if(TextUtils.isEmpty(textContent)){
Toast.makeText(this, "输入的为空", Toast.LENGTH_SHORT).show();
return;
}
mEditText.setText("");
mBitmap = CodeUtils.createImage(textContent, 400, 400, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
mImageView.setImageBitmap(mBitmap);
}
//不带log
public void ptB(View view) {
//获取输入框的内容
String textContent = mEditText.getText().toString();
//判断是否为空
if(TextUtils.isEmpty(textContent)){
Toast.makeText(this, "输入的为空", Toast.LENGTH_SHORT).show();
return;
}
mEditText.setText("");
Bitmap bitmap = CodeUtils.createImage(textContent, 400, 400, null);
mImageView.setImageBitmap(bitmap);
}
布局还是特别简单的
---------------------
作者:家辉,喂
来源:CSDN
原文:https://blog.csdn.net/jiahui6666/article/details/83831478
版权声明:本文为博主原创文章,转载请附上博文链接!
|
|