鸿蒙系统生成二维码技术

在实际应用开发中,时不时的会遇到 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 布局:
  案例代码:
mainabilityslice.javapackage 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.javapackage 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();    }}  


深圳达信电路板全厂解散!
凡亿依托完善的互联网交易平台 获得国外知名企业的高度认可
NASA计划2023年月球车运行开源软件
DEKRA德凯实验室成为联想授权认可的第三方实验室
高通6000万到账,夏普确认两者将在MEMS技术上合作
鸿蒙系统生成二维码技术
光电通信:WDM-PON关键技术解析
未来三年AI投资规模都会超过千亿元,全国5G基站将约为653万座
简单介绍个人服务器的相关详细配置要求
三星推出多款Galaxy Book系列笔记本电脑
深圳大学城携手华为打造数字化一卡通
差分探头差分信号的常见测量方法
机器人服务产业备受关注,***机器人都已经提上日程
中国芯迎好消息,紫光展锐宣布拟在国内建设DRAM芯片工厂
钢水测温仪的特点_钢水测温仪的优点
R128点灯指南加强篇—LEDC点三色流水灯(WS2812)
新品华为mate10保时捷版曝光,售价过万,连iPhoneX都无力对抗
站群相关问题的解答
温湿度检测仪适用于城市地下管廊监测系统中吗?
交换机和Wi-Fi是无线网络坚强的后盾