QR code recognition
This document provides sample code for QR code recognition in various programming languages and for common use cases.
-
For real-time assistance, start an online consultation.
-
If you have questions about API access or usage for the Alibaba Cloud Vision AI Platform, join our DingTalk group (ID: 23109592) to contact us.
Overview
For a functional overview and detailed parameter descriptions for QR code recognition, see QR code recognition.
Install the SDK package
For information about the SDK dependencies for common languages, see Overview of SDKs.
Configure environment variables
Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.
An Alibaba Cloud account has full access to all API operations. We recommend that you use a RAM user for API calls or routine O&M. For more information, see Create a RAM user.
Do not save your AccessKey ID or AccessKey Secret in project code. Otherwise, the AccessKey pair may be leaked and the security of all resources in your account may be compromised.
Configure environment variables on Linux and macOS
Open a terminal in IntelliJ IDEA.
Run the following commands to configure the environment variables.
Replace
<access_key_id>with the AccessKey ID of your RAM user and<access_key_secret>with the AccessKey Secret of your RAM user. If you need to configure more permissions, see Control access permissions using a RAM policy.export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id> export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
Configure environment variables on Windows
Create a new environment variable file, add the environment variables
ALIBABA_CLOUD_ACCESS_KEY_IDandALIBABA_CLOUD_ACCESS_KEY_SECRET, and set them to the AccessKey ID and AccessKey Secret that you have prepared. Then, restart the Windows operating system. The following example uses Windows 10.Open File Explorer, right-click This PC, and then select Properties.
In the left-side navigation pane, click Advanced system settings.
On the Advanced tab of the System Properties dialog box, click Environment Variables.
In the Environment Variables dialog box, click New.
In the New System Variable dialog box, add the environment variables
ALIBABA_CLOUD_ACCESS_KEY_IDandALIBABA_CLOUD_ACCESS_KEY_SECRET, and set them to the AccessKey ID and AccessKey Secret that you have prepared.Restart the Windows operating system for the configurations to take effect.
Sample code
OSS file in the China (Shanghai) region
/*
Add the dependency.
<!-- https://mvnrepository.com/artifact/com.aliyun/ocr20191230 -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>ocr20191230</artifactId>
<version>${aliyun.ocr.version}</version>
</dependency>
*/
import com.aliyun.ocr20191230.models.RecognizeQrCodeResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaModel;
public class RecognizeQrCode {
public static com.aliyun.ocr20191230.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
/*
Initialize the configuration object. This object stores configurations such as your AccessKey ID, AccessKey Secret, and endpoint.
*/
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
.setAccessKeyId(accessKeyId)
.setAccessKeySecret(accessKeySecret);
// The API endpoint.
config.endpoint = "ocr.cn-shanghai.aliyuncs.com";
return new com.aliyun.ocr20191230.Client(config);
}
public static void main(String[] args_) throws Exception {
// For information about how to create an AccessKey pair, see https://help.aliyun.com/document_detail/175144.html.
// If you use an AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before running the sample code.
String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
com.aliyun.ocr20191230.Client client = RecognizeQrCode.createClient(accessKeyId, accessKeySecret);
com.aliyun.ocr20191230.models.RecognizeQrCodeRequest.RecognizeQrCodeRequestTasks tasks0 = new com.aliyun.ocr20191230.models.RecognizeQrCodeRequest.RecognizeQrCodeRequestTasks()
.setImageURL("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/ocr/RecognizeQrCode/RecognizeQrCode1.jpg");
com.aliyun.ocr20191230.models.RecognizeQrCodeRequest recognizeQrCodeRequest = new com.aliyun.ocr20191230.models.RecognizeQrCodeRequest()
.setTasks(java.util.Arrays.asList(
tasks0
));
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// Call the API and print the response.
RecognizeQrCodeResponse response = client.recognizeQrCodeWithOptions(recognizeQrCodeRequest, runtime);
System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(response)));
} catch (TeaException error) {
// Catch and print the error message.
System.out.println(com.aliyun.teautil.Common.toJSONString(error));
// Print the error code.
System.out.println(error.getCode());
}
}
}
# -*- coding: utf-8 -*-
# Install the dependency.
# pip install alibabacloud_ocr20191230
import os
from alibabacloud_ocr20191230.client import Client
from alibabacloud_ocr20191230.models import RecognizeQrCodeRequestTasks, RecognizeQrCodeRequest
from alibabacloud_tea_openapi.models import Config
from alibabacloud_tea_util.models import RuntimeOptions
config = Config(
# For information about how to create an AccessKey pair, see https://help.aliyun.com/document_detail/175144.html.
# If you use an AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
# Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before running the sample code.
access_key_id=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),
access_key_secret=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),
# The API endpoint.
endpoint='ocr.cn-shanghai.aliyuncs.com',
# The ID of the region that corresponds to the endpoint.
region_id='cn-shanghai'
)
tasks_0 = RecognizeQrCodeRequestTasks(
image_url='http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/ocr/RecognizeQrCode/RecognizeQrCode3.jpg'
)
tasks_1 = RecognizeQrCodeRequestTasks(
image_url='http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/ocr/RecognizeQrCode/RecognizeQrCode4.jpg'
)
recognize_qr_code_request = RecognizeQrCodeRequest(
tasks=[
tasks_0,
tasks_1
]
)
runtime = RuntimeOptions()
try:
# Initialize the client.
client = Client(config)
response = client.recognize_qr_code_with_options(recognize_qr_code_request, runtime)
# Print the full response.
print(response.body)
except Exception as error:
# Catch and print the error message.
print(error)
# Print the error code.
print(error.code)
# Tip: You can view attribute names by using error.__dict__.
<?php
// Install the dependency.
//composer require alibabacloud/ocr-20191230
use AlibabaCloud\SDK\Ocr\V20191230\Ocr;
use AlibabaCloud\Tea\Utils\Utils;
use \Exception;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Ocr\V20191230\Models\RecognizeQrCodeRequest\tasks;
use AlibabaCloud\SDK\Ocr\V20191230\Models\RecognizeQrCodeRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
class RecognizeQrCode {
/**
* Initializes the client by using an AccessKey pair.
* @param string $accessKeyId
* @param string $accessKeySecret
* @return Ocr Client
*/
public static function createClient($accessKeyId, $accessKeySecret){
// Initialize the configuration object. This object stores configurations such as your AccessKey ID, AccessKey Secret, and endpoint.
$config = new Config([
"accessKeyId" => $accessKeyId,
"accessKeySecret" => $accessKeySecret
]);
// The API endpoint.
$config->endpoint = "ocr.cn-shanghai.aliyuncs.com";
return new Ocr($config);
}
/**
* @param string[] $args
* @return void
*/
public static function main($args){
// For information about how to create an AccessKey pair, see https://help.aliyun.com/document_detail/175144.html.
// If you use an AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before running the sample code.
$accessKeyId = getenv('ALIBABA_CLOUD_ACCESS_KEY_ID');
$accessKeySecret = getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET');
$client = self::createClient($accessKeyId, $accessKeySecret);
$tasks0 = new tasks([
"imageURL" => "http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/ocr/RecognizeQrCode/RecognizeQrCode3.jpg"
]);
$tasks1 = new tasks([
"imageURL" => "http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/ocr/RecognizeQrCode/RecognizeQrCode4.jpg"
]);
$recognizeQrCodeRequest = new RecognizeQrCodeRequest([
"tasks" => [
$tasks0,
$tasks1
]
]);
$runtime = new RuntimeOptions([]);
try {
$resp = $client->recognizeQrCodeWithOptions($recognizeQrCodeRequest, $runtime);
# Print the full response.
echo Utils::toJSONString($resp->body);
} catch (Exception $exception) {
# Catch and print the error message.
echo Utils::toJSONString($exception);
# Print the error code.
echo $exception->getCode();
}
}
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
require_once $path;
}
// The $argv parameter is a reserved array for input arguments and must not be modified.
RecognizeQrCode::main(array_slice($argv, 1));
// Install the dependency.
// npm install @alicloud/ocr20191230
// Import the SDK.
const OcrClient = require('@alicloud/ocr20191230');
const OpenapiClient = require('@alicloud/openapi-client');
const TeaUtil = require('@alicloud/tea-util');
let config = new OpenapiClient.Config({
// For information about how to create an AccessKey pair, see https://help.aliyun.com/document_detail/175144.html.
// If you use an AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before running the sample code.
accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET
});
// The API endpoint.
config.endpoint = `ocr.cn-shanghai.aliyuncs.com`;
const client = new OcrClient.default(config);
let tasks0 = new OcrClient.RecognizeQrCodeRequestTasks({
imageURL: "http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/ocr/RecognizeQrCode/RecognizeQrCode3.jpg",
});
let tasks1 = new OcrClient.RecognizeQrCodeRequestTasks({
imageURL: "http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/ocr/RecognizeQrCode/RecognizeQrCode4.jpg",
});
let recognizeQrCodeRequest = new OcrClient.RecognizeQrCodeRequest({
tasks: [
tasks0,
tasks1
],
});
let runtime = new TeaUtil.RuntimeOptions({ });
client.recognizeQrCodeWithOptions(recognizeQrCodeRequest, runtime)
.then(function(recognizeQrCodeResponse) {
// Print the full response.
console.log(recognizeQrCodeResponse);
// Print a specific field from the response.
console.log(recognizeQrCodeResponse.body.data);
}, function(error) {
// Catch and print the error message.
console.log(error);
// Print the error code from the error data.
console.log(error.data.Code);
})
/**
This code depends on github.com/alibabacloud-go/ocr-20191230/v3.
We recommend running `go mod tidy` to install the dependency.
*/
package main
import (
"fmt"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
ocr20191230 "github.com/alibabacloud-go/ocr-20191230/v3/client"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
)
func main() {
// For information about how to create an AccessKey pair, see https://help.aliyun.com/document_detail/175144.html.
// If you use an AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before running the sample code.
accessKeyId := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
accessKeySecret := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
// Initialize the configuration object. This object stores configurations such as your AccessKey ID, AccessKey Secret, and endpoint.
config := &openapi.Config{
AccessKeyId: tea.String(accessKeyId),
AccessKeySecret: tea.String(accessKeySecret)
}
// The API endpoint.
config.Endpoint = tea.String("ocr.cn-shanghai.aliyuncs.com")
client, err := ocr20191230.NewClient(config)
if err != nil {
fmt.Println(err.Error())
}
tasks0 := &ocr20191230.RecognizeQrCodeRequestTasks{
ImageURL: tea.String("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/ocr/RecognizeQrCode/RecognizeQrCode1.jpg"),
}
recognizeQrCodeRequest := &ocr20191230.RecognizeQrCodeRequest{
Tasks: []*ocr20191230.RecognizeQrCodeRequestTasks{tasks0},
}
runtime := &util.RuntimeOptions{}
recognizeQrCodeResponse, _err := client.RecognizeQrCodeWithOptions(recognizeQrCodeRequest, runtime)
if _err != nil {
fmt.Println(_err.Error())
} else {
// Print the full response.
fmt.Println(recognizeQrCodeResponse)
// Print a specific field from the response.
fmt.Println(recognizeQrCodeResponse.Body.Data)
}
}
// Install the dependency.
// dotnet add package AlibabaCloud.SDK.Ocr20191230
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using AlibabaCloud.SDK.Ocr20191230.Models;
using Tea;
using Tea.Utils;
namespace AlibabaCloud.SDK.Sample
{
public class Sample
{
/**
* Initializes the client by using an AccessKey pair.
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static AlibabaCloud.SDK.Ocr20191230.Client CreateClient(string accessKeyId, string accessKeySecret)
{
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
{
AccessKeyId = accessKeyId,
AccessKeySecret = accessKeySecret,
};
// The API endpoint.
config.Endpoint = "ocr.cn-shanghai.aliyuncs.com";
return new AlibabaCloud.SDK.Ocr20191230.Client(config);
}
public static void Main(string[] args)
{
// For information about how to create an AccessKey pair, see https://help.aliyun.com/document_detail/175144.html.
// If you use an AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before running the sample code.
AlibabaCloud.SDK.Ocr20191230.Client client = CreateClient(System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
AlibabaCloud.SDK.Ocr20191230.Models.RecognizeQrCodeRequest.RecognizeQrCodeRequestTasks tasks0 = new AlibabaCloud.SDK.Ocr20191230.Models.RecognizeQrCodeRequest.RecognizeQrCodeRequestTasks
{
ImageURL = "http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/ocr/RecognizeQrCode/RecognizeQrCode3.jpg",
};
AlibabaCloud.SDK.Ocr20191230.Models.RecognizeQrCodeRequest.RecognizeQrCodeRequestTasks tasks1 = new AlibabaCloud.SDK.Ocr20191230.Models.RecognizeQrCodeRequest.RecognizeQrCodeRequestTasks
{
ImageURL = "http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/ocr/RecognizeQrCode/RecognizeQrCode4.jpg",
};
AlibabaCloud.SDK.Ocr20191230.Models.RecognizeQrCodeRequest recognizeQrCodeRequest = new AlibabaCloud.SDK.Ocr20191230.Models.RecognizeQrCodeRequest
{
Tasks = new List<AlibabaCloud.SDK.Ocr20191230.Models.RecognizeQrCodeRequest.RecognizeQrCodeRequestTasks>
{
tasks0,
tasks1
},
};
AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
try
{
AlibabaCloud.SDK.Ocr20191230.Models.RecognizeQrCodeResponse recognizeQrCodeResponse = client.RecognizeQrCodeWithOptions(recognizeQrCodeRequest, runtime);
// Print the full response.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(recognizeQrCodeResponse.Body));
// Print a specific field from the response.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(recognizeQrCodeResponse.Body.Data));
}
catch (TeaException error)
{
// Print the error message.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message));
}
catch (Exception _error)
{
TeaException error = new TeaException(new Dictionary<string, object>
{
{ "message", _error.Message }
});
// Print the error message.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message));
}
}
}
}
Local file or public URL
/*
Add the dependency.
<!-- https://mvnrepository.com/artifact/com.aliyun/ocr20191230 -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>ocr20191230</artifactId>
<version>${aliyun.ocr.version}</version>
</dependency>
*/
import com.aliyun.ocr20191230.models.RecognizeQrCodeResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaModel;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
public class RecognizeQrCode {
public static com.aliyun.ocr20191230.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
/*
Initialize the configuration object. This object stores configurations such as your AccessKey ID, AccessKey Secret, and endpoint.
*/
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
.setAccessKeyId(accessKeyId)
.setAccessKeySecret(accessKeySecret);
// The API endpoint.
config.endpoint = "ocr.cn-shanghai.aliyuncs.com";
return new com.aliyun.ocr20191230.Client(config);
}
public static void main(String[] args) throws Exception {
// For information about how to create an AccessKey pair, see https://help.aliyun.com/document_detail/175144.html.
// If you use an AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before running the sample code.
String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
com.aliyun.ocr20191230.Client client = RecognizeQrCode.createClient(accessKeyId, accessKeySecret);
// Scenario 1: Use a local file.
InputStream inputStream0 = new FileInputStream(new File("/tmp/RecognizeQrCode1.jpg"));
com.aliyun.ocr20191230.models.RecognizeQrCodeAdvanceRequest.RecognizeQrCodeAdvanceRequestTasks tasks0 = new com.aliyun.ocr20191230.models.RecognizeQrCodeAdvanceRequest.RecognizeQrCodeAdvanceRequestTasks()
.setImageURLObject(inputStream0);
// Scenario 2: Use a publicly accessible URL.
URL url = new URL("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/ocr/RecognizeQrCode/RecognizeQrCode1.jpg");
InputStream inputStream1 = url.openConnection().getInputStream();
com.aliyun.ocr20191230.models.RecognizeQrCodeAdvanceRequest.RecognizeQrCodeAdvanceRequestTasks tasks1 = new com.aliyun.ocr20191230.models.RecognizeQrCodeAdvanceRequest.RecognizeQrCodeAdvanceRequestTasks()
.setImageURLObject(inputStream1);
com.aliyun.ocr20191230.models.RecognizeQrCodeAdvanceRequest recognizeQrCodeAdvanceRequest = new com.aliyun.ocr20191230.models.RecognizeQrCodeAdvanceRequest()
.setTasks(java.util.Arrays.asList(
tasks0, tasks1
));
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// Call the API and print the response.
RecognizeQrCodeResponse response = client.recognizeQrCodeAdvance(recognizeQrCodeAdvanceRequest, runtime);
System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(response)));
} catch (TeaException error) {
// Catch and print the error message.
System.out.println(com.aliyun.teautil.Common.toJSONString(error));
// Print the error code.
System.out.println(error.getCode());
}
}
}
# -*- coding: utf-8 -*-
# Install the dependency.
# pip install alibabacloud_ocr20191230
import os
import io
from urllib.request import urlopen
from alibabacloud_ocr20191230.client import Client
from alibabacloud_ocr20191230.models import RecognizeQrCodeAdvanceRequestTasks, RecognizeQrCodeAdvanceRequest
from alibabacloud_tea_openapi.models import Config
from alibabacloud_tea_util.models import RuntimeOptions
config = Config(
# For information about how to create an AccessKey pair, see https://help.aliyun.com/document_detail/175144.html.
# If you use an AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
# Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before running the sample code.
access_key_id=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),
access_key_secret=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),
# The API endpoint.
endpoint='ocr.cn-shanghai.aliyuncs.com',
# The ID of the region that corresponds to the endpoint.
region_id='cn-shanghai'
)
# Scenario 1: Use a local file.
stream0 = open(r'/tmp/RecognizeQrCode3.jpg', 'rb')
tasks_0 = RecognizeQrCodeAdvanceRequestTasks()
tasks_0.image_urlobject = stream0
# Scenario 2: Use a publicly accessible URL.
url1 = 'http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/ocr/RecognizeQrCode/RecognizeQrCode1.jpg'
img1 = urlopen(url1).read()
tasks_1 = RecognizeQrCodeAdvanceRequestTasks()
tasks_1.image_urlobject = io.BytesIO(img1)
recognize_qr_code_request = RecognizeQrCodeAdvanceRequest(
tasks=[
tasks_0,
tasks_1
]
)
runtime = RuntimeOptions()
try:
# Initialize the client.
client = Client(config)
response = client.recognize_qr_code_advance(recognize_qr_code_request, runtime)
# Print the full response.
print(response.body)
except Exception as error:
# Catch and print the error message.
print(error)
# Print the error code.
print(error.code)
# Tip: You can view attribute names by using error.__dict__.
<?php
// Install the dependency.
//composer require alibabacloud/ocr-20191230
use AlibabaCloud\SDK\Ocr\V20191230\Ocr;
use AlibabaCloud\Tea\Utils\Utils;
use \Exception;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Ocr\V20191230\Models\RecognizeQrCodeAdvanceRequest\tasks;
use AlibabaCloud\SDK\Ocr\V20191230\Models\RecognizeQrCodeAdvanceRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use GuzzleHttp\Psr7\Stream;
class RecognizeQrCodeAdvance {
/**
* Initializes the client by using an AccessKey pair.
* @param string $accessKeyId
* @param string $accessKeySecret
* @return Ocr Client
*/
public static function createClient($accessKeyId, $accessKeySecret){
// Initialize the configuration object. This object stores configurations such as your AccessKey ID, AccessKey Secret, and endpoint.
$config = new Config([
"accessKeyId" => $accessKeyId,
"accessKeySecret" => $accessKeySecret
]);
// The API endpoint.
$config->endpoint = "ocr.cn-shanghai.aliyuncs.com";
return new Ocr($config);
}
/**
* @param string[] $args
* @return void
*/
public static function main($args){
// For information about how to create an AccessKey pair, see https://help.aliyun.com/document_detail/175144.html.
// If you use an AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before running the sample code.
$accessKeyId = getenv('ALIBABA_CLOUD_ACCESS_KEY_ID');
$accessKeySecret = getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET');
$client = self::createClient($accessKeyId, $accessKeySecret);
// Scenario 1: Use a local file.
$file0 = fopen('/tmp/RecognizeQrCode3.jpg', 'rb');
$stream0 = new Stream($file0);
// Scenario 2: Use a publicly accessible URL.
$file1 = fopen('http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/ocr/RecognizeQrCode/RecognizeQrCode1.jpg', 'rb');
$stream1 = new Stream($file1);
$tasks0 = new tasks([
"imageURLObject" => $stream0
]);
$tasks1 = new tasks([
"imageURLObject" => $stream1
]);
$recognizeQrCodeAdvanceRequest = new RecognizeQrCodeAdvanceRequest([
"tasks" => [
$tasks0,
$tasks1
]
]);
$runtime = new RuntimeOptions([]);
try {
$resp = $client->recognizeQrCodeAdvance($recognizeQrCodeAdvanceRequest, $runtime);
# Print the full response.
echo Utils::toJSONString($resp->body);
} catch (Exception $exception) {
# Catch and print the error message.
echo Utils::toJSONString($exception);
# Print the error code.
echo $exception->getCode();
}
}
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
require_once $path;
}
// The $argv parameter is a reserved array for input arguments and must not be modified.
RecognizeQrCodeAdvance::main(array_slice($argv, 1));
// Install the dependency.
// npm install @alicloud/ocr20191230
const OcrClient = require('@alicloud/ocr20191230');
const OpenapiClient = require('@alicloud/openapi-client');
const TeaUtil = require('@alicloud/tea-util');
const fs = require('fs');
const http = require('http');
const https = require('https');
let config = new OpenapiClient.Config({
// For information about how to create an AccessKey pair, see https://help.aliyun.com/document_detail/175144.html.
// If you use an AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before running the sample code.
accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET
});
// The API endpoint.
config.endpoint = `ocr.cn-shanghai.aliyuncs.com`;
const client = new OcrClient.default(config);
const getResponse = function (httpClient, url) {
return new Promise((resolve, reject) => {
httpClient.get(url, function (response) {
resolve(response);
})
})
}
const request = async function () {
try {
// Scenario 1: Use a local file.
const fileStream = fs.createReadStream('/tmp/RecognizeQrCode3.jpg');
let tasks0 = new OcrClient.RecognizeQrCodeAdvanceRequestTasks();
tasks0.imageURLObject = fileStream;
// Scenario 2: Use a publicly accessible URL.
const url = new URL("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/ocr/RecognizeQrCode/RecognizeQrCode1.jpg");
const httpClient = (url.protocol == "https:") ? https : http;
let tasks1 = new OcrClient.RecognizeQrCodeAdvanceRequestTasks();
tasks1.imageURLObject = await getResponse(httpClient, url);
let recognizeQrCodeAdvanceRequest = new OcrClient.RecognizeQrCodeAdvanceRequest({
tasks: [
tasks0,
tasks1
],
});
let runtime = new TeaUtil.RuntimeOptions({});
client.recognizeQrCodeAdvance(recognizeQrCodeAdvanceRequest, runtime)
.then(function (recognizeQrCodeResponse) {
// Print the full response.
console.log(recognizeQrCodeResponse);
// Print a specific field from the response.
console.log(recognizeQrCodeResponse.body.data);
}, function (error) {
// Catch and print the error message.
console.log(error);
// Print the error code from the error data.
console.log(error.data.Code);
})
} catch (error) {
console.log(error);
}
}();
/**
This code depends on github.com/alibabacloud-go/ocr-20191230/v3.
We recommend running `go mod tidy` to install the dependency.
*/
package main
import (
"fmt"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
ocr20191230 "github.com/alibabacloud-go/ocr-20191230/v3/client"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
"net/http"
"os"
)
func main() {
// For information about how to create an AccessKey pair, see https://help.aliyun.com/document_detail/175144.html.
// If you use an AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before running the sample code.
accessKeyId := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
accessKeySecret := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
// Initialize the configuration object. This object stores configurations such as your AccessKey ID, AccessKey Secret, and endpoint.
config := &openapi.Config{
AccessKeyId: tea.String(accessKeyId),
AccessKeySecret: tea.String(accessKeySecret)
}
// The API endpoint.
config.Endpoint = tea.String("ocr.cn-shanghai.aliyuncs.com")
client, err := ocr20191230.NewClient(config)
if err != nil {
fmt.Println(err.Error())
}
// Scenario 1: Use a local file.
file, err := os.Open("/tmp/RecognizeQrCode1.jpg")
if err != nil {
fmt.Println("cannot open file", err)
}
tasks0 := &ocr20191230.RecognizeQrCodeAdvanceRequestTasks{
ImageURLObject: file,
}
// Scenario 2: Use a publicly accessible URL.
httpClient := http.Client{}
file1, _ := httpClient.Get("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/ocr/RecognizeQrCode/RecognizeQrCode1.jpg")
tasks1 := &ocr20191230.RecognizeQrCodeAdvanceRequestTasks{
ImageURLObject: file1.Body,
}
recognizeQrCodeAdvanceRequest := &ocr20191230.RecognizeQrCodeAdvanceRequest{
Tasks: []*ocr20191230.RecognizeQrCodeAdvanceRequestTasks{tasks0, tasks1},
}
runtime := &util.RuntimeOptions{}
recognizeQrCodeResponse, _err := client.RecognizeQrCodeAdvance(recognizeQrCodeAdvanceRequest, runtime)
if _err != nil {
fmt.Println(_err.Error())
} else {
// Print the full response.
fmt.Println(recognizeQrCodeResponse)
// Print a specific field from the response.
fmt.Println(recognizeQrCodeResponse.Body.Data)
}
}
// Install the dependency.
// dotnet add package AlibabaCloud.SDK.Ocr20191230
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using AlibabaCloud.SDK.Ocr20191230.Models;
using Tea;
using Tea.Utils;
namespace AlibabaCloud.SDK.Sample
{
public class Sample
{
/**
* Initializes the client by using an AccessKey pair.
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static AlibabaCloud.SDK.Ocr20191230.Client CreateClient(string accessKeyId, string accessKeySecret)
{
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
{
AccessKeyId = accessKeyId,
AccessKeySecret = accessKeySecret,
};
// The API endpoint.
config.Endpoint = "ocr.cn-shanghai.aliyuncs.com";
return new AlibabaCloud.SDK.Ocr20191230.Client(config);
}
public static void Main(string[] args)
{
// For information about how to create an AccessKey pair, see https://help.aliyun.com/document_detail/175144.html.
// If you use an AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before running the sample code.
AlibabaCloud.SDK.Ocr20191230.Client client = CreateClient(System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
AlibabaCloud.SDK.Ocr20191230.Models.RecognizeQrCodeAdvanceRequest.RecognizeQrCodeAdvanceRequestTasks tasks0 = new AlibabaCloud.SDK.Ocr20191230.Models.RecognizeQrCodeAdvanceRequest.RecognizeQrCodeAdvanceRequestTasks
();
AlibabaCloud.SDK.Ocr20191230.Models.RecognizeQrCodeAdvanceRequest.RecognizeQrCodeAdvanceRequestTasks tasks1 = new AlibabaCloud.SDK.Ocr20191230.Models.RecognizeQrCodeAdvanceRequest.RecognizeQrCodeAdvanceRequestTasks
();
// Scenario 1: Use a local file.
System.IO.StreamReader file = new System.IO.StreamReader(@"/tmp/RecognizeQrCode3.jpg");
tasks0.ImageURLObject = file.BaseStream;
// Scenario 2: Use a publicly accessible URL.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/ocr/RecognizeQrCode/RecognizeQrCode1.jpg");
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
tasks1.ImageURLObject = stream;
AlibabaCloud.SDK.Ocr20191230.Models.RecognizeQrCodeAdvanceRequest recognizeQrCodeAdvanceRequest = new AlibabaCloud.SDK.Ocr20191230.Models.RecognizeQrCodeAdvanceRequest
{
Tasks = new List<AlibabaCloud.SDK.Ocr20191230.Models.RecognizeQrCodeAdvanceRequest.RecognizeQrCodeAdvanceRequestTasks>
{
tasks0,
tasks1
},
};
AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
try
{
AlibabaCloud.SDK.Ocr20191230.Models.RecognizeQrCodeResponse recognizeQrCodeResponse = client.RecognizeQrCodeAdvance(recognizeQrCodeAdvanceRequest, runtime);
// Print the full response.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(recognizeQrCodeResponse.Body));
// Print a specific field from the response.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(recognizeQrCodeResponse.Body.Data));
}
catch (TeaException error)
{
// Print the error message.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message));
}
catch (Exception _error)
{
TeaException error = new TeaException(new Dictionary<string, object>
{
{ "message", _error.Message }
});
// Print the error message.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message));
}
}
}
}