二维码识别

本文介绍如何使用二维码识别功能。

功能简介

二维码识别功能可以识别图片(包括照片、截图等多种图片)中多个有效二维码的位置及内容,输出图像中二维码的位置框和其包含的文本信息。其中位置包含左上角横坐标、左上角纵坐标、宽度和高度的值,如下图所示。

figqcode11

基于获取到的二维码内容信息和位置信息,您可以在应用中实现二维码扫描和读取,以及对图片中的二维码进行屏蔽、打码等操作。

前提条件

使用方法

调用DetectImageCodes - 图片二维码检测接口获取目标图片二维码信息。

图片信息

  • IMM项目名称:test-project

  • 图片的存储地址:oss://test-bucket/test-object.jpg

请求示例

{
    "ProjectName": "test-project",
    "SourceURI": "oss://test-bucket/test-object.jpg",
}

返回示例

{
    "Codes": [
        {
            "Type": "qrcode",
            "Content": "二维码又称二维条码,常见的二维码为QR Code,QR全称Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的Bar Code条形码能存更多的信息,也能表示更多的数据类型。",
            "Boundary": {
                "Left": 5,
                "Top": 44,
                "Height": 246,
                "Width": 238
            }
        },
        {
            "Type": "qrcode",
            "Content": "https://www.aliyun.com/product/imm",
            "Boundary": {
                "Left": 11,
                "Top": 64,
                "Height": 449,
                "Width": 458
            }
        }
    ],
    "RequestId": "50921E47-BC18-0351-8AFE-4FA50AF*****"
}
说明

返回示例显示该图片有两个二维码。

示例代码

以Python SDK为例,二维码识别的完整示例代码如下。

# -*- coding: utf-8 -*-
# This file is auto-generated, don't edit it. Thanks.
import sys

from typing import List

from alibabacloud_imm20200930.client import Client as imm20200930Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_imm20200930 import models as imm_20200930_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient


class Sample:
    def __init__(self):
        pass

    @staticmethod
    def create_client(
        access_key_id: str,
        access_key_secret: str,
    ) -> imm20200930Client:
        """
        使用AccessKey ID&AccessKey Secret初始化账号Client。
        @param access_key_id:
        @param access_key_secret:
        @return: Client
        @throws Exception
        """
        config = open_api_models.Config(
            # 填写AccessKey ID。
            access_key_id=access_key_id,
            # 填写AccessKey Secret。
            access_key_secret=access_key_secret
        )
        # 填写访问的IMM域名。
        config.endpoint = f'imm.cn-beijing.aliyuncs.com'
        return imm20200930Client(config)

    @staticmethod
    def main(
        args: List[str],
    ) -> None:
        # 工程代码泄露可能会导致AccessKey泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议使用更安全的STS方式,更多鉴权访问方式请参见https://help.aliyun.com/document_detail/378659.html。
        client = Sample.create_client('accessKeyId', 'accessKeySecret')
        detect_image_codes_request = imm_20200930_models.DetectImageCodesRequest(
            project_name='test-project',
            source_uri='oss://test-bucket/test-object.jpg'
        )
        runtime = util_models.RuntimeOptions()
        try:
            # 复制代码运行请自行打印API的返回值。
            client.detect_image_codes_with_options(detect_image_codes_request, runtime)
        except Exception as error:
            # 如有需要,请打印错误信息。
            UtilClient.assert_as_string(error.message)

    @staticmethod
    async def main_async(
        args: List[str],
    ) -> None:
        # 工程代码泄露可能会导致AccessKey泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议使用更安全的STS方式,更多鉴权访问方式请参见https://help.aliyun.com/document_detail/378659.html。
        client = Sample.create_client('accessKeyId', 'accessKeySecret')
        detect_image_codes_request = imm_20200930_models.DetectImageCodesRequest(
            project_name='test-project',
            source_uri='oss://test-bucket/test-object.jpg'
        )
        runtime = util_models.RuntimeOptions()
        try:
            # 复制代码运行请自行打印API的返回值。
            await client.detect_image_codes_with_options_async(detect_image_codes_request, runtime)
        except Exception as error:
            # 如有需要,请打印错误信息。
            UtilClient.assert_as_string(error.message)


if __name__ == '__main__':
    Sample.main(sys.argv[1:])
阿里云首页 智能媒体管理 相关技术圈