全部产品
云市场

Java

更新时间:2019-10-14 16:11:23

本文介绍了如何使用阿里云IVPD服务的Java SDK,具体包括SDK的获取和安装方法以及SDK代码示例。

获取地址

您需要引入两个SDK,包括aliyun-java-sdk-core(阿里云核心SDK)和aliyun-java-sdk-ivpd(智能视觉生产SDK)。每个SDK都提供了Maven Repository、Central Repository、GitHub的获取方式,您可以选择合适的方式获取SDK。

安装说明

方法1:使用Maven(推荐)

如果您使用Maven管理Java项目,可以通过在pom.xml文件中添加Maven依赖

  1. <dependency>
  2. <groupId>com.aliyun</groupId>
  3. <artifactId>aliyun-java-sdk-ivpd</artifactId>
  4. <version>${aliyun.ivpd.version}</version>
  5. </dependency>

说明 version的值以SDK获取地址中的最新版本为准。

方法2:在集成开发环境(IDE)中导入jar文件

  • Eclipse安装
    • 将下载的aliyun-java-sdk-xxx.jar文件复制到您的项目文件夹中。
    • 在Eclipse中打开您的项目,右键单击该项目,单击Properties。
    • 在弹出的对话框中,单击Java Build Path > Libraries > Add JARs添加下载的JAR文件。
    • 单击Apply and Close。
  • IntelliJ 安装
    • 将下载的aliyun-java-sdk-xxx.jar文件复制到您的项目文件夹中。
    • 在IntelliJ中打开您的项目,在菜单栏中单击File > Project > Structure。
    • 单击Apply,然后单击OK。

示例

  1. public class ImageTest{
  2. static IAcsClient client = null;
  3. public static void main(String[] args) throws Exception {
  4. DefaultProfile profile = DefaultProfile.getProfile(
  5. "cn-shanghai", //默认
  6. "${accessKeyId}", //您的Access Key ID
  7. "${accessKeySecret}"); //您的Access Key Secret
  8. client = new DefaultAcsClient(profile);
  9. testSegmentImage();//图像分割/抠图
  10. testMakeSuperResolutionImage();//超分辨率
  11. testChangeImageSize();//尺寸变换
  12. testDetectImageElements();//元素检测
  13. testExtendImageStyle();//风格迁移
  14. testRecognizeImageColor();//色板识别
  15. testRecognizeImageStyle();//风格识别
  16. testRecolorImage();//色彩迁移
  17. System.out.println("--------------------------------------------------------------");
  18. }
  19. private static <R extends RpcAcsRequest<T>, T extends AcsResponse> T getAcsResponse(R req) throws Exception {
  20. return client.getAcsResponse(req);
  21. }
  22. public static void testChangeImageSize() throws Exception {
  23. System.out.println("-------- 尺寸变换 --------------");
  24. ChangeImageSizeRequest req = new ChangeImageSizeRequest();
  25. req.setBizUrl("http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/ivpd-demo/images/ChangeImageSize/change-image-size-src.png");
  26. req.setWidth(800);
  27. req.setHeight(600);
  28. ChangeImageSizeResponse resp = getAcsResponse(req);
  29. printResponse(req.getSysActionName(), resp.getRequestId(), resp.getCode(), resp.getMessage(), resp.getData());
  30. }
  31. public static void testExtendImageStyle() throws Exception {
  32. ExtendImageStyleRequest req = new ExtendImageStyleRequest();
  33. System.out.println("-------- 图像风格迁移 --------------");
  34. req.setStyleUrl("http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/ivpd-demo/images/ExtendImageStyle/majorUrl.jpeg");
  35. req.setMajorUrl("http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/ivpd-demo/images/ExtendImageStyle/styleUrl.jpeg");
  36. ExtendImageStyleResponse resp = getAcsResponse(req);
  37. printResponse(req.getSysActionName(), resp.getRequestId(), resp.getCode(), resp.getMessage(), resp.getData());
  38. }
  39. public static void testMakeSuperResolutionImage() throws Exception {
  40. MakeSuperResolutionImageRequest req = new MakeSuperResolutionImageRequest();
  41. System.out.print("-------- 清晰化/超分辨 ----");
  42. req.setBizUrl("http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/ivpd-demo/images/MakeSuperResolution/sup-dog.png");
  43. MakeSuperResolutionImageResponse resp = getAcsResponse(req);
  44. printResponse(req.getSysActionName(), resp.getRequestId(), resp.getCode(), resp.getMessage(), resp.getData());
  45. }
  46. public static void testRecognizeImageColor() throws Exception {
  47. RecognizeImageColorRequest req = new RecognizeImageColorRequest();
  48. System.out.print("-------- 色板识别 ----");
  49. req.setBizUrl("http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/ivpd-demo/images/RecognizeImageColor/RecognizeImageColor.png");
  50. RecognizeImageColorResponse resp = getAcsResponse(req);
  51. printResponse(req.getSysActionName(), resp.getRequestId(), resp.getCode(), resp.getMessage(), resp.getData());
  52. }
  53. public static void testRecognizeImageStyle() throws Exception {
  54. RecognizeImageStyleRequest req = new RecognizeImageStyleRequest();
  55. System.out.print("-------- 风格识别 ----");
  56. req.setBizUrl("http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/ivpd-demo/images/RecognizeImageStyle/technology.png");
  57. RecognizeImageStyleResponse resp = getAcsResponse(req);
  58. printResponse(req.getSysActionName(), resp.getRequestId(), resp.getCode(), resp.getMessage(), resp.getData());
  59. }
  60. public static void testRecolorImage() throws Exception {
  61. RecolorImageRequest req = new RecolorImageRequest();
  62. System.out.print("-------- 拓色 ----");
  63. req.setBizUrl("http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/ivpd-demo/images/RecolorImage/recolor-1-src.png");
  64. req.setColorCount(3);
  65. //(必填)拓色模式,默认AUTO,取值范围[AUTO:自动拓色, TEMPLATE:色板拓色, REF_PIC:参考图拓色]
  66. req.setMode("AUTO");
  67. RecolorImageResponse resp = null;
  68. //AUTO
  69. System.out.println("自动拓色");
  70. resp = getAcsResponse(req);
  71. printResponse(req.getSysActionName(), resp.getRequestId(), resp.getCode(), resp.getMessage(), resp.getData());
  72. //RefUrl TODO
  73. System.out.println("参考图拓色");
  74. req.setMode("REF_PIC");
  75. req.setRefUrl("http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/ivpd-demo/images/RecolorImage/recolor-refurl-src.png");
  76. resp = getAcsResponse(req);
  77. printResponse(req.getSysActionName(), resp.getRequestId(), resp.getCode(), resp.getMessage(), resp.getData());
  78. //ColorTemplate TODO
  79. System.out.println("色板拓色");
  80. req.setMode("TEMPLATE");
  81. req.setBizUrl("http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/ivpd-demo/images/RecolorImage/recolor-2-src.png");
  82. List<RecolorImageRequest.ColorTemplate> colorTemplateList = new ArrayList<>();
  83. colorTemplateList.add(new RecolorImageRequest.ColorTemplate("056A6B"));//
  84. colorTemplateList.add(new RecolorImageRequest.ColorTemplate("FF0000"));//16711680
  85. colorTemplateList.add(new RecolorImageRequest.ColorTemplate("00FF00"));//65280
  86. req.setColorTemplates(colorTemplateList);
  87. resp = getAcsResponse(req);
  88. printResponse(req.getSysActionName(), resp.getRequestId(), resp.getCode(), resp.getMessage(), resp.getData());
  89. }
  90. public static void testDetectImageElements() throws Exception {
  91. DetectImageElementsRequest req = new DetectImageElementsRequest();
  92. System.out.print("-------- 元素检测 -----");
  93. req.setBizUrl("http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/ivpd-demo/images/DetectImageElements/detect-elements-src.png");
  94. DetectImageElementsResponse resp = getAcsResponse(req);
  95. printResponse(req.getSysActionName(), resp.getRequestId(), resp.getCode(), resp.getMessage(), resp.getData());
  96. }
  97. public static void testSegmentImage() throws Exception {
  98. SegmentImageRequest req = new SegmentImageRequest();
  99. System.out.print("-------- 抠图 -----");
  100. req.setBizUrl("http://ivpd-demo.oss-cn-shanghai.aliyuncs.com/aliyun-doc/pop/images/segment-image-src.jpg");
  101. SegmentImageResponse resp = getAcsResponse(req);
  102. printResponse(req.getSysActionName(), resp.getRequestId(), resp.getCode(), resp.getMessage(), resp.getData());
  103. }