一、前言
- 最近,有个Android端项目需要加入二维码扫描及拍照上传的功能,本篇博文将介绍以下两个功能的实现:。
- 用户点击扫描按钮,app自动打开摄像头,等待扫描;
- 用户进行二维码扫描,扫描后的内容发往后台进行校验;
- app等待校验结果,若校验结果通过,则继续打开摄像头,并提示用户进行拍照留存;
- 用户拍照后,app程序会将拍照后的图片上传至后台服务器。
二、主界面布局
- 布局实景图

- 主页面布局
- 循环组件,循环列表中放置卡片布局
- 底部导航条显示首页、统计、设置三个菜单按钮
- 放置浮动按钮,点击此按钮调用摄象头进行二维码扫描
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="?attr/colorButtonNormal">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycleView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="58dp"
app:layout_constraintBottom_toTopOf="@+id/navigation"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="56dp"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/scan