test.java 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package com.fdkankan.scene.util;
  2. // This file is auto-generated, don't edit it. Thanks.
  3. import cn.hutool.core.io.FileUtil;
  4. import cn.hutool.core.thread.ThreadUtil;
  5. import cn.hutool.http.HttpUtil;
  6. import com.aliyun.imageseg20191230.Client;
  7. import com.aliyun.imageseg20191230.models.SegmentBodyResponse;
  8. import com.aliyun.tea.TeaException;
  9. import com.aliyun.teautil.Common;
  10. import java.io.File;
  11. public class test {
  12. /**
  13. * 使用AK&SK初始化账号Client
  14. *
  15. * @param accessKeyId
  16. * @param accessKeySecret
  17. * @return Client
  18. * @throws Exception
  19. */
  20. public static com.aliyun.imageseg20191230.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
  21. com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
  22. // 必填,您的 AccessKey ID
  23. .setAccessKeyId(accessKeyId)
  24. // 必填,您的 AccessKey Secret
  25. .setAccessKeySecret(accessKeySecret);
  26. // 访问的域名
  27. config.endpoint = "imageseg.cn-shanghai.aliyuncs.com";
  28. return new com.aliyun.imageseg20191230.Client(config);
  29. }
  30. public static void main(String[] args_) throws Exception {
  31. java.util.List<String> args = java.util.Arrays.asList(args_);
  32. // 工程代码泄露可能会导致AccessKey泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html
  33. com.aliyun.imageseg20191230.Client client = test.createClient("LTAIUrvuHqj8pvry", "JLOVl0k8Ke0aaM8nLMMiUAZ3EiiqI4");
  34. ThreadUtil.execAsync(() -> {
  35. extracted(client, "https://4dkankan-huadong.oss-cn-shanghai.aliyuncs.com/segment/1.jpg", "1");
  36. });
  37. ThreadUtil.execAsync(() -> {
  38. extracted(client, "https://4dkankan-huadong.oss-cn-shanghai.aliyuncs.com/segment/2.jpg", "2");
  39. });
  40. ThreadUtil.sleep(1000);
  41. ThreadUtil.execAsync(() -> {
  42. extracted(client, "https://4dkankan-huadong.oss-cn-shanghai.aliyuncs.com/segment/3.jpg", "3");
  43. });
  44. ThreadUtil.execAsync(() -> {
  45. extracted(client, "https://4dkankan-huadong.oss-cn-shanghai.aliyuncs.com/segment/4.webp", "4");
  46. });
  47. // ThreadUtil.sleep(2000);
  48. // ThreadUtil.sleep(3000);
  49. // ThreadUtil.sleep(4000);
  50. }
  51. private static void extracted(Client client, String imageUrl, String imgName) {
  52. com.aliyun.imageseg20191230.models.SegmentBodyRequest segmentBodyRequest = new com.aliyun.imageseg20191230.models.SegmentBodyRequest()
  53. .setImageURL(imageUrl);
  54. com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
  55. try {
  56. // 复制代码运行请自行打印 API 的返回值
  57. long stime = System.currentTimeMillis();
  58. System.out.println("开始-----------" + imgName);
  59. SegmentBodyResponse segmentBodyResponse = client.segmentBodyWithOptions(segmentBodyRequest, runtime);
  60. String imageURL = segmentBodyResponse.getBody().getData().getImageURL();
  61. long etime = System.currentTimeMillis();
  62. System.out.printf("执行时长:%d 毫秒.----%s", (etime - stime), imgName);
  63. File file = FileUtil.file("C:\\Users\\4DAGE\\Downloads\\抠像测试\\抠像测试\\1\\" + imgName + ".png");
  64. if (FileUtil.exist(file)) {
  65. FileUtil.del(file);
  66. }
  67. System.out.println(imageURL);
  68. ThreadUtil.sleep(1000);
  69. HttpUtil.downloadFile(imageURL, FileUtil.file(file));
  70. } catch (TeaException error) {
  71. // 如有需要,请打印 error
  72. String s = Common.assertAsString(error.message);
  73. System.out.println("错误1" + s);
  74. } catch (Exception _error) {
  75. TeaException error = new TeaException(_error.getMessage(), _error);
  76. // 如有需要,请打印 error
  77. String s = Common.assertAsString(error.message);
  78. System.out.println("错误2" + s);
  79. }
  80. }
  81. }