欢迎学习Android 扫一扫功能
主要实现的功能
1.二维码解析
2.二维码生成
二维码的库源码(慕课网链接)
导入方法:1.导入module:file>new>import module>libzxing
2.源码库添加为依赖库:
Project Structure(ctrl+alt+shift+s)>Modules>app>dependencies>点击绿色加号,选择第三个选项
Demo下载 密码:vrf6(百度云链接)
源码部分
主界面XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:gravity="center"
android:text="扫一扫二维码"
android:textSize="24sp"/>
<Button
android:textSize="18sp"
android:id="@+id/btn_sao"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@color/colorPrimary"
android:text="扫一扫"
android:textColor="#FFFFFF"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="扫描结果:"
android:textSize="18sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:textSize="16sp"
tools:text="预览"/>
<View
android:layout_width="match_parent"
android:layout_height="1.5px"
android:layout_margin="20dp"
android:background="@color/colorPrimary"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="生成二维码"
android:textSize="24sp"/>
<EditText
android:id="@+id/ed_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:hint="请输入需要装换的文字"/>
<CheckBox
android:id="@+id/cb_logo"
android:text="logo"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:layout_marginTop="10dp"
android:textSize="18sp"
android:background="@color/colorPrimary"
android:textColor="#FFFFFF"
android:id="@+id/btn_scan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="生成"/>
<ImageView
android:id="@+id/iv_result"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
</LinearLayout>
主页activity代码
- Buttom的点击事件
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_sao:
startActivityForResult(new Intent(this, CaptureActivity.class), 0);
break;
case R.id.btn_scan:
String input = mEdInput.getText().toString().trim();
if (input.equals("")) {
Toast.makeText(HomeActivity.this, "输入不能为空", Toast.LENGTH_SHORT).show();
} else {
//mCheckBox是否在二维码中心添加logo
Bitmap bitmap = EncodingUtils.createQRCode(input, 500, 500, mCheckBox.isChecked() ? BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher) : null);
mIvResult.setImageBitmap(bitmap);
}
break;
}
}
- 回调事件
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
String result = bundle.getString("result");
mTvResult.setText(result);
}
}
遇到问题
弹出提示框 camera error
解决方法:1.手机安全中心->授权管理->应用权限管理->程序名称->多媒体相关相机、拍照、录像和闪光灯选中
2.设置->其他应用程序->程序名称->权限管理->多媒体相关相机、拍照、录像和闪光灯选中