依赖文件
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);
} }