Skip to content

Commit d5b3275

Browse files
committed
新增万象图片处理样式 更新demo
1 parent d9431eb commit d5b3275

16 files changed

+582
-5
lines changed

src/main/java/com/qcloud/cos/COS.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3030,6 +3030,14 @@ SelectObjectContentResult selectObjectContent(SelectObjectContentRequest selectR
30303030
MediaWorkflowListResponse triggerWorkflowList(MediaWorkflowListRequest request);
30313031

30323032
InputStream getSnapshot(CosSnapshotRequest request);
3033+
3034+
String generateQrcode(GenerateQrcodeRequest request);
3035+
3036+
Boolean addImageStyle(ImageStyleRequest request);
3037+
3038+
ImageStyleResponse getImageStyle(ImageStyleRequest request);
3039+
3040+
Boolean deleteImageStyle(ImageStyleRequest request);
30333041
}
30343042

30353043

src/main/java/com/qcloud/cos/COSClient.java

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import com.qcloud.cos.auth.COSSessionCredentials;
4949
import com.qcloud.cos.auth.COSSigner;
5050
import com.qcloud.cos.auth.COSStaticCredentialsProvider;
51+
import com.qcloud.cos.endpoint.CIPicRegionEndpointBuilder;
5152
import com.qcloud.cos.endpoint.CIRegionEndpointBuilder;
5253
import com.qcloud.cos.endpoint.EndpointBuilder;
5354
import com.qcloud.cos.endpoint.RegionEndpointBuilder;
@@ -64,6 +65,7 @@
6465
import com.qcloud.cos.http.HttpResponseHandler;
6566
import com.qcloud.cos.internal.BucketNameUtils;
6667
import com.qcloud.cos.internal.CIGetSnapshotResponseHandler;
68+
import com.qcloud.cos.internal.CIPicServiceRequest;
6769
import com.qcloud.cos.internal.CIServiceRequest;
6870
import com.qcloud.cos.internal.CIWorkflowServiceRequest;
6971
import com.qcloud.cos.internal.COSDefaultAclHeaderHandler;
@@ -107,12 +109,15 @@
107109
import com.qcloud.cos.model.ciModel.bucket.MediaBucketResponse;
108110
import com.qcloud.cos.model.ciModel.common.ImageProcessRequest;
109111
import com.qcloud.cos.model.ciModel.common.MediaOutputObject;
112+
import com.qcloud.cos.model.ciModel.image.GenerateQrcodeRequest;
110113
import com.qcloud.cos.model.ciModel.image.ImageLabelRequest;
111114
import com.qcloud.cos.model.ciModel.image.ImageLabelResponse;
112115
import com.qcloud.cos.model.ciModel.image.ImageLabelV2Request;
113116
import com.qcloud.cos.model.ciModel.image.ImageLabelV2Response;
114117
import com.qcloud.cos.model.ciModel.image.ImageSearchRequest;
115118
import com.qcloud.cos.model.ciModel.image.ImageSearchResponse;
119+
import com.qcloud.cos.model.ciModel.image.ImageStyleRequest;
120+
import com.qcloud.cos.model.ciModel.image.ImageStyleResponse;
116121
import com.qcloud.cos.model.ciModel.image.OpenImageSearchRequest;
117122
import com.qcloud.cos.model.ciModel.job.DocHtmlRequest;
118123
import com.qcloud.cos.model.ciModel.job.DocJobListRequest;
@@ -526,6 +531,8 @@ private <X extends CosServiceRequest> void buildUrlAndHost(CosHttpRequest<X> req
526531
bucket = formatBucket(bucket, fetchCredential().getCOSAppId());
527532
if (isCIRequest) {
528533
endpoint = new CIRegionEndpointBuilder(clientConfig.getRegion()).buildGeneralApiEndpoint(bucket);
534+
} else if (request.getOriginalRequest() instanceof CIPicServiceRequest) {
535+
endpoint = new CIPicRegionEndpointBuilder(clientConfig.getRegion()).buildGeneralApiEndpoint(bucket);
529536
} else {
530537
endpoint = clientConfig.getEndpointBuilder().buildGeneralApiEndpoint(bucket);
531538
}
@@ -4203,7 +4210,7 @@ public PutAsyncFetchTaskResult putAsyncFetchTask(PutAsyncFetchTaskRequest putAsy
42034210
CosHttpRequest<PutAsyncFetchTaskRequest> request = createRequest(putAsyncFetchTaskRequest.getBucketName(),
42044211
String.format("/%s/", putAsyncFetchTaskRequest.getBucketName()), putAsyncFetchTaskRequest, HttpMethodName.POST);
42054212
PutAsyncFetchTaskSerializer serializer = new PutAsyncFetchTaskSerializer(PutAsyncFetchTaskRequest.class);
4206-
SimpleModule module =
4213+
SimpleModule module =
42074214
new SimpleModule("PutAsyncFetchTaskSerializer", new Version(1, 0, 0, null, null, null));
42084215
module.addSerializer(PutAsyncFetchTaskRequest.class, serializer);
42094216

@@ -4348,5 +4355,58 @@ public InputStream getSnapshot(CosSnapshotRequest snapshotRequest) {
43484355
return this.invoke(request, new CIGetSnapshotResponseHandler());
43494356
}
43504357

4358+
@Override
4359+
public String generateQrcode(GenerateQrcodeRequest generateQrcodeRequest) {
4360+
rejectNull(generateQrcodeRequest,
4361+
"The request parameter must be specified setting the object tags");
4362+
rejectNull(generateQrcodeRequest.getBucketName(),
4363+
"The bucketName parameter must be specified setting the object tags");
4364+
CosHttpRequest<GenerateQrcodeRequest> request = this.createRequest(generateQrcodeRequest.getBucketName(), "/", generateQrcodeRequest, HttpMethodName.GET);
4365+
addParameterIfNotNull(request, "ci-process", "qrcode-generate");
4366+
addParameterIfNotNull(request, "qrcode-content", generateQrcodeRequest.getQrcodeContent());
4367+
addParameterIfNotNull(request, "mode", generateQrcodeRequest.getMode());
4368+
addParameterIfNotNull(request, "width", generateQrcodeRequest.getWidth());
4369+
return this.invoke(request, new Unmarshallers.GenerateQrcodeUnmarshaller());
4370+
}
4371+
4372+
@Override
4373+
public Boolean addImageStyle(ImageStyleRequest imageStyleRequest) {
4374+
rejectNull(imageStyleRequest,
4375+
"The request parameter must be specified setting the object tags");
4376+
rejectNull(imageStyleRequest.getBucketName(),
4377+
"The bucketName parameter must be specified setting the object tags");
4378+
CosHttpRequest<ImageStyleRequest> request = createRequest(imageStyleRequest.getBucketName(), "/", imageStyleRequest, HttpMethodName.PUT);
4379+
request.addParameter("style", "");
4380+
this.setContent(request, CImageXmlFactory.addStyleConvertToXmlByteArray(imageStyleRequest), "application/xml", false);
4381+
invoke(request, voidCosResponseHandler);
4382+
return true;
4383+
}
4384+
4385+
@Override
4386+
public ImageStyleResponse getImageStyle(ImageStyleRequest imageStyleRequest) {
4387+
rejectNull(imageStyleRequest,
4388+
"The request parameter must be specified setting the object tags");
4389+
rejectNull(imageStyleRequest.getBucketName(),
4390+
"The bucketName parameter must be specified setting the object tags");
4391+
CosHttpRequest<ImageStyleRequest> request = createRequest(imageStyleRequest.getBucketName(), "/", imageStyleRequest, HttpMethodName.GET);
4392+
request.addParameter("style", "");
4393+
this.setContent(request, CImageXmlFactory.getStyleConvertToXmlByteArray(imageStyleRequest), "application/xml", false);
4394+
invoke(request, new Unmarshallers.getImageStyleUnmarshaller());
4395+
return null;
4396+
}
4397+
4398+
@Override
4399+
public Boolean deleteImageStyle(ImageStyleRequest imageStyleRequest) {
4400+
rejectNull(imageStyleRequest,
4401+
"The request parameter must be specified setting the object tags");
4402+
rejectNull(imageStyleRequest.getBucketName(),
4403+
"The bucketName parameter must be specified setting the object tags");
4404+
CosHttpRequest<ImageStyleRequest> request = createRequest(imageStyleRequest.getBucketName(), "/", imageStyleRequest, HttpMethodName.DELETE);
4405+
request.addParameter("style", "");
4406+
this.setContent(request, CImageXmlFactory.deleteStyleConvertToXmlByteArray(imageStyleRequest), "application/xml", false);
4407+
invoke(request, voidCosResponseHandler);
4408+
return true;
4409+
}
4410+
43514411
}
43524412

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.qcloud.cos.demo.ci;
2+
3+
import com.qcloud.cos.COSClient;
4+
import com.qcloud.cos.model.ciModel.image.ImageStyleRequest;
5+
import com.qcloud.cos.model.ciModel.image.ImageStyleResponse;
6+
import com.qcloud.cos.model.ciModel.image.StyleRule;
7+
8+
import java.util.List;
9+
10+
11+
/**
12+
* 图片样式接口 demo
13+
* 接口详情见 https://cloud.tencent.com/document/product/460/30118
14+
*/
15+
public class ImageStyleDemo {
16+
public static void main(String[] args) throws Exception {
17+
COSClient cosClient = ClientUtils.getTestClient();
18+
// 小于5GB文件用简单上传
19+
addImageStyle(cosClient);
20+
cosClient.shutdown();
21+
}
22+
23+
/**
24+
* 添加图片处理样式
25+
* https://cloud.tencent.com/document/product/460/53491
26+
*/
27+
public static void addImageStyle(COSClient cosClient) {
28+
//1.创建二维码生成请求对象
29+
ImageStyleRequest request = new ImageStyleRequest();
30+
//2.添加请求参数 参数详情请见api接口文档
31+
request.setBucketName("examplebucket-1250000000");
32+
request.setStyleName("DemoStyle");
33+
//设置样式规则,demo此处处理规则含义为:缩放图片宽高为原图50%
34+
request.setStyleBody("imageMogr2/thumbnail/!50p");
35+
Boolean response = cosClient.addImageStyle(request);
36+
}
37+
38+
/**
39+
* 查询图片处理样式
40+
* https://cloud.tencent.com/document/product/460/30117
41+
*/
42+
public static void getImageStyle(COSClient cosClient) {
43+
//1.创建二维码生成请求对象
44+
ImageStyleRequest request = new ImageStyleRequest();
45+
//2.添加请求参数 参数详情请见api接口文档
46+
request.setBucketName("examplebucket-1250000000");
47+
request.setStyleName("DemoStyle");
48+
ImageStyleResponse response = cosClient.getImageStyle(request);
49+
List<StyleRule> styleRule = response.getStyleRule();
50+
for (StyleRule rule : styleRule) {
51+
System.out.println(rule.getStyleName());
52+
System.out.println(rule.getStyleBody());
53+
}
54+
}
55+
56+
/**
57+
* 查询图片处理样式
58+
* https://cloud.tencent.com/document/product/460/30117
59+
*/
60+
public static void deleteImageStyle(COSClient cosClient) {
61+
//1.创建二维码生成请求对象
62+
ImageStyleRequest request = new ImageStyleRequest();
63+
//2.添加请求参数 参数详情请见api接口文档
64+
request.setBucketName("examplebucket-1250000000");
65+
request.setStyleName("DemoStyle");
66+
//设置样式规则,demo此处处理规则含义为:缩放图片宽高为原图50%
67+
request.setStyleBody("imageMogr2/thumbnail/!50p");
68+
Boolean response = cosClient.deleteImageStyle(request);
69+
}
70+
71+
72+
}

src/main/java/com/qcloud/cos/demo/ci/QRCodeDemo.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.qcloud.cos.model.UploadPartRequest;
1414
import com.qcloud.cos.model.UploadPartResult;
1515
import com.qcloud.cos.model.UploadResult;
16+
import com.qcloud.cos.model.ciModel.image.GenerateQrcodeRequest;
1617
import com.qcloud.cos.model.ciModel.persistence.CIObject;
1718
import com.qcloud.cos.model.ciModel.persistence.CIUploadResult;
1819
import com.qcloud.cos.model.ciModel.persistence.PicOperations;
@@ -27,6 +28,22 @@
2728

2829

2930
public class QRCodeDemo {
31+
32+
/**
33+
* 二维码生成 https://cloud.tencent.com/document/product/460/53491
34+
*/
35+
public static void generateQrcode(COSClient cosClient) {
36+
//1.创建二维码生成请求对象
37+
GenerateQrcodeRequest request = new GenerateQrcodeRequest();
38+
//2.添加请求参数 参数详情请见api接口文档
39+
request.setBucketName("examplebucket-1250000000");
40+
request.setQrcodeContent("数据万象");
41+
request.setWidth("400");
42+
request.setMode("0");
43+
String imageBase64 = cosClient.generateQrcode(request);
44+
System.out.println(imageBase64);
45+
}
46+
3047
public static void identifyQrCode(COSClient cosClient) {
3148
// bucket名需包含appid
3249
// api 请参考 https://cloud.tencent.com/document/product/436/54070
@@ -50,7 +67,7 @@ public static void identifyQrCode(COSClient cosClient) {
5067
CIUploadResult ciUploadResult = putObjectResult.getCiUploadResult();
5168
System.out.println(putObjectResult.getRequestId());
5269
System.out.println(ciUploadResult.getOriginalInfo().getEtag());
53-
for(CIObject ciObject:ciUploadResult.getProcessResults().getObjectList()) {
70+
for (CIObject ciObject : ciUploadResult.getProcessResults().getObjectList()) {
5471
System.out.println(ciObject.getLocation());
5572
}
5673
} catch (CosServiceException e) {
@@ -107,7 +124,7 @@ public static void identifyQrCodeWithMultipart(COSClient cosClient) throws FileN
107124
CIUploadResult ciUploadResult = completeMultipartUploadResult.getCiUploadResult();
108125
System.out.println(completeMultipartUploadResult.getRequestId());
109126
System.out.println(ciUploadResult.getOriginalInfo().getEtag());
110-
for(CIObject ciObject:ciUploadResult.getProcessResults().getObjectList()) {
127+
for (CIObject ciObject : ciUploadResult.getProcessResults().getObjectList()) {
111128
System.out.println(ciObject.getLocation());
112129
}
113130

@@ -136,14 +153,15 @@ public static void identifyQrCodeWithTransferManager(TransferManager transferMan
136153
CIUploadResult ciUploadResult = uploadResult.getCiUploadResult();
137154
System.out.println(uploadResult.getRequestId());
138155
System.out.println(ciUploadResult.getOriginalInfo().getEtag());
139-
for(CIObject ciObject:ciUploadResult.getProcessResults().getObjectList()) {
156+
for (CIObject ciObject : ciUploadResult.getProcessResults().getObjectList()) {
140157
System.out.println(ciObject.getLocation());
141158
}
142159
}
160+
143161
public static void main(String[] args) throws Exception {
144162
COSClient cosClient = ClientUtils.getTestClient();
145163
// 小于5GB文件用简单上传
146-
identifyQrCode(cosClient);
164+
generateQrcode(cosClient);
147165
cosClient.shutdown();
148166
}
149167
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
15+
* According to cos feature, we modify some class,comment, field name, etc.
16+
*/
17+
18+
19+
package com.qcloud.cos.endpoint;
20+
21+
import com.qcloud.cos.internal.BucketNameUtils;
22+
import com.qcloud.cos.region.Region;
23+
24+
public class CIPicRegionEndpointBuilder implements EndpointBuilder {
25+
private Region region;
26+
27+
public CIPicRegionEndpointBuilder(Region region) {
28+
super();
29+
this.region = region;
30+
}
31+
32+
@Override
33+
public String buildGeneralApiEndpoint(String bucketName) {
34+
if (this.region == null) {
35+
throw new IllegalArgumentException("region is null");
36+
}
37+
BucketNameUtils.validateBucketName(bucketName);
38+
return String.format("%s.pic.%s.myqcloud.com", bucketName, Region.formatCIRegion(this.region));
39+
}
40+
41+
@Override
42+
public String buildGetServiceApiEndpoint() {
43+
return "service.pic.myqcloud.com";
44+
}
45+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2020 腾讯云, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License").
7+
* You may not use this file except in compliance with the License.
8+
* A copy of the License is located at
9+
*
10+
* http://aws.amazon.com/apache2.0
11+
*
12+
* or in the "license" file accompanying this file. This file is distributed
13+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14+
* express or implied. See the License for the specific language governing
15+
* permissions and limitations under the License.
16+
17+
* According to ci feature, we modify some class,comment, field name, etc.
18+
*/
19+
20+
package com.qcloud.cos.internal;
21+
22+
public class CIPicServiceRequest extends CIServiceRequest {
23+
24+
}

src/main/java/com/qcloud/cos/internal/Unmarshallers.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import com.qcloud.cos.model.ciModel.image.ImageLabelResponse;
6464
import com.qcloud.cos.model.ciModel.image.ImageLabelV2Response;
6565
import com.qcloud.cos.model.ciModel.image.ImageSearchResponse;
66+
import com.qcloud.cos.model.ciModel.image.ImageStyleResponse;
6667
import com.qcloud.cos.model.ciModel.job.DocJobListResponse;
6768
import com.qcloud.cos.model.ciModel.job.DocJobResponse;
6869
import com.qcloud.cos.model.ciModel.job.MediaJobResponse;
@@ -807,4 +808,22 @@ public MediaWorkflowListResponse unmarshall(InputStream in) throws Exception {
807808
.parsetriggerWorkflowListResponse(in).getResponse();
808809
}
809810
}
811+
812+
public static final class GenerateQrcodeUnmarshaller
813+
implements Unmarshaller<String, InputStream> {
814+
815+
public String unmarshall(InputStream in) throws Exception {
816+
return new XmlResponsesSaxParser()
817+
.parseGenerateQrcodeResponse(in).getResponse();
818+
}
819+
}
820+
821+
public static final class getImageStyleUnmarshaller
822+
implements Unmarshaller<ImageStyleResponse, InputStream> {
823+
824+
public ImageStyleResponse unmarshall(InputStream in) throws Exception {
825+
return new XmlResponsesSaxParser()
826+
.parseGetImageStyleResponse(in).getResponse();
827+
}
828+
}
810829
}

src/main/java/com/qcloud/cos/internal/XmlResponsesSaxParser.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import com.qcloud.cos.exception.CosServiceException;
3636
import com.qcloud.cos.exception.MultiObjectDeleteException.DeleteError;
3737
import com.qcloud.cos.internal.cihandler.DetectCarHandler;
38+
import com.qcloud.cos.internal.cihandler.GenerateQrcodeHandler;
39+
import com.qcloud.cos.internal.cihandler.GetImageStyleHandler;
3840
import com.qcloud.cos.internal.cihandler.SearchImageHandler;
3941
import com.qcloud.cos.internal.cihandler.TriggerWorkflowListHandler;
4042
import com.qcloud.cos.model.AbortIncompleteMultipartUpload;
@@ -875,6 +877,24 @@ public TriggerWorkflowListHandler parsetriggerWorkflowListResponse(InputStream i
875877
return handler;
876878
}
877879

880+
public GenerateQrcodeHandler parseGenerateQrcodeResponse(InputStream inputStream) throws IOException {
881+
GenerateQrcodeHandler handler = new GenerateQrcodeHandler();
882+
parseXmlInputStream(handler, sanitizeXmlDocument(handler, inputStream));
883+
return handler;
884+
}
885+
886+
public GenerateQrcodeHandler parseImageStyleResponse(InputStream inputStream) throws IOException {
887+
GenerateQrcodeHandler handler = new GenerateQrcodeHandler();
888+
parseXmlInputStream(handler, sanitizeXmlDocument(handler, inputStream));
889+
return handler;
890+
}
891+
892+
public GetImageStyleHandler parseGetImageStyleResponse(InputStream inputStream) throws IOException {
893+
GetImageStyleHandler handler = new GetImageStyleHandler();
894+
parseXmlInputStream(handler, sanitizeXmlDocument(handler, inputStream));
895+
return handler;
896+
}
897+
878898
/**
879899
* @param inputStream
880900
*

0 commit comments

Comments
 (0)