本帖最后由 liu 于 2019-1-2 15:50 编辑
二维码的生成和扫描
本次将使用github上的开源框架zxing来实现二维码的生成和扫描
首先在build.gradle中添加 compile 'cn.yipianfengye.android:zxing-library:2.2'
添加权限[Java] 纯文本查看 复制代码 <uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
然后在Application中执行初始化操作
[Java] 纯文本查看 复制代码 public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
ZXingLibrary.initDisplayOpinion(this);
}
}
生成:
[Java] 纯文本查看 复制代码 String textContent = mEditText.getText().toString();
if (TextUtils.isEmpty(textContent)) {
Toast.makeText(MainActivity.this, "请输入内容!", Toast.LENGTH_SHORT).show();
return;
}
//生成不带Logo的二维码
// Bitmap mBitmap = CodeUtils.createImage(textContent, 400, 400, null);
// mImageView.setImageBitmap(mBitmap);
//生成带Logo的二维码
Bitmap mBitmap = CodeUtils.createImage(textContent, 400, 400, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
mImageView.setImageBitmap(mBitmap);
扫描:
[Java] 纯文本查看 复制代码 //打开默认二维码扫描界面
Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
startActivityForResult(intent, REQUEST_CODE);
在Activity的onActivityResult方法中接收扫描结果:
[Java] 纯文本查看 复制代码 @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_LONG).show();
} else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
Toast.makeText(MainActivity.this, "解析二维码失败", Toast.LENGTH_LONG).show();
}
}
}
}
完整代码:
activity_main.xml:
[XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="20dp"
tools:context="com.company.helloworld.test.MainActivity">
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="生成二维码" />
<ImageView
android:id="@+id/iv"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginTop="20dp" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="扫描二维码" />
</LinearLayout>
MyApplication:[Java] 纯文本查看 复制代码 public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
ZXingLibrary.initDisplayOpinion(this);
}
} AndroidManifest.xml:
MainActivity:
[Java] 纯文本查看 复制代码 public class MainActivity extends AppCompatActivity {
private Button mButton1;
private Button mButton2;
private EditText mEditText;
private ImageView mImageView;
private final int REQUEST_CODE = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mButton1 = (Button) findViewById(R.id.btn);
mButton2 = (Button) findViewById(R.id.btn2);
mEditText = (EditText) findViewById(R.id.et);
mImageView = (ImageView) findViewById(R.id.iv);
mButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String textContent = mEditText.getText().toString();
if (TextUtils.isEmpty(textContent)) {
Toast.makeText(MainActivity.this, "请输入内容!", Toast.LENGTH_SHORT).show();
return;
}
//生成不带Logo的二维码
// Bitmap mBitmap = CodeUtils.createImage(textContent, 400, 400, null);
// mImageView.setImageBitmap(mBitmap);
//生成带Logo的二维码
Bitmap mBitmap = CodeUtils.createImage(textContent, 400, 400, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
mImageView.setImageBitmap(mBitmap);
}
});
mButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//打开默认二维码扫描界面
Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent 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_LONG).show();
} else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
Toast.makeText(MainActivity.this, "解析二维码失败", Toast.LENGTH_LONG).show();
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
效果图:
|