下载安装
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-ivpd</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.4.9</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.60</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
代码示例
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.aliyuncs.*;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.ivpd.model.v20190625.*;
import com.aliyuncs.profile.DefaultProfile;
import org.apache.http.util.Asserts;
import java.util.*;
public class ZhiNengShiJueShengChan {
static IAcsClient client = null;
static String regionId = "cn-beijing";
public static void main(String[] args) throws Exception {
DefaultProfile profile = DefaultProfile.getProfile(regionId, "accessKeyId", "accessKey secret");
profile.getHttpClientConfig().setMaxRequests(100);
profile.getHttpClientConfig().setMaxRequestsPerHost(50);
client = new DefaultAcsClient(profile);
testChangeImageSize();//尺寸变换
}
public static void testChangeImageSize() throws Exception {
System.out.println("-------- 尺寸变换 --------");
ChangeImageSizeRequest req = new ChangeImageSizeRequest();
req.setUrl(formatDemoUrl("ChangeImageSize/change-image-size-src.png"));
req.setWidth(800);
req.setHeight(600);
ChangeImageSizeResponse resp = getAcsResponse(req);
printResponse(req.getSysActionName(), resp.getRequestId(), resp.getCode(), resp.getMessage(), resp.getData());
}
public static void printResponse(String action, String requestId, String code, String message, Object data) {
System.out.print(" " + action + " ======================================\n");
System.out.println("requestId: " + requestId);
System.out.println("code: " + code);
System.out.println(" msg: " + message);
System.out.println("body.class: " + data == null ? null : data.getClass());
System.out.println("body: " + JSON.toJSONString(data, SerializerFeature.PrettyFormat));
Asserts.check("0".equals(code), "assert code == 0 req:");
Asserts.check(null != data, "assert data != null");
System.out.println("\n");
}
private static <R extends RpcAcsRequest<T>, T extends AcsResponse> T getAcsResponse(R req) throws Exception {
req.setSysProtocol(ProtocolType.HTTPS);
return client.getAcsResponse(req);
}
private static String formatDemoUrl(String file) {
return String.format("http://ivpd-%s.oss-%s.aliyuncs.com/ivpd-demo/images/%s", regionId, regionId, file);
}
}
参考链接
ChangeImageSize裁剪/尺寸变换