package com.fdkankan.scene.util; // This file is auto-generated, don't edit it. Thanks. import cn.hutool.core.io.FileUtil; import cn.hutool.core.thread.ThreadUtil; import cn.hutool.http.HttpUtil; import com.aliyun.imageseg20191230.Client; import com.aliyun.imageseg20191230.models.SegmentBodyResponse; import com.aliyun.tea.TeaException; import com.aliyun.teautil.Common; import java.io.File; public class test { /** * 使用AK&SK初始化账号Client * * @param accessKeyId * @param accessKeySecret * @return Client * @throws Exception */ public static com.aliyun.imageseg20191230.Client createClient(String accessKeyId, String accessKeySecret) throws Exception { com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config() // 必填,您的 AccessKey ID .setAccessKeyId(accessKeyId) // 必填,您的 AccessKey Secret .setAccessKeySecret(accessKeySecret); // 访问的域名 config.endpoint = "imageseg.cn-shanghai.aliyuncs.com"; return new com.aliyun.imageseg20191230.Client(config); } public static void main(String[] args_) throws Exception { java.util.List args = java.util.Arrays.asList(args_); // 工程代码泄露可能会导致AccessKey泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html com.aliyun.imageseg20191230.Client client = test.createClient("LTAIUrvuHqj8pvry", "JLOVl0k8Ke0aaM8nLMMiUAZ3EiiqI4"); ThreadUtil.execAsync(() -> { extracted(client, "https://4dkankan-huadong.oss-cn-shanghai.aliyuncs.com/segment/1.jpg", "1"); }); ThreadUtil.execAsync(() -> { extracted(client, "https://4dkankan-huadong.oss-cn-shanghai.aliyuncs.com/segment/2.jpg", "2"); }); ThreadUtil.sleep(1000); ThreadUtil.execAsync(() -> { extracted(client, "https://4dkankan-huadong.oss-cn-shanghai.aliyuncs.com/segment/3.jpg", "3"); }); ThreadUtil.execAsync(() -> { extracted(client, "https://4dkankan-huadong.oss-cn-shanghai.aliyuncs.com/segment/4.webp", "4"); }); // ThreadUtil.sleep(2000); // ThreadUtil.sleep(3000); // ThreadUtil.sleep(4000); } private static void extracted(Client client, String imageUrl, String imgName) { com.aliyun.imageseg20191230.models.SegmentBodyRequest segmentBodyRequest = new com.aliyun.imageseg20191230.models.SegmentBodyRequest() .setImageURL(imageUrl); com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions(); try { // 复制代码运行请自行打印 API 的返回值 long stime = System.currentTimeMillis(); System.out.println("开始-----------" + imgName); SegmentBodyResponse segmentBodyResponse = client.segmentBodyWithOptions(segmentBodyRequest, runtime); String imageURL = segmentBodyResponse.getBody().getData().getImageURL(); long etime = System.currentTimeMillis(); System.out.printf("执行时长:%d 毫秒.----%s", (etime - stime), imgName); File file = FileUtil.file("C:\\Users\\4DAGE\\Downloads\\抠像测试\\抠像测试\\1\\" + imgName + ".png"); if (FileUtil.exist(file)) { FileUtil.del(file); } System.out.println(imageURL); ThreadUtil.sleep(1000); HttpUtil.downloadFile(imageURL, FileUtil.file(file)); } catch (TeaException error) { // 如有需要,请打印 error String s = Common.assertAsString(error.message); System.out.println("错误1" + s); } catch (Exception _error) { TeaException error = new TeaException(_error.getMessage(), _error); // 如有需要,请打印 error String s = Common.assertAsString(error.message); System.out.println("错误2" + s); } } }