通过阅读本文,您可以了解通过服务端SDK调用OpenAPI发起并查询智能媒体服务截图任务的示例代码。
使用说明
发起截图任务前,需要先创建截图模板。通过TemplateId(截图模板ID)发起截图任务,发起截图任务时,支持对截图模板参数进行覆盖重置,截图任务处理成功后,可以根据返回的JobId(任务ID)查询该截图任务的详细信息、截图任务列表及截图的访问URL。
示例代码
您可以通过阿里云OpenAPI开发者门户在线调试。
package com.aliyun.ice.sample;
import com.aliyun.ice20201109.Client;
import com.aliyun.ice20201109.models.*;
import com.aliyun.teaopenapi.models.Config;
public class Sample {
public static void main() throws Exception {
// 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
// 本示例以将AccessKey ID和 AccessKey Secret保存在环境变量为例说明。配置方法请参见:https://help.aliyun.com/document_detail/378657.html
com.aliyun.credentials.Client credentialClient = new com.aliyun.credentials.Client();
Config config = new Config();
config.setCredential(credentialClient);
// 如需硬编码AccessKey ID和AccessKey Secret,代码如下,但强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
// config.accessKeyId = <yourAccessKeyId>;
// config.accessKeySecret = <yourAccessKeySecret>;
config.endpoint = "ice.cn-shanghai.aliyuncs.com";
Client client = new Client(config);
// 创建截图模板
String snapshotTemplateConfig = "{\"Type\":\"Normal\",\"FrameType\":\"normal\",\"Time\":0,\"Count\":10}";
CreateCustomTemplateRequest createCustomTemplateRequest = new CreateCustomTemplateRequest()
.setName("sdk_sample_snapshot_template")
.setType(2)
.setTemplateConfig(snapshotTemplateConfig);
CreateCustomTemplateResponse createCustomTemplateResponse = client.createCustomTemplate(createCustomTemplateRequest);
String templateId = createCustomTemplateResponse.getBody().getCustomTemplate().getTemplateId();
System.out.println("template id: " + templateId);
// 发起截图任务
SubmitSnapshotJobRequest submitSnapshotJobRequest = new SubmitSnapshotJobRequest()
.setName("sdk_sample_snapshot_job")
.setInput(new SubmitSnapshotJobRequest.SubmitSnapshotJobRequestInput()
.setType("Media")
.setMedia("3945e42d01823498****"))
.setOutput(new SubmitSnapshotJobRequest.SubmitSnapshotJobRequestOutput()
.setType("Media")
.setMedia("3945e42d01823498****"))
.setTemplateConfig(new SubmitSnapshotJobRequest.SubmitSnapshotJobRequestTemplateConfig()
.setTemplateId(templateId));
SubmitSnapshotJobResponse submitSnapshotJobResponse = client.submitSnapshotJob(submitSnapshotJobRequest);
String jobId = submitSnapshotJobResponse.getBody().getJobId();
System.out.println("job id: " + templateId);
// 查询截图任务详细信息
GetSnapshotJobRequest getSnapshotJobRequest = new GetSnapshotJobRequest()
.setJobId(jobId);
GetSnapshotJobResponse getSnapshotJobResponse = client.getSnapshotJob(getSnapshotJobRequest);
System.out.println("request id: " + getSnapshotJobResponse.getBody().getRequestId());
// 获取截图任务列表
ListSnapshotJobsRequest listSnapshotJobsRequest = new ListSnapshotJobsRequest()
.setPageSize(5);
ListSnapshotJobsResponse listSnapshotJobsResponse = client.listSnapshotJobs(listSnapshotJobsRequest);
System.out.println("request id: " + listSnapshotJobsResponse.getBody().getRequestId());
// 获取截图任务输出图片的访问URL
GetSnapshotUrlsRequest getSnapshotUrlsRequest = new GetSnapshotUrlsRequest()
.setJobId("20b48fb04483915d4f2cd8ac****")
.setPageSize(5);
GetSnapshotUrlsResponse getSnapshotUrlsResponse = client.getSnapshotUrls(getSnapshotUrlsRequest);
System.out.println("request id: " + getSnapshotJobResponse.getBody().getRequestId());
}
}