【sdk】- 对接阿里云抠图

打印 上一主题 下一主题

主题 1043|帖子 1043|积分 3129

文档地址:https://help.aliyun.com/zh/viapi/use-cases/general-image-segmentation?spm=a2c4g.11186623.0.0.3814173cenldIs
java对接阿里云的通用分割,将代码原封不动复制进来,执行结果失败,咨询阿里云的人员之后,由于jar包冲突导致,必要引入一些特定的包版本来实现

pom引用

  1.   <dependency>
  2.             <groupId>com.aliyun</groupId>
  3.             <artifactId>imageseg20191230</artifactId>
  4.             <version>2.0.2</version>
  5.             <exclusions>
  6.                 <exclusion>
  7.                     <groupId>com.squareup.okhttp3</groupId>
  8.                     <artifactId>okhttp</artifactId>
  9.                 </exclusion>
  10.             </exclusions>
  11.         </dependency>
  12.         <dependency>
  13.             <groupId>com.aliyun</groupId>
  14.             <artifactId>tea-openapi</artifactId>
  15.             <version>0.3.4</version>
  16.         </dependency>
  17.         <dependency>
  18.             <groupId>com.aliyun</groupId>
  19.             <artifactId>tea-console</artifactId>
  20.             <version>0.0.1</version>
  21.         </dependency>
  22.         <dependency>
  23.             <groupId>com.aliyun</groupId>
  24.             <artifactId>tea-util</artifactId>
  25.             <version>0.2.22</version>
  26.         </dependency>
  27.       <!--在我的服务中必须手动再次引人,不然报错-->
  28.         <dependency>
  29.             <groupId>com.squareup.okhttp3</groupId>
  30.             <artifactId>okhttp</artifactId>
  31.         </dependency>
复制代码
代码

只必要改pom,对于获取环境变量中的ak信息,完全没必要。
  1. public class AliyunApiService {
  2.     public static com.aliyun.imageseg20191230.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
  3.         /*
  4.           初始化配置对象com.aliyun.teaopenapi.models.Config
  5.           Config对象存放 AccessKeyId、AccessKeySecret、endpoint等配置
  6.          */
  7.         com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
  8.                 .setAccessKeyId(accessKeyId)
  9.                 .setAccessKeySecret(accessKeySecret);
  10.         // 访问的域名
  11.         config.endpoint = "imageseg.cn-shanghai.aliyuncs.com";
  12.         return new com.aliyun.imageseg20191230.Client(config);
  13.     }
  14.     public static void main(String[] args_) throws Exception {
  15.         // 创建AccessKey ID和AccessKey Secret,请参考https://help.aliyun.com/document_detail/175144.html
  16.         // 如果您使用的是RAM用户的AccessKey,还需要为子账号授予权限AliyunVIAPIFullAccess,请参考https://help.aliyun.com/document_detail/145025.html
  17.         // 从环境变量读取配置的AccessKey ID和AccessKey Secret。运行代码示例前必须先配置环境变量。
  18.         String accessKeyId = "";
  19.         String accessKeySecret = "";
  20.         com.aliyun.imageseg20191230.Client client = AliyunApiService.createClient(accessKeyId, accessKeySecret);
  21.         // 场景一,使用本地文件
  22. //         InputStream inputStream = new FileInputStream(new File("/Users/qweasdzxc/Downloads/WechatIMG201.jpeg"));
  23.         // 场景二,使用任意可访问的url
  24.         URL url = new URL("https://viapi-test-bj.oss-cn-beijing.aliyuncs.com/viapi-3.0domepic/imageseg/SegmentCommonImage/SegmentCommonImage1.jpg");
  25.         InputStream inputStream = url.openConnection().getInputStream();
  26.         com.aliyun.imageseg20191230.models.SegmentCommonImageAdvanceRequest segmentCommonImageAdvanceRequest = new com.aliyun.imageseg20191230.models.SegmentCommonImageAdvanceRequest()
  27.                 .setImageURLObject(inputStream);
  28.         com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
  29.         try {
  30.             SegmentCommonImageResponse segmentCommonImageResponse = client.segmentCommonImageAdvance(segmentCommonImageAdvanceRequest, runtime);
  31.             // 获取整体结果
  32.             System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(segmentCommonImageResponse)));
  33.             // 获取单个字段
  34.             System.out.println(segmentCommonImageResponse.getBody().data.imageURL);
  35.         } catch (TeaException teaException) {
  36.             // 获取整体报错信息
  37.             System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
  38.             // 获取单个字段
  39.             System.out.println(teaException.getCode());
  40.         }
  41.     }
  42. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

去皮卡多

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表