快捷搜索: 王者荣耀 脱发

【鸿蒙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();
        }
经验分享 程序员 微信小程序 职场和发展