Detect QR codes and barcodes in images stored in OSS. The response returns each code's decoded text and its bounding box coordinates within the image.
This feature is powered by Intelligent Media Management (IMM) and supports synchronous processing only.
Use cases
Payment flows: Decode QR codes embedded in payment screenshots or receipts.
Marketing and advertising: Extract URLs or product information from QR codes on scanned posters or packaging images.
Content moderation: Identify the position of QR codes or barcodes in user-uploaded images to apply downstream redaction or blurring.
Limitations
Supports synchronous processing (
x-oss-process) only.Detects up to five codes per image.
Anonymous access is denied.
The bounding box uses a rectangular representation (Left, Top, Width, Height).
Prerequisites
Before you begin, ensure that you have:
Bound an IMM project to your bucket — via the OSS console or the IMM AttachOSSBucket API
Detect QR codes and barcodes
Use the image/codes action to detect all QR codes and barcodes in an image object stored in OSS.
Python
Requires OSS SDK for Python 2.18.4 or later.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Load access credentials from environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region where the bucket is located.
endpoint = 'https://oss-cn-hangzhou.aliyuncs.com'
# Specify the region where the bucket is located.
region = 'cn-hangzhou'
bucket = oss2.Bucket(auth, endpoint, 'examplebucket', region=region)
# Specify the full object path. Example: exampledir/example.jpg.
key = 'example.jpg'
try:
# Submit a QR code detection request.
result = bucket.get_object(key, process='image/codes')
print(result.read().decode('utf-8'))
except oss2.exceptions.OssError as e:
print("Error:", e)Java
Requires OSS SDK for Java 3.17.4 or later.
import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyuncs.exceptions.ClientException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class Demo {
public static void main(String[] args) throws ClientException {
// Specify the endpoint of the region where the bucket is located.
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the region where the bucket is located. Example: cn-hangzhou.
String region = "cn-hangzhou";
// Load access credentials from environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
EnvironmentVariableCredentialsProvider credentialsProvider =
CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// Specify the bucket name.
String bucketName = "examplebucket";
// Specify the full object path. Example: exampledir/example.jpg.
String key = "example.jpg";
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// Submit a QR code detection request.
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
getObjectRequest.setProcess("image/codes");
OSSObject ossObject = ossClient.getObject(getObjectRequest);
// Read the JSON response.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = ossObject.getObjectContent().read(buffer)) != -1) {
baos.write(buffer, 0, bytesRead);
}
System.out.println(baos.toString("UTF-8"));
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
} finally {
ossClient.shutdown();
}
}
}Go
Requires OSS SDK for Go 3.0.2 or later.
package main
import (
"fmt"
"io"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Load access credentials from environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSS client. Specify the endpoint and region where the bucket is located.
client, err := oss.New(
"https://oss-cn-hangzhou.aliyuncs.com", "", "",
oss.SetCredentialsProvider(&provider),
oss.AuthVersion(oss.AuthV4),
oss.Region("cn-hangzhou"),
)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
bucket, err := client.Bucket("examplebucket")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Submit a QR code detection request.
body, err := bucket.GetObject("example.jpg", oss.Process("image/codes"))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
defer body.Close()
data, err := io.ReadAll(body)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Println(string(data))
}PHP
Requires OSS SDK for PHP 2.7.0 or later.
<?php
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
try {
// Load access credentials from environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
$provider = new EnvironmentVariableCredentialsProvider();
// Specify the endpoint of the region where the bucket is located.
$endpoint = 'https://oss-cn-hangzhou.aliyuncs.com';
// Specify the bucket name.
$bucket = 'examplebucket';
// Specify the full object path. Example: exampledir/example.jpg.
$key = 'example.jpg';
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region" => "cn-hangzhou"
);
$ossClient = new OssClient($config);
// Submit a QR code detection request.
$options[$ossClient::OSS_PROCESS] = "image/codes";
$result = $ossClient->getObject($bucket, $key, $options);
var_dump($result);
} catch (OssException $e) {
printf($e->getMessage() . "\n");
}API reference
For direct API access, include the x-oss-process=image/codes parameter in a GetObject request. Calculate the Authorization header using signature version 4. For the full GetObject specification, see GetObject.
Sample request
GET /example.jpg?x-oss-process=image/codes HTTP/1.1
Host: image-demo.oss-cn-hangzhou.aliyuncs.com
Date: Fri, 21 Jul 2023 08:56:50 GMT
Authorization: SignatureValueSample response
HTTP/1.1 200 OK
Server: AliyunOSS
Date: Fri, 21 Jul 2023 08:56:52 GMT
Content-Type: application/json;charset=utf-8
Content-Length: 64
Connection: keep-alive
x-oss-request-id: 64BA48531253C5383707D5B3
ETag: "2CE2EA370531B7CC1D23BE6015CF5DA5"
Last-Modified: Mon, 10 Jul 2023 13:07:30 GMT
x-oss-object-type: Normal
x-oss-hash-crc64ecma: 13420962247653419692
x-oss-storage-class: Standard
x-oss-ec: 0048-00000104
Content-Disposition: attachment
x-oss-force-download: true
x-oss-server-time: 453
{
"RequestId": "3B7BD09F-18D8-56F0-90B7-889FBD9FFF70",
"Codes": [
{
"Content": "https://www.aliyun.com/product/imm",
"Boundary": {
"Width": 741,
"Height": 706,
"Left": 460,
"Top": 295
}
}
]
}Response fields
The Codes array contains one entry per detected code.
| Field | Type | Description |
|---|---|---|
Content | String | Decoded text or URL from the QR code or barcode |
Boundary.Left | Integer | X-coordinate of the upper-left corner, in pixels |
Boundary.Top | Integer | Y-coordinate of the upper-left corner, in pixels |
Boundary.Width | Integer | Width of the bounding box, in pixels |
Boundary.Height | Integer | Height of the bounding box, in pixels |
For the full response schema, see DetectImageCodes.
Permissions
For synchronous QR code recognition (x-oss-process=image/codes), grant the following permissions.
Permissions for the caller (RAM user or RAM role)
Grant the caller permissions using a RAM policy or bucket policy.
| API | Action | Description |
|---|---|---|
| GetObject | oss:GetObject | Read the source image from OSS |
| — | kms:Decrypt | Required only if the object is encrypted with KMS (X-Oss-Server-Side-Encryption: KMS) |
| — | oss:ProcessImm | Invoke IMM data processing from OSS |
| PostProcessTask | oss:PostProcessTask | Required only for asynchronous processing (x-oss-async-process) |
Permissions for the IMM service role
IMM calls OSS on your behalf using the default service role AliyunIMMDefaultRole (ARN: acs:ram:*:<account-id>:role/aliyunimmdefaultrole). Grant this role the following permissions.
| API | Action | Description |
|---|---|---|
| GetObject | oss:GetObject | Read the source image |
| — | kms:Decrypt | Required only if the object is encrypted with KMS |
Permissions for IMM
| API | Action | Description |
|---|---|---|
| DetectImageCodes | imm:DetectImageCodes | Run QR code recognition in IMM |
Billing
QR code recognition generates charges on both OSS and IMM. Unit prices vary by region.
OSS charges — see OSS pricing for current rates.
| Billing item | Basis |
|---|---|
| GET requests | Per successful request |
| Outbound traffic | Data size, when accessed via a public endpoint (e.g., oss-cn-hangzhou.aliyuncs.com) or an acceleration endpoint |
| IA data retrieval | Size of retrieved IA objects |
| Archive data retrieval | Size of retrieved Archive objects, when real-time access is enabled on the bucket |
| Transfer acceleration | Data size, when transfer acceleration is enabled |
IMM charges — see IMM billing for current rates.
| Billing item | Basis |
|---|---|
| ImageQRCodes | Per successful DetectImageCodes request |