This topic describes how to use the QR code recognition feature.
Scenarios
Pay with QR codes: Payers can scan a QR code to complete payments.
Marketing and advertising with QR codes: Marketers and advertisers can add QR codes to posters and product packaging to promote products.
Overview
Intelligent Media Management (IMM) provides the QR code recognition feature that you can use to detect positions and content of one or more QR codes or barcodes from image files such as photos and screenshots and return the position information and text information that the codes convey. The position information contains the upper-left corner x-axis, upper-left corner y-axis, width, and height, as shown in the following figure.
QR code

Barcode

You can use the QR code recognition feature to implement QR code or barcode scanning and reading in your business applications. You can also develop features to block or pixelate QR codes or barcodes in images based on the QR code recognition feature.
Prerequisites
An AccessKey pair is created and obtained. For more information, see Create an AccessKey pair.
Object Storage Service (OSS) is activated, a bucket is created, and objects are uploaded to the bucket. For more information, see Upload objects.
IMM is activated. For more information, see Activate IMM.
A project is created in the IMM console. For more information, see Create a project.
NoteYou can call the CreateProject operation to create a project. For more information, see CreateProject.
You can call the ListProjects operation to query the existing projects in a specific region. For more information, see ListProjects.
Usage
Call the DetectImageCodes operation to detect barcodes from an image.
QR code detection allows you to detect up to five QR codes from an image.
QR code detection example
Image information
IMM project: test-project
Image storage location: oss://test-bucket/test-object1.jpg
Sample image

Sample request
{
"ProjectName": "test-project",
"SourceURI": "oss://test-bucket/test-object1.jpg",
}Sample response
{
"Codes": [
{
"Type": "qrcode",
"Content": "https://www.aliyun.com/product/imm",
"Boundary": {
"Left": 447,
"Top": 288,
"Height": 720,
"Width": 767
}
}
],
"RequestId": "9692C8B4-2AAF-55DE-91A0-B0B53725FBAB"
}The preceding sample response indicates that the image contains a QR code.
Sample code
The following sample code provides an example on how to use IMM SDK for Python to detect QR codes from an image.
# -*- coding: utf-8 -*-
# This file is auto-generated, don't edit it. Thanks.
import sys
import os
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:
"""
Use your AccessKey ID and AccessKey secret to initialize the client.
@param access_key_id:
@param access_key_secret:
@return: Client
@throws Exception
"""
config = open_api_models.Config(
access_key_id=access_key_id,
access_key_secret=access_key_secret
)
# Specify the IMM endpoint.
config.endpoint = f'imm.cn-beijing.aliyuncs.com'
return imm20200930Client(config)
@staticmethod
def main(
args: List[str],
) -> None:
# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. To prevent security risks, we recommend that you call API operations or perform routine O&M as a RAM user.
# We recommend that you do not include your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised.
# In this example, the AccessKey pair is read from the environment variables to implement identity verification for API access. For information about how to configure environment variables, see https://www.alibabacloud.com/help/document_detail/2361894.html.
imm_access_key_id = os.getenv("AccessKeyId")
imm_access_key_secret = os.getenv("AccessKeySecret")
# Initialize the client.
client = Sample.create_client(imm_access_key_id, imm_access_key_secret)
detect_image_codes_request = imm_20200930_models.DetectImageCodesRequest(
project_name='test-project',
source_uri='oss://test-bucket/test-object1.jpg'
)
runtime = util_models.RuntimeOptions()
try:
# You can choose to print the response of the API operation.
client.detect_image_codes_with_options(detect_image_codes_request, runtime)
except Exception as error:
# Print the error message if necessary.
UtilClient.assert_as_string(error.message)
@staticmethod
async def main_async(
args: List[str],
) -> None:
# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. To prevent security risks, we recommend that you call API operations or perform routine O&M as a RAM user.
# We recommend that you do not include your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised.
# In this example, the AccessKey pair is read from the environment variables to implement identity verification for API access. For information about how to configure environment variables, see https://www.alibabacloud.com/help/document_detail/2361894.html.
imm_access_key_id = os.getenv("AccessKeyId")
imm_access_key_secret = os.getenv("AccessKeySecret")
# Initialize the client.
client = Sample.create_client(imm_access_key_id, imm_access_key_secret)
detect_image_codes_request = imm_20200930_models.DetectImageCodesRequest(
project_name='test-project',
source_uri='oss://test-bucket/test-object1.jpg'
)
runtime = util_models.RuntimeOptions()
try:
# You can choose to print the response of the API operation.
await client.detect_image_codes_with_options_async(detect_image_codes_request, runtime)
except Exception as error:
# Print the error message if necessary.
UtilClient.assert_as_string(error.message)
if __name__ == '__main__':
Sample.main(sys.argv[1:])Barcode detection example
Image information
IMM project: test-project
Image storage location: oss://test-bucket/test-object2.jpg
Sample image

Sample request
{
"ProjectName": "test-project",
"SourceURI": "oss://test-bucket/test-object2.jpg",
}Sample response
{
"Codes": [
{
"Type": "barcode",
"Confidence": 0.433,
"Content": "",
"Boundary": {
"Left": 162,
"Top": 393,
"Height": 86,
"Width": 78
}
}
],
"RequestId": "391A3AEC-FF64-5450-96DF-18DBDC234B2F"
}The preceding sample response indicates that the image contains a barcode
Sample code
# -*- coding: utf-8 -*-
# This file is auto-generated, don't edit it. Thanks.
import sys
import os
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:
"""
Use your AccessKey ID and AccessKey secret to initialize the client.
@param access_key_id:
@param access_key_secret:
@return: Client
@throws Exception
"""
config = open_api_models.Config(
access_key_id=access_key_id,
access_key_secret=access_key_secret
)
# Specify the IMM endpoint.
config.endpoint = f'imm.cn-beijing.aliyuncs.com'
return imm20200930Client(config)
@staticmethod
def main(
args: List[str],
) -> None:
# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. To prevent security risks, we recommend that you call API operations or perform routine O&M as a RAM user.
# We recommend that you do not include your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised.
# In this example, the AccessKey pair is read from the environment variables to implement identity verification for API access. For information about how to configure environment variables, see https://www.alibabacloud.com/help/document_detail/2361894.html.
imm_access_key_id = os.getenv("AccessKeyId")
imm_access_key_secret = os.getenv("AccessKeySecret")
# Initialize the client.
client = Sample.create_client(imm_access_key_id, imm_access_key_secret)
detect_image_codes_request = imm_20200930_models.DetectImageCodesRequest(
project_name='test-project',
source_uri='oss://test-bucket/test-object2.jpg'
)
runtime = util_models.RuntimeOptions()
try:
# You can choose to print the response of the API operation.
client.detect_image_codes_with_options(detect_image_codes_request, runtime)
except Exception as error:
# Print the error message if necessary.
UtilClient.assert_as_string(error.message)
@staticmethod
async def main_async(
args: List[str],
) -> None:
# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. To prevent security risks, we recommend that you call API operations or perform routine O&M as a RAM user.
# We recommend that you do not include your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised.
# In this example, the AccessKey pair is read from the environment variables to implement identity verification for API access. For information about how to configure environment variables, see https://www.alibabacloud.com/help/document_detail/2361894.html.
imm_access_key_id = os.getenv("AccessKeyId")
imm_access_key_secret = os.getenv("AccessKeySecret")
# Initialize the client.
client = Sample.create_client(imm_access_key_id, imm_access_key_secret)
detect_image_codes_request = imm_20200930_models.DetectImageCodesRequest(
project_name='test-project',
source_uri='oss://test-bucket/test-object2.jpg'
)
runtime = util_models.RuntimeOptions()
try:
# You can choose to print the response of the API operation.
await client.detect_image_codes_with_options_async(detect_image_codes_request, runtime)
except Exception as error:
# Print the error message if necessary.
UtilClient.assert_as_string(error.message)
if __name__ == '__main__':
Sample.main(sys.argv[1:])