【sdk】- 对接阿里云抠图

文档地址:https://help.aliyun.com/zh/viapi/use-cases/general-image-segmentation?spm=a2c4g.11186623.0.0.3814173cenldIs

java对接阿里云的通用分割,将代码原封不动复制进来,执行结果失败,咨询阿里云的人员之后,由于jar包冲突导致,需要引入一些特定的包版本来实现

pom引用

xml 复制代码
  <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>imageseg20191230</artifactId>
            <version>2.0.2</version>
            <exclusions>
                <exclusion>
                    <groupId>com.squareup.okhttp3</groupId>
                    <artifactId>okhttp</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>tea-openapi</artifactId>
            <version>0.3.4</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>tea-console</artifactId>
            <version>0.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>tea-util</artifactId>
            <version>0.2.22</version>
        </dependency>
      <!--在我的服务中必须手动再次引人,不然报错-->
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
        </dependency>

代码

只需要改pom,对于获取环境变量中的ak信息,完全没必要。

java 复制代码
public class AliyunApiService {
    public static com.aliyun.imageseg20191230.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        /*
          初始化配置对象com.aliyun.teaopenapi.models.Config
          Config对象存放 AccessKeyId、AccessKeySecret、endpoint等配置
         */
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "imageseg.cn-shanghai.aliyuncs.com";
        return new com.aliyun.imageseg20191230.Client(config);
    }

    public static void main(String[] args_) throws Exception {


        // 创建AccessKey ID和AccessKey Secret,请参考https://help.aliyun.com/document_detail/175144.html
        // 如果您使用的是RAM用户的AccessKey,还需要为子账号授予权限AliyunVIAPIFullAccess,请参考https://help.aliyun.com/document_detail/145025.html
        // 从环境变量读取配置的AccessKey ID和AccessKey Secret。运行代码示例前必须先配置环境变量。
        String accessKeyId = "";
        String accessKeySecret = "";
        com.aliyun.imageseg20191230.Client client = AliyunApiService.createClient(accessKeyId, accessKeySecret);
        // 场景一,使用本地文件
//         InputStream inputStream = new FileInputStream(new File("/Users/qweasdzxc/Downloads/WechatIMG201.jpeg"));
        // 场景二,使用任意可访问的url
        URL url = new URL("https://viapi-test-bj.oss-cn-beijing.aliyuncs.com/viapi-3.0domepic/imageseg/SegmentCommonImage/SegmentCommonImage1.jpg");
        InputStream inputStream = url.openConnection().getInputStream();
        com.aliyun.imageseg20191230.models.SegmentCommonImageAdvanceRequest segmentCommonImageAdvanceRequest = new com.aliyun.imageseg20191230.models.SegmentCommonImageAdvanceRequest()
                .setImageURLObject(inputStream);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            SegmentCommonImageResponse segmentCommonImageResponse = client.segmentCommonImageAdvance(segmentCommonImageAdvanceRequest, runtime);
            // 获取整体结果
            System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(segmentCommonImageResponse)));
            // 获取单个字段
            System.out.println(segmentCommonImageResponse.getBody().data.imageURL);
        } catch (TeaException teaException) {
            // 获取整体报错信息
            System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
            // 获取单个字段
            System.out.println(teaException.getCode());
        }
    }
}
相关推荐
茜茜西西CeCe12 分钟前
移动技术开发:登录注册界面
java·gitee·gradle·android studio·安卓·移动技术开发·原生安卓开发
linux_lzj_cainiao13 分钟前
准备招银社招记录
java
不是编程家17 分钟前
C++ 第三讲:内存管理
java·开发语言·c++
尸僵打怪兽18 分钟前
软考(中级-软件设计师)(0919)
java·c语言·数据库·计算机网络·软考·多媒体·软件设计师
Liii40329 分钟前
【ARM】Cache深度解读
java·arm开发·spring
litGrey1 小时前
Maven国内镜像(四种)
java·数据库·maven
丶白泽2 小时前
重修设计模式-结构型-桥接模式
java·设计模式·桥接模式
o独酌o2 小时前
递归的‘浅’理解
java·开发语言
无问8172 小时前
数据结构-排序(冒泡,选择,插入,希尔,快排,归并,堆排)
java·数据结构·排序算法
customer082 小时前
【开源免费】基于SpringBoot+Vue.JS在线文档管理系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源