在鸿蒙上生成二维码的方法!
在实际应用开发中,时不时的会遇到 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];
barcodeDetector.detect(barText,byteArray,SAMPLE_LENGTH,SAMPLE_LENGTH);
barcodeDetector.release();
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 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/

随时掌握互联网精彩
赞助链接
排名
热点
搜索指数
- 1 让乡亲们的生活芝麻开花节节高 7943570
- 2 中央一号文件:推进农村高额彩礼治理 7970541
- 3 DeepSeek预测《哪吒2》最终票房 7828180
- 4 以“真金白银”为企业加油 7774188
- 5 姚晨闷声干大事 7693596
- 6 塌房大主播集体“复活” 7537716
- 7 00后女生扎哪吒头舞红缨枪真飒 7486304
- 8 不允许城镇居民到农村买农房、宅基地 7314781
- 9 县委书记上午投案中午落马 7237924
- 10 宝马行驶中突发无电滑行 车主发声 7162292