有奖捉虫:办公协同&微信生态&物联网文档专题 HOT

简介

本文档提供关于图片二维码相关的 API 概览以及 SDK 示例代码。
API
说明
二维码识别功能可识别图片中有效二维码的位置及内容,输出图像中二维码包含的文本信息(每个二维码对应的 URL 或文本),并可对识别出的二维码添加马赛克
二维码生成功能可根据用户指定的文本信息(URL 或文本),生成对应的二维码或条形码

二维码识别

功能说明

二维码识别功能可识别图片中有效二维码的位置及内容,输出图像中二维码包含的文本信息(每个二维码对应的 URL 或文本),并可对识别出的二维码添加马赛克。

请求示例1:上传时识别

<?php

require dirname(__FILE__) . '/../vendor/autoload.php';

$secretId = "SECRETID"; //替换为用户的 secretId,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi
$secretKey = "SECRETKEY"; //替换为用户的 secretKey,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi
$region = "ap-beijing"; //替换为用户的 region,已创建桶归属的 region 可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket
$cosClient = new Qcloud\\Cos\\Client(
array(
'region' => $region,
'schema' => 'https', //协议头部,默认为http
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));

try {
$imageQrcodeTemplate = new Qcloud\\Cos\\ImageParamTemplate\\ImageQrcodeTemplate();//创建二维码识别参数模版实例
$imageQrcodeTemplate->setMode(0);//二维码覆盖功能。0表示不开启二维码覆盖,1表示开启二维码覆盖功能,开启后,将对识别出的二维码覆盖上马赛克,默认值0
$picOperationsTemplate = new \\Qcloud\\Cos\\ImageParamTemplate\\PicOperationsTransformation();//创建图片持久化处理参数模版实例
$picOperationsTemplate->setIsPicInfo(1);//设置是否返回原图信息,0不返回原图信息,1返回原图信息,默认为0
$picOperationsTemplate->addRule($imageQrcodeTemplate, "resultobject");//设置图片持久化处理参数
$result = $cosClient->putObject(array(
'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
'Key' => 'exampleobject',
'Body' => fopen('path/to/localFile', 'rb'),
'PicOperations' => $picOperationsTemplate->queryString(),//生成图片持久化处理参数
));
// 请求成功
print_r($result);
} catch (\\Exception $e) {
// 请求失败
echo($e);
}

参数说明

参数名称
类型
描述
是否必填
Bucket
String
存储桶名称,格式:BucketName-APPID
Key
String
此处的 Key 为对象键,对象键是对象在存储桶中的唯一标识。例如,在对象的访问域名examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg 中,对象键为doc/pic.jpg
Body
File/String
上传的内容
PicOperations
Json/String
图片持久化处理信息

返回结果示例

Guzzle\\Service\\Resource\\Model Object
(
[structure:protected] =>
[data:protected] => Array
(
[Body] =>
[ETag] => "698d51a19d8a121ce581499d7b701668"
[RequestId] => NWQwOGRkNDdfMjJiMjU4NjRfNzVjXzEwNmVjY2M=
[ContentLength] => 238186
[Key] => exampleobject
[Bucket] => examplebucket-1250000000
[Location] => examplebucket-1250000000.cos.ap-beijing.myqcloud.com/exampleobject
[Data] => Array
(
[OriginalInfo] => Array
(
[Key] => exampleobject
[Location] => examplebucket-1250000000.cos.ap-beijing.myqcloud.com/exampleobject
[ETag] => "7037fb6fb4cca43b958a28789605e73d98088720"
[ImageInfo] => Array
(
[Format] => JPEG
[Width] => 600
[Height] => 500
[Quality] => 90
[Ave] => 0x46442e
[Orientation] => 0
)

)
[ProcessResults] => Array
(
[Object] => Array
(
[0] => Array(
[Key] => resultobject
[Location] => examplebucket-1250000000.cos.ap-beijing.myqcloud.com/resultobject
[Format] => JPEG
[Width] => 300
[Height] => 200
[Size] => 30000
[Quality] => 90
[CodeStatus] => 1
[QRcodeInfo] => Array
(
[0] => Array
(
[CodeUrl] => xxxxxxxxxxxxx
[CodeLocation] => Array
(
[Point] => Array
(
[0] => 100,100
[1] => 100,200
[2] => 200,200
[3] => 200,100
)
)
)
)
[ETag] => "87c153bc2909aa0ba111ca126b675c510d36b817"
)
)
)
)
)
)


返回结果说明

参数名称
类型
描述
父节点
Body
File/String
返回体
ETag
String
文件的 MD5 值
RequestId
String
请求 ID 标识
ContentLength
Int
响应体长度
Key
String
对象键
Bucket
String
存储桶名称,格式:BucketName-APPID
Location
String
请求资源地址
Data
Array
图片处理结果信息(二维码信息)

请求示例2:下载时识别

<?php

require dirname(__FILE__) . '/../vendor/autoload.php';

$secretId = "SECRETID"; //替换为用户的 secretId,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi
$secretKey = "SECRETKEY"; //替换为用户的 secretKey,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi
$region = "ap-beijing"; //替换为用户的 region,已创建桶归属的 region 可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket
$cosClient = new Qcloud\\Cos\\Client(
array(
'region' => $region,
'schema' => 'https', //协议头部,默认为 http
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));

try {
$result = $cosClient->Qrcode(array(
'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
'Key' => 'exampleobject',
'Cover' => 1,//二维码覆盖功能
));
// 请求成功
print_r($result);
} catch (\\Exception $e) {
// 请求失败
echo($e);
}

参数说明

参数名称
类型
描述
是否必填
Bucket
String
存储桶名称,格式:BucketName-APPID
Key
String
此处的 Key 为对象键,对象键是对象在存储桶中的唯一标识。例如,在对象的访问域名examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg 中,对象键为doc/pic.jpg
Cover
Int
二维码覆盖功能,将对识别出的二维码覆盖上马赛克。0表示不开启二维码覆盖,1表示开启二维码覆盖,默认值0

返回结果示例

Guzzle\\Service\\Resource\\Model Object
(
[structure:protected] =>
[data:protected] => Array
(
[RequestId] => NWQwOGRkNDdfMjJiMjU4NjRfNzVjXzEwNmVjY2M=
[ContentLength] => 238186
[Key] => exampleobject
[Bucket] => examplebucket-1250000000
[Location] => examplebucket-1250000000.cos.ap-beijing.myqcloud.com/exampleobject
[CodeStatus] => 1
[QRcodeInfo] => Array
(
[0] => Array
(
[CodeUrl] => xxxxxxxxxxxxx
[CodeLocation] => Array
(
[Point] => Array
(
[0] => 100,100
[1] => 100,200
[2] => 200,200
[3] => 200,100
)
)
)
)
[ResultImage] =>

)
)


返回结果说明

参数名称
类型
描述
父节点
RequestId
String
请求 ID 标识
ContentLength
Int
响应体长度
Key
String
对象键
Bucket
String
存储桶名称,格式:BucketName-APPID
Location
String
请求资源地址
CodeStatus
Int
二维码识别结果。0表示未识别到二维码,1表示识别到二维码
QRcodeInfo
Array
二维码识别结果,可能有多个
ResultImage
String
处理后的图片 base64数据,请求参数 cover 为1时返回

二维码生成

功能说明

二维码生成功能可根据用户指定的文本信息(URL 或文本),生成对应的二维码或条形码。

示例代码

<?php

require dirname(__FILE__) . '/../vendor/autoload.php';

$secretId = "SECRETID"; //替换为用户的 secretId,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi
$secretKey = "SECRETKEY"; //替换为用户的 secretKey,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi
$region = "ap-beijing"; //替换为用户的 region,已创建桶归属的 region 可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket
$cosClient = new Qcloud\\Cos\\Client(
array(
'region' => $region,
'schema' => 'https', //协议头部,默认为 http
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));

try {
$result = $cosClient->QrcodeGenerate(array(
'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
'QrcodeContent' => '<https://www.example.com>',//可识别的二维码文本信息
'QrcodeMode' => 0 //生成的二维码类型,可选值:0或1。0为二维码,1为条形码,默认值为0
'QrcodeWidth' => '200',//指定生成的二维码或条形码的宽度,高度会进行等比压缩
));
// 请求成功
print_r($result);
} catch (\\Exception $e) {
// 请求失败
echo($e);
}

参数说明

参数名称
类型
描述
是否必填
Bucket
String
存储桶名称,格式:BucketName-APPID
QrcodeContent
String
可识别的二维码文本信息
QrcodeMode
Int
生成的二维码类型,可选值:0或1。0为二维码,1为条形码,默认值为0
QrcodeWidth
String
指定生成的二维码或条形码的宽度,高度会进行等比压缩

返回结果示例

Guzzle\\Service\\Resource\\Model Object
(
[structure:protected] =>
[data:protected] => Array
(
[RequestId] => NWQwOGRkNDdfMjJiMjU4NjRfNzVjXzEwNmVjY2M=
[Bucket] => examplebucket-1250000000
[Location] => examplebucket-1250000000.cos.ap-beijing.myqcloud.com/
[ResultImage] =>
)
)


返回结果说明

参数名称
类型
描述
父节点
RequestId
String
请求 ID 标识
Bucket
String
存储桶名称,格式:BucketName-APPID
Location
String
请求资源地址
ResultImage
String
处理后的图片 base64数据