简介
本文档提供关于对象存储图片二维码相关的 API 概览以及 SDK 示例代码。
API | 操作描述 |
二维码识别功能可识别图片中有效二维码的位置及内容,输出图像中二维码包含的文本信息(每个二维码对应的 URL 或文本),并可对识别出的二维码添加马赛克 |
图片二维码
上传时识别二维码
功能说明
识别图片中有效二维码的位置及内容,输出图像中二维码包含的文本信息(每个二维码对应的 URL 或文本),并可对识别出的二维码添加马赛克。
方法原型
CosResult PutImage(const PutImageByFileReq& request, PutImageByFileResp* response);
请求示例
qcloud_cos::CosConfig config("./config.json");qcloud_cos::CosAPI cos(config);std::string bucket_name = "examplebucket-1250000000";std::string object_name = "test.jpg"; // 保存到 COS 的对象名std::string local_file = "./test.jpg"; // 本地待上传文件PutImageByFileReq req(bucket_name, object_name, local_file);PutImageByFileResp resp;PicOperation pic_operation;PicRules rule1;rule1.fileid = "/" + object_name + "_put_qrcode";rule1.rule = "QRcode/cover/1";pic_operation.AddRule(rule1);req.SetPicOperation(pic_operation);qcloud_cos::CosResult result = cos.PutImage(req, &resp);if (result.IsSucc()) {std::cout << "ProcessResult: " << resp.GetUploadResult().to_string() << std::endl;} else {std::cout << "ErrorCode: " << result.GetErrorCode()<< ", ErrorMsg: " << result.GetErrorMsg()<< ", RequestId: " << result.GetXCosRequestId() << std::endl;}
参数说明
参数 | 参数描述 | 类型 | 是否必填 |
req | PutImage 操作的请求 | PutImageByFileReq | 是 |
resp | PutImage 操作的响应 | PutImageByFileResp | 是 |
req 涉及到如下结构:
struct PicRules {std::string bucket; // 存储结果的目标存储桶名称,格式为 BucketName-APPID,如果不指定的话默认保存到当前存储桶std::string fileid; // 处理结果的文件路径名称,如以/开头,则存入指定文件夹中,否则,存入原图文件存储的同目录std::string rule; // 处理参数,参见对象存储图片处理API。若按指定样式处理,则以style/开头,后加样式名,如样式名为test,则 rule 字段为style/test};class PicOperation {public:PicOperation() : is_pic_info(true) {}virtual ~PicOperation() {}void AddRule(const PicRules& rule) { rules.push_back(rule); }void TurnOffPicInfo() { is_pic_info = false; }private:bool is_pic_info; // 是否返回原图信息,0不返回,1返回;本 SDK 默认为1(返回),如需关闭调用 TurnOffPicInfo()std::vector<PicRules> rules; //处理规则,一条规则对应一个处理结果(目前支持五条规则),不填则不进行图片处理};
resp 涉及到如下结构:
struct CodeLocation {std::vector<std::string> points; // 二维码坐标点};struct QRcodeInfo {std::string code_url; // 二维码的内容。可能识别不出CodeLocation code_location; // 图中识别到的二维码位置坐标};struct Object {std::string key; // 文件名std::string location; // 图片路径std::string format; // 图片格式int width; // 图片宽度int height; // 图片高度int size; // 图片大小int quality; // 图片质量std::string etag; // 处理结果图 ETag 信息int code_status; // 二维码识别结果。0表示未识别到二维码,1表示识别到二维码int watermark_status; // 当 type// 为2时返回该字段,表示提取到全盲水印的可信度。// 具体为0-100的数字,75分以上表示确定有盲水印,60-75表示疑似有盲水印,60以下可认为未提取到盲水印std::vector<QRcodeInfo> qr_code_info; // 二维码识别结果,可能有多个};struct ProcessResults {std::vector<Object> objects; // 可能有多个对象};struct ImageInfo {std::string format; // 格式int width; // 图片宽度int height; // 图片高度int quality; // 图片质量std::string ave; // 图片主色调int orientation; // 图片旋转角度};struct UploadResult {OriginalInfo original_info; // 原图信息ProcessResults process_result; // 图片处理结果};
下载时识别二维码
功能说明
下载图片时识别二维码。
方法原型
CosResult GetQRcode(const GetQRcodeReq& request, GetQRcodeResp* response);
请求示例
qcloud_cos::CosConfig config("./config.json");qcloud_cos::CosAPI cos(config);std::string bucket_name = "examplebucket-1250000000";std::string object_name = "test.jpg";GetQRcodeReq req(bucket_name, object_name);GetQRcodeResp resp;CosResult result = cos.GetQRcode(req, &resp);if (result.IsSucc()) {GetQRcodeResult qr = resp.GetResult();// 读取二维码识别结果字段,例如 CodeStatus、QRcodeInfo(URL/文本、坐标)等} else {std::cout << "ErrorCode: " << result.GetErrorCode()<< ", ErrorMsg: " << result.GetErrorMsg()<< ", RequestId: " << result.GetXCosRequestId() << std::endl;}
参数说明
参数 | 参数描述 | 类型 | 是否必填 |
req | GetQRCode 操作的请求 | GetQRcodeReq | 是 |
resp | GetQRcode 操作的响应 | GetQRcodeResp | 是 |
req 涉及到如下结构:
class GetQRcodeReq : public ObjectReq {public:GetQRcodeReq(const std::string& bucket_name, const std::string& object_name): ObjectReq(bucket_name, object_name) {AddParam("ci-process", "QRcode");AddParam("cover", "0");m_method = "GET";}void EnableCover() { AddParam("cover", "1"); } // 开启二维码马赛克覆盖};
resp 涉及到如下结构:
struct CodeLocation {std::vector<std::string> points; // 二维码坐标点};struct QRcodeInfo {std::string code_url; // 二维码的内容。可能识别不出CodeLocation code_location; // 图中识别到的二维码位置坐标};struct GetQRcodeResult {int code_status; // 二维码识别结果。0表示未识别到二维码,1表示识别到二维码std::vector<QRcodeInfo> qr_code_info; // 二维码识别结果,可能有多个std::string result_image; // 处理后的图片 base64数据,请求参数 cover 为1时返回};