一、前言
前几天我通过学习和实践,使用华为统一扫描服务实现了用安卓手机扫条形码和二维码的功能,相关内容见我的日志《Android应用开发学习-使用华为统一扫码服务实现扫码功能》。当前我正以此为基础给销售部门的同事开发一款扫描盘点的小软件,帮助同事提高工作效率,现在软件已经能够实现一部分的功能了。今天向我向另外一个部门的同事展示扫描功能的时候,那位同事提出了希望为他制作一款能生成二维码的app用于特殊物品管理工作中。他告诉我,最近根据政府职能部门和公司内部要求,他负责管理的特殊物品必须张贴二维码,通过扫描二维码要能够得到物品的相关信息。当前,他正在使用一款某软件公司开发的软件来生成二维码,虽然可以实现目的,但信息会保存到软件开发方的服务器内,存在公司信息泄露的风险。
我作为公司科技创新工作室的负责人,听到这样的信息都会特别上心,先和同事沟通一番,了解了更多的信息。基本上同事对软件功能上的需求都是比较常规的,其他的需求我都有把握实现,唯独生成二维码的需求,我还没弄过,但有了前面扫描二维码的成功,对生成二维码的功能,我有信心。
二、实现方法
由于前面是用的华为统一扫码服务实现的扫码功能,我先到华为开发者官方网站上寻找相关资料,果然在华为统一扫码服务板块内查到了生成二维码的相关功能介绍——“构建码生成功能”。
https://developer.huawei.com/consumer/cn/doc/HMSCore-Guides/scan-generate-barcode-0000001050995005
把页面中的内容看一遍,发现比扫码功能的实现容易多了,照着官方提供的代码写就OK了。
三、代码展示
下面是我编写的部分代码
XML代码
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CreateQRCodeActivity">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:text="生成二维码"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/et_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:autofillHints=""
android:hint="输入要生成二位码的内容"
android:inputType="none"
android:imeOptions="actionDone"
android:minHeight="48dp"
android:textSize="17sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_title" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/btn_encode1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="生成二维码" />
<ImageView
android:id="@+id/iv_code1"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"
android:layout_marginTop="10dp" />
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
java代码
public class CreateQRCodeActivity extends AppCompatActivity implements View.OnClickListener {
private final static String TAG = "CreateQRCodeActivity";
private EditText et_content;
private ImageView iv_code1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_qrcode);
et_content = findViewById(R.id.et_content); // 要生成二维码的内容
iv_code1 = findViewById(R.id.iv_code1); // 显示二维码
findViewById(R.id.btn_encode1).setOnClickListener(this); // 生成二维码
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_encode1) {
String qrCodeText = et_content.getText().toString(); // 从EditText中获取输入的内容
int type = 3 // HmsScan.QRCODE_SCAN_TYPE 的值为1
int width = 300;
int height = 300;
HmsBuildBitmapOption options = new HmsBuildBitmapOption.Creator().create();;
try {
Bitmap qrBitmap = ScanUtil.buildBitmap(qrCodeText,type,width,height, options);
iv_code1.setImageBitmap(qrBitmap); // 将二维码显示在ImageView中
} catch (WriterException e) {
Log.w("buildBitmap", e);
}
}
}
}
四、运行效果
先测试一下生成二维码的功能,输入一窜字符,点“生成二维码”按钮,成功显示出了二维码。
把屏幕截个图,再用自己做了扫码模块识别一下,扫码界面选择右上角的图片按钮,从相册中选中刚才的截图。
扫码结果与输入的一致。考虑到数据的保密性,实际制作APP时,得把输入的信息进行加密,下一步研究一下加密和解密功能。