Step By Step
1.调用准备
- 开通oss,并创建bucket
- 开通头像分割服务
- 本地图片一张
2.添加依赖
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.15.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>imageseg20191230</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.79</version>
</dependency>
3.java Code
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.aliyun.oss.*;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.google.gson.Gson;
import org.codehaus.jettison.json.JSONObject;
import java.io.File;
import java.util.Date;
public class Demo {
public static com.aliyun.imageseg20191230.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
Config config = new Config()
// 您的 AccessKey ID
.setAccessKeyId(accessKeyId)
// 您的 AccessKey Secret
.setAccessKeySecret(accessKeySecret);
// 访问的域名
config.endpoint = "imageseg.cn-shanghai.aliyuncs.com";
return new com.aliyun.imageseg20191230.Client(config);
}
//此方法将接口返回的url处理了下,如果官方修复了问题,可以直接获取接口内容
public static String detailFormat(String StringBody) throws Exception{
String data = new JSONObject(StringBody).getString("data");
String elements = new JSONObject(data).getString("elements");
JSONArray jsonArray = JSONArray.parseArray(elements);
JSON jsonOne = jsonArray.getJSONObject(0);
String jsonOneToString = jsonOne.toJSONString();
JSONObject json = new JSONObject(jsonOneToString);
String imageUrl = json.getString("imageURL");
return imageUrl;
}
public static void main(String[] args) throws Exception {
// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
String endpoint = "https://oss-cn-shanghai.aliyuncs.com";
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
String accessKeyId = "您的AK";
String accessKeySecret = "您的SK";
// 填写Bucket名称,例如examplebucket。
String bucketName = "bucket名字";
String filePath= "本地图片";
String filename = "(文件夹名字)将图片传入特定文件夹/"+filePath.substring(filePath.lastIndexOf("\\")+1);
String objectName = filename;
System.out.println(filename);
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, objectName, HttpMethod.PUT);
// 设置签名URL过期时间,单位为毫秒。
Date expiration = new Date(new Date().getTime() + 3600 * 1000);
// 创建PutObjectRequest对象。
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, new File(filePath));
// 上传文件。
ossClient.putObject(putObjectRequest);
System.out.println("上传成功");
//获取上传图片的url
String url = ossClient.generatePresignedUrl(bucketName,objectName, expiration).toString();
System.out.println("oss获取的地址:"+url);
//调用头像分割接口
com.aliyun.imageseg20191230.Client client = Demo.createClient(accessKeyId, accessKeySecret);
com.aliyun.imageseg20191230.models.SegmentHeadRequest segmentHeadRequest = new com.aliyun.imageseg20191230.models.SegmentHeadRequest()
.setImageURL(url);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// 复制代码运行请自行打印 API 的返回值
com.aliyun.imageseg20191230.models.SegmentHeadResponse result = client.segmentHeadWithOptions(segmentHeadRequest, runtime);
String body = new Gson().toJson(result.getBody());
//接口返回的url有转义字符的问题,需要单独拿出来处理下
System.out.println("接口返回的Body:"+body);
String finalUrl = detailFormat(body);
System.out.println("最终的url:"+finalUrl);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// 如有需要,请打印 error
com.aliyun.teautil.Common.assertAsString(error.message);
}
} catch (OSSException oe) {
System.out.println("ossE");
} catch (ClientException ce) {
System.out.println("clientE");
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
4.运行结果
最终的图片