在鸿蒙上生成二维码的方法!

百家 作者:51CTO技术栈 2021-10-12 20:36:09

在实际应用开发中,时不时的会遇到 AI 领域相关的一些技术,本节主要详细讲述一下生成二维码技术,二维码可能涉及在各领域中,如:社交或通讯类应用、购物或支付类应用等。


所以对于 HarmonyOS 开发者而言,也需要了解和掌握 HarmonyOS AI 领域相关技术,这对于每一个 HarmonyOS 开发者,也是一项必不可少的专业技能。


功能介绍


生成二维码主要根据开发者给定的字符串信息和二维码图片尺寸,返回相应的二维码图片字节流。调用方可以通过二维码字节流生成二维码图片。


开发指南


①创建二维码


实例化接口,获取二维码侦测器:
????IBarcodeDetector?barcodeDetector??
=VisionManager.getBarcodeDetector(QRCodeAbilitySlice.this);

定义码生成图像的尺寸:
?????final?int?SAMPLE_LENGTH?=?500;

根据图像的大小,分配字节流数组的空间:
????byte[]?byteArray?=?new?byte[SAMPLE_LENGTH?*?SAMPLE_LENGTH?*?4];

调用 IBarcodeDetector 的 detect() 方法,根据输入的字符串信息 barText 生成相应的二维码图片字节流:
barcodeDetector.detect(barText,byteArray,SAMPLE_LENGTH,SAMPLE_LENGTH);

释放侦测器:
????barcodeDetector.release();

通过 SourceOptions 指定数据源的格式信息:
????ImageSource.SourceOptions?srcOpts?=?new?ImageSource.SourceOptions();

定义图片格式:
????srcOpts.formatHint?=?"image/png";

创建图片源:
??????ImageSource?imgSource=?ImageSource.create(byteArray,srcOpts);

创建图像解码选项:
????ImageSource.DecodingOptions?decodingOpts?=new?
ImageSource.DecodingOptions?();?
decodingOpts.desiredPixelFormat=?PixelFormat.ARGB_8888;

通过图片源创建 PixelMap:
?????PixelMap?pMap?=imgSource.createPixelmap(decodingOpts);

赋值到图片标签:
?????imgQrCode.setPixelMap(pMap);

释放资源:
?????barcodeDetector.release?();
imgSource.release?();
??if?(pMap!?=null)
????{
????????pMap.release?();
????}


断开与能力引擎的连接:
?????VisionManager.destroy?();


②定义 ConnectionCallback 回调,实现连接能力引擎成功与否后的操作


代码如下:
ConnectionCallback?connectionCallback?=?new?ConnectionCallback?()?{
????@Override
????public?void?onServiceConnect?()?{

需要生成二维码的字符串:
??????String?barText=?"";


连接成功生成二维码:
???????createQRCode(barText);
????}
????@Override
????public?void?onServiceDisconnect?()?{
????????//?Do?something?when?service?connects?unsuccessfully
????}
};

③调用 VisionManager.init() 方法,将此工程的 context 和 connectionCallback?作为入参,建立与能力引擎的连接


代码如下:
int?result?=?VisionManager.init(context,?connectionCallback);


示例代码


xml 布局:

<?xml?version="1.0"?encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="vertical">


<Image
??ohos:id="$+id:imgQrCode"
??ohos:height="500vp"
??ohos:width="500vp"
??ohos:layout_alignment="center"/>


</DirectionalLayout>

案例代码:

MainAbilitySlice.java
package?com.isoftstone.qrcode.slice;

import?com.isoftstone.qrcode.ResourceTable;
import?ohos.aafwk.ability.AbilitySlice;
import?ohos.aafwk.content.Intent;
import?ohos.agp.components.Text;

public?class?MainAbilitySlice?extends?AbilitySlice?{

??@Override
??public?void?onStart(Intent?intent)?{
????super.onStart(intent);
????super.setUIContent(ResourceTable.Layout_ability_main);
????Text?qrCode?=?(Text)?findComponentById(ResourceTable.Id_qrCode_text);
????qrCode.setClickedListener(component?->?present(new?QRCodeAbilitySlice(),new?Intent()));
??}

??@Override
??public?void?onActive()?{
????super.onActive();
??}

??@Override
??public?void?onForeground(Intent?intent)?{
????super.onForeground(intent);
??}
}
QRCodeAbilitySlice.java

package?com.isoftstone.qrcode.slice;

import?com.isoftstone.qrcode.ResourceTable;
import?ohos.aafwk.ability.AbilitySlice;
import?ohos.aafwk.content.Intent;
import?ohos.agp.components.Image;
import?ohos.ai.cv.common.ConnectionCallback;
import?ohos.ai.cv.common.VisionManager;
import?ohos.ai.cv.qrcode.IBarcodeDetector;
import?ohos.media.image.ImageSource;
import?ohos.media.image.PixelMap;
import?ohos.media.image.common.PixelFormat;

/**
?*?二维码生成
?*/

public?class?QRCodeAbilitySlice?extends?AbilitySlice?{

????private?Image?imgQrCode;
????@Override
????public?void?onStart(Intent?intent)?{
????????super.onStart(intent);
????????super.setUIContent(ResourceTable.Layout_ability_qrcode);
????????imgQrCode=(Image)findComponentById(ResourceTable.Id_imgQrCode);
????}

????@Override
????public?void?onActive()?{
????????super.onActive();
????????ConnectionCallback?connectionCallback?=?new?ConnectionCallback()?{
????????????@Override
????????????public?void?onServiceConnect()?{
????????????????//需要生成二维码的字符串
????????????????String?barText=?"www.baidu.com";
????????????????//连接成功生成二维码
????????????????createQRCode(barText);
????????????}

????????????@Override
????????????public?void?onServiceDisconnect()?{
????????????????//?Do?something?when?service?connects?unsuccessfully
????????????}
????????};
????????//初始化,建立与能力引擎的连接
????????VisionManager.init(this,?connectionCallback);
????}

????@Override
????public?void?onForeground(Intent?intent)?{
????????super.onForeground(intent);
????}

????/**
?????*?创建二维码
?????*?@param?barText?需要生成二维码的字符串
?????*/

????private?void?createQRCode(String?barText){
????????//实例化接口,获取二维码侦测器
????????IBarcodeDetector?barcodeDetector?=?VisionManager.getBarcodeDetector(QRCodeAbilitySlice.this);
????????//定义码生成图像的尺寸
????????final?int?SAMPLE_LENGTH?=?500;
????????//根据图像的大小,分配字节流数组的空间
????????byte[]?byteArray?=?new?byte[SAMPLE_LENGTH?*?SAMPLE_LENGTH?*?4];
????????//调用IBarcodeDetector的detect()方法,根据输入的字符串信息生成相应的二维码图片字节流
????????barcodeDetector.detect(barText,?byteArray,?SAMPLE_LENGTH,?SAMPLE_LENGTH);
????????//释放侦测器
????????barcodeDetector.release();
????????//通过SourceOptions指定数据源的格式信息
????????ImageSource.SourceOptions?srcOpts?=?new?ImageSource.SourceOptions();
????????//定义图片格式
????????srcOpts.formatHint?=?"image/png";
????????//创建图片源
????????ImageSource?imgSource=?ImageSource.create(byteArray,srcOpts);
????????//创建图像解码选项
????????ImageSource.DecodingOptions?decodingOpts?=new?ImageSource.DecodingOptions();
????????decodingOpts.desiredPixelFormat=?PixelFormat.ARGB_8888;
????????//通过图片源创建PixelMap
????????PixelMap?pMap?=imgSource.createPixelmap(decodingOpts);
????????//赋值到图片标签
????????imgQrCode.setPixelMap(pMap);
????????//释放资源
????????barcodeDetector.release();
????????imgSource.release();
????????if(pMap!=null)
????????{
????????????pMap.release();
????????}
????????//断开与能力引擎的连接
????????VisionManager.destroy();
????}
}


实现效果


如下图:

关注公众号:拾黑(shiheibook)了解更多

[广告]赞助链接:

四季很好,只要有你,文娱排行榜:https://www.yaopaiming.com/
让资讯触达的更精准有趣:https://www.0xu.cn/

公众号 关注网络尖刀微信公众号
随时掌握互联网精彩
赞助链接