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

简介

本文档提供关于图片持久化处理的 API 概览以及 SDK 示例代码。
API
说明
用于图片持久化处理

上传时处理

功能说明

在图片上传时实现相应的图片处理,并可将原图和处理结果存入到 COS。目前支持20M以内文件处理 。

方法原型

CosResult PutImage(const PutImageByFileReq& request, PutImageByFileResp* response);

请求示例

qcloud_cos::CosConfig config("./config.json");
qcloud_cos::CosAPI cos(config);
std::string bucket_name = "examplebucket-1250000000";

PutImageByFileReq req(bucket_name, object_name, local_file);
PutImageByFileResp resp;

PicOperation pic_operation;

PicRules rule;
// 处理结果的文件路径名称,如以/开头
rule.fileid = "/" + object_name + "_sharpen";
// 锐化处理参数
rule.rule = "imageMogr2/sharpen/70";
pic_operation.AddRule(rule);

rule.fileid = "/" + object_name + "_rotate";
// 旋转处理参数
rule.rule = "imageMogr2/rotate/90";
pic_operation.AddRule(rule);

CosResult result = cos.PutImage(req, &resp);
if (result.IsSucc()) {
// 调用成功,调用 resp 的成员函数获取返回内容
std::cout << "ProcessResult: " << resp.GetUploadResult().to_string() << std::endl;
} else {
// 调用失败,调用 result 的成员函数获取错误信息
}

参数说明

参数
参数描述
类型
是否必填
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:
boolis_pic_info; // 是否返回原图信息,0不返回原图信息,1返回原图信息,默认为0
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; // 图片处理结果
};

云上图片处理

功能说明

能够对已存储在 COS 的图片进行相应处理操作,并将结果存入到 COS。

方法原型

CosResult CloudImageProcess(const CloudImageProcessReq& request, CloudImageProcessResp* response);

请求示例

qcloud_cos::CosConfig config("./config.json");
qcloud_cos::CosAPI cos(config);
std::string bucket_name = "examplebucket-1250000000";

CloudImageProcessReq req(bucket_name, object_name);
CloudImageProcessResp resp;

PicOperation pic_operation;
PicRules rule;
// 处理结果的文件路径名称,如以/开头
rule.fileid = "/" + object_name + "_thumbnail";
// 缩放处理参数
rule.rule = "imageMogr2/thumbnail/!30p";
pic_operation.AddRule(rule);

rule.fileid = "/" + object_name + "_cut";
// 裁剪处理参数
rule.rule = "imageMogr2/cut/300x300";
pic_operation.AddRule(rule);
req.SetPicOperation(pic_operation);

CosResult result = cos.CloudImageProcess(req, &resp);
if (result.IsSucc()) {
// 调用成功,调用 resp 的成员函数获取返回内容
} else {
// 调用失败,调用 result 的成员函数获取错误信息
}

参数说明

参数
参数描述
类型
是否必填
req
PutImage操作的请求
PutImageByFileReq
resp
PutImage操作的响应
PutImageByFileResp