在實際應用開發中,時不時的會遇到 AI 領域相關的一些技術,本節主要詳細講述一下生成二維碼技術,二維碼可能涉及在各領域中,如:社交或通訊類應用、購物或支付類應用等。
所以對于 HarmonyOS 開發者而言,也需要了解和掌握 HarmonyOS AI 領域相關技術,這對于每一個 HarmonyOS 開發者,也是一項必不可少的專業技能。
功能介紹
生成二維碼主要根據開發者給定的字符串信息和二維碼圖片尺寸,返回相應的二維碼圖片字節流。調用方可以通過二維碼字節流生成二維碼圖片。
開發指南
①創建二維碼
實例化接口,獲取二維碼偵測器:
IBarcodeDetectorbarcodeDetector
=VisionManager.getBarcodeDetector(QRCodeAbilitySlice.this);
定義碼生成圖像的尺寸:
finalintSAMPLE_LENGTH=500;
根據圖像的大小,分配字節流數組的空間:
byte[]byteArray=newbyte[SAMPLE_LENGTH*SAMPLE_LENGTH*4];
調用 IBarcodeDetector 的 detect() 方法,根據輸入的字符串信息 barText 生成相應的二維碼圖片字節流:
barcodeDetector.detect(barText,byteArray,SAMPLE_LENGTH,SAMPLE_LENGTH);
釋放偵測器:
barcodeDetector.release();
通過 SourceOptions 指定數據源的格式信息:
ImageSource.SourceOptionssrcOpts=newImageSource.SourceOptions();
定義圖片格式:
srcOpts.formatHint="image/png";
創建圖片源:
ImageSourceimgSource=ImageSource.create(byteArray,srcOpts);
創建圖像解碼選項:
ImageSource.DecodingOptionsdecodingOpts=new
ImageSource.DecodingOptions();
decodingOpts.desiredPixelFormat=PixelFormat.ARGB_8888;
通過圖片源創建 PixelMap:
PixelMappMap=imgSource.createPixelmap(decodingOpts);
賦值到圖片標簽:
imgQrCode.setPixelMap(pMap);
釋放資源:
barcodeDetector.release();
imgSource.release();
if(pMap!=null)
{
pMap.release();
}
斷開與能力引擎的連接:
VisionManager.destroy();
②定義 ConnectionCallback 回調,實現連接能力引擎成功與否后的操作
代碼如下:
ConnectionCallbackconnectionCallback=newConnectionCallback(){
@Override
publicvoidonServiceConnect(){
需要生成二維碼的字符串:
StringbarText="";
連接成功生成二維碼:
createQRCode(barText);
}
@Override
publicvoidonServiceDisconnect(){
//Dosomethingwhenserviceconnectsunsuccessfully
}
};
③調用 VisionManager.init() 方法,將此工程的 context 和 connectionCallback作為入參,建立與能力引擎的連接
代碼如下:
intresult=VisionManager.init(context,connectionCallback);
示例代碼
xml 布局:
<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
packagecom.isoftstone.qrcode.slice;
importcom.isoftstone.qrcode.ResourceTable;
importohos.aafwk.ability.AbilitySlice;
importohos.aafwk.content.Intent;
importohos.agp.components.Text;
publicclassMainAbilitySliceextendsAbilitySlice{
@Override
publicvoidonStart(Intentintent){
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
TextqrCode=(Text)findComponentById(ResourceTable.Id_qrCode_text);
qrCode.setClickedListener(component->present(newQRCodeAbilitySlice(),newIntent()));
}
@Override
publicvoidonActive(){
super.onActive();
}
@Override
publicvoidonForeground(Intentintent){
super.onForeground(intent);
}
}
QRCodeAbilitySlice.java
packagecom.isoftstone.qrcode.slice;
importcom.isoftstone.qrcode.ResourceTable;
importohos.aafwk.ability.AbilitySlice;
importohos.aafwk.content.Intent;
importohos.agp.components.Image;
importohos.ai.cv.common.ConnectionCallback;
importohos.ai.cv.common.VisionManager;
importohos.ai.cv.qrcode.IBarcodeDetector;
importohos.media.image.ImageSource;
importohos.media.image.PixelMap;
importohos.media.image.common.PixelFormat;
/**
*二維碼生成
*/
publicclassQRCodeAbilitySliceextendsAbilitySlice{
privateImageimgQrCode;
@Override
publicvoidonStart(Intentintent){
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_qrcode);
imgQrCode=(Image)findComponentById(ResourceTable.Id_imgQrCode);
}
@Override
publicvoidonActive(){
super.onActive();
ConnectionCallbackconnectionCallback=newConnectionCallback(){
@Override
publicvoidonServiceConnect(){
//需要生成二維碼的字符串
StringbarText="www.baidu.com";
//連接成功生成二維碼
createQRCode(barText);
}
@Override
publicvoidonServiceDisconnect(){
//Dosomethingwhenserviceconnectsunsuccessfully
}
};
//初始化,建立與能力引擎的連接
VisionManager.init(this,connectionCallback);
}
@Override
publicvoidonForeground(Intentintent){
super.onForeground(intent);
}
/**
*創建二維碼
*@parambarText需要生成二維碼的字符串
*/
privatevoidcreateQRCode(StringbarText){
//實例化接口,獲取二維碼偵測器
IBarcodeDetectorbarcodeDetector=VisionManager.getBarcodeDetector(QRCodeAbilitySlice.this);
//定義碼生成圖像的尺寸
finalintSAMPLE_LENGTH=500;
//根據圖像的大小,分配字節流數組的空間
byte[]byteArray=newbyte[SAMPLE_LENGTH*SAMPLE_LENGTH*4];
//調用IBarcodeDetector的detect()方法,根據輸入的字符串信息生成相應的二維碼圖片字節流
barcodeDetector.detect(barText,byteArray,SAMPLE_LENGTH,SAMPLE_LENGTH);
//釋放偵測器
barcodeDetector.release();
//通過SourceOptions指定數據源的格式信息
ImageSource.SourceOptionssrcOpts=newImageSource.SourceOptions();
//定義圖片格式
srcOpts.formatHint="image/png";
//創建圖片源
ImageSourceimgSource=ImageSource.create(byteArray,srcOpts);
//創建圖像解碼選項
ImageSource.DecodingOptionsdecodingOpts=newImageSource.DecodingOptions();
decodingOpts.desiredPixelFormat=PixelFormat.ARGB_8888;
//通過圖片源創建PixelMap
PixelMappMap=imgSource.createPixelmap(decodingOpts);
//賦值到圖片標簽
imgQrCode.setPixelMap(pMap);
//釋放資源
barcodeDetector.release();
imgSource.release();
if(pMap!=null)
{
pMap.release();
}
//斷開與能力引擎的連接
VisionManager.destroy();
}
}
責任編輯:haq
-
鴻蒙系統
+關注
關注
183文章
2642瀏覽量
69834 -
HarmonyOS
+關注
關注
80文章
2153瀏覽量
36039
原文標題:在鴻蒙上生成二維碼的方法!
文章出處:【微信號:gh_834c4b3d87fe,微信公眾號:OpenHarmony技術社區】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
有哪些常見的二維碼模組類型?
二維碼掃碼模塊是什么?它和普通條碼識別設備有什么不同
門禁二維碼掃描模塊使用哪個產品合適?
哪款二維碼模組適合嵌入戶外取餐柜,用于掃二維碼
【嘉楠堪智K230開發板試用體驗】+二維碼識別
掃描條碼模塊、二維碼模塊,廣泛應用于定制的手持設備
基于STM32的二維碼識別源碼+二維碼解碼庫lib
二維碼讀取器是干嘛的
一“碼”當先!看二維碼模組如何重塑智能門鎖掃碼體驗
嵌入式二維碼識別引擎是什么設備?哪些場景用得到?
鴻蒙系統生成二維碼技術
評論