【鸿蒙harmoner开发】AI能力之二维码生成

二维码在现在的日常生活中非常常见,在鸿蒙系统中无需引入任何控件即可实现二维码的功能。

 

 二维码生成功能属于鸿蒙AI能力中的功能。所以在使用二维码生成功能之前我们需要初始化AI能力引擎。AI能力引擎初始化成功后会有相应的连接回调。 

一、初始化AI引擎

回调接口

    private ConnectionCallback connectionCallback = new ConnectionCallback() {
        @Override
        public void onServiceConnect() {
            // 服务连接成功回调。
        }

        @Override
        public void onServiceDisconnect() {
            // 服务断开回调。
        }
    };

初始化AI引擎

VisionManager.init(this, connectionCallback);

二、获取条码检测器

服务连接成功后获取条码检测器

IBarcodeDetector barcodeDetector = VisionManager.getBarcodeDetector(getAbility());

解析成图片并显示

        @Override
        public void onServiceConnect() {
            IBarcodeDetector barcodeDetector = VisionManager.getBarcodeDetector(getAbility());

            final int SAMPLE_LENGTH = 152;
            byte[] byteArray = new byte[SAMPLE_LENGTH * SAMPLE_LENGTH * 4];
            int result = barcodeDetector.detect("https://www.baidu.com", byteArray, SAMPLE_LENGTH, SAMPLE_LENGTH);
            if (result == 0) {
                ImageSource.SourceOptions options = new ImageSource.SourceOptions();
                options.formatHint = "image/png";
                ImageSource imageSource = ImageSource.create(byteArray, options);
                PixelMap pixelMap = imageSource.createPixelmap(null);
                image.setPixelMap(pixelMap);
            }

            barcodeDetector.release();
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值