diff --git a/api-third/src/main/java/com/heyu/api/alibaba/ImageEditSync.java b/api-third/src/main/java/com/heyu/api/alibaba/ImageEditSync.java new file mode 100644 index 0000000..4e70bc6 --- /dev/null +++ b/api-third/src/main/java/com/heyu/api/alibaba/ImageEditSync.java @@ -0,0 +1,112 @@ +package com.heyu.api.alibaba;// Copyright (c) Alibaba, Inc. and its affiliates. + +import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis; +import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisParam; +import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisResult; +import com.alibaba.dashscope.exception.ApiException; +import com.alibaba.dashscope.exception.NoApiKeyException; +import com.alibaba.dashscope.utils.JsonUtils; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Base64; +import java.util.HashMap; +import java.util.Map; + +/** + * 环境要求 + * dashscope java SDK >=2.20.9 + * 更新maven依赖: + * https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java + */ + +public class ImageEditSync { + + /** + * 图像输入方式说明:三选一即可 + * + * 1. 使用公网URL - 适合已有公开可访问的图片 + * 2. 使用本地文件 - 适合本地开发测试 + * 3. 使用Base64编码 - 适合私有图片或需要加密传输的场景 + */ + + //【方式一】公网URL + static String maskImageUrl = "https://help-static-aliyun-doc.aliyuncs.com/assets/img/zh-CN/2971781571/p942929.png"; + static String baseImageUrl = "https://gw.alicdn.com/bao/uploaded/i2/2219725998116/O1CN01c1MATl29pBWjNu3MS_!!2219725998116.jpg_.webp"; + + //【方式二】本地文件路径(file://+绝对路径 or file:///+绝对路径) + // static String maskImageUrl = "file://" + "/your/path/to/mask_image.png"; // Linux/macOS + // static String baseImageUrl = "file:///" + "C:/your/path/to/base_image.png"; // Windows + + //【方式三】Base64编码 + // static String maskImageUrl = encodeFile("/your/path/to/mask_image.png"); + // static String baseImageUrl = encodeFile("/your/path/to/base_image.png"); + + + public static void syncCall() { + // 设置parameters参数 + Map parameters = new HashMap<>(); + parameters.put("prompt_extend", true); + + ImageSynthesisParam param = + ImageSynthesisParam.builder() + .apiKey(LLMUtils.apiKey) + .model("wanx2.1-imageedit") + .function(ImageSynthesis.ImageEditFunction.DESCRIPTION_EDIT_WITH_MASK) + .prompt("陶瓷兔子拿着陶瓷小花") + .maskImageUrl(maskImageUrl) + .baseImageUrl(baseImageUrl) + .n(1) + .size("1024*1024") + .parameters(parameters) + .build(); + + ImageSynthesis imageSynthesis = new ImageSynthesis(); + ImageSynthesisResult result = null; + try { + System.out.println("---sync call, please wait a moment----"); + result = imageSynthesis.call(param); + } catch (ApiException | NoApiKeyException e){ + throw new RuntimeException(e.getMessage()); + } + System.out.println(JsonUtils.toJson(result)); + } + + /** + * 将文件编码为Base64字符串 + * @param filePath 文件路径 + * @return Base64字符串,格式为 data:{MIME_type};base64,{base64_data} + */ + public static String encodeFile(String filePath) { + Path path = Paths.get(filePath); + if (!Files.exists(path)) { + throw new IllegalArgumentException("文件不存在: " + filePath); + } + // 检测MIME类型 + String mimeType = null; + try { + mimeType = Files.probeContentType(path); + } catch (IOException e) { + throw new IllegalArgumentException("无法检测文件类型: " + filePath); + } + if (mimeType == null || !mimeType.startsWith("image/")) { + throw new IllegalArgumentException("不支持或无法识别的图像格式"); + } + // 读取文件内容并编码 + byte[] fileBytes = null; + try{ + fileBytes = Files.readAllBytes(path); + } catch (IOException e) { + throw new IllegalArgumentException("无法读取文件内容: " + filePath); + } + + String encodedString = Base64.getEncoder().encodeToString(fileBytes); + return "data:" + mimeType + ";base64," + encodedString; + } + + public static void main(String[] args) { + syncCall(); + } +} \ No newline at end of file diff --git a/api-third/src/main/java/com/heyu/api/alibaba/Main.java b/api-third/src/main/java/com/heyu/api/alibaba/Main.java new file mode 100644 index 0000000..baf1d7d --- /dev/null +++ b/api-third/src/main/java/com/heyu/api/alibaba/Main.java @@ -0,0 +1,58 @@ +package com.heyu.api.alibaba;// DashScope SDK 版本不低于2.21.9 +// 2.20.7 及以上版本支持指定 Dylan、Jada与 Sunny 三种音色 + +import com.alibaba.dashscope.aigc.multimodalconversation.AudioParameters; +import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversation; +import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationParam; +import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult; +import com.alibaba.dashscope.exception.ApiException; +import com.alibaba.dashscope.exception.NoApiKeyException; +import com.alibaba.dashscope.exception.UploadFileException; + +import java.io.FileOutputStream; +import java.io.InputStream; +import java.net.URL; + +public class Main { + private static final String MODEL = "qwen3-tts-flash"; + + public static void call() throws ApiException, NoApiKeyException, UploadFileException { + + MultiModalConversation conv = new MultiModalConversation(); + + MultiModalConversationParam param = MultiModalConversationParam.builder() + .apiKey(LLMUtils.apiKey) + .model(MODEL) + .text("甲、乙双方在平等自愿的基础上,就甲方委托乙方提供工商注册、代理记账、报税等本合同约定的服务,经友好协商达成如下协议,以资共同遵守。") + .voice(AudioParameters.Voice.NOFISH) + .languageType("Chinese") // 建议与文本语种一致,以获得正确的发音和自然的语调。 + .build(); + + MultiModalConversationResult result = conv.call(param); + + String audioUrl = result.getOutput().getAudio().getUrl(); + + System.out.print(audioUrl); + + // 下载音频文件到本地 + try (InputStream in = new URL(audioUrl).openStream(); + FileOutputStream out = new FileOutputStream("/Users/quyixiao/Desktop/ocr/downloaded_audio.wav")) { + byte[] buffer = new byte[1024]; + int bytesRead; + while ((bytesRead = in.read(buffer)) != -1) { + out.write(buffer, 0, bytesRead); + } + System.out.println("\n音频文件已下载到本地: downloaded_audio.wav"); + } catch (Exception e) { + System.out.println("\n下载音频文件时出错: " + e.getMessage()); + } + } + public static void main(String[] args) { + try { + call(); + } catch (ApiException | NoApiKeyException | UploadFileException e) { + System.out.println(e.getMessage()); + } + System.exit(0); + } +} \ No newline at end of file diff --git a/api-third/src/main/java/com/heyu/api/alibaba/QwenImageEdit.java b/api-third/src/main/java/com/heyu/api/alibaba/QwenImageEdit.java index f91e61d..5a8c01e 100644 --- a/api-third/src/main/java/com/heyu/api/alibaba/QwenImageEdit.java +++ b/api-third/src/main/java/com/heyu/api/alibaba/QwenImageEdit.java @@ -32,19 +32,20 @@ public class QwenImageEdit { MultiModalConversation conv = new MultiModalConversation(); - MultiModalMessage userMessage = MultiModalMessage.builder().role(Role.USER.getValue()) - .content(Arrays.asList( - Collections.singletonMap("image", "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcbu01.alicdn.com%2Fimg%2Fibank%2F2019%2F790%2F571%2F12184175097_276213056.jpg&refer=http%3A%2F%2Fcbu01.alicdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1761752391&t=9d85136483c31600c81d6dfc20595979"), - Collections.singletonMap("text", "将珍珠戴在美女模特身上") - )).build(); + // MultiModalMessage userMessage = MultiModalMessage.builder().role(Role.USER.getValue()) + // .content(Arrays.asList( + // Collections.singletonMap("image", "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcbu01.alicdn.com%2Fimg%2Fibank%2F2019%2F790%2F571%2F12184175097_276213056.jpg&refer=http%3A%2F%2Fcbu01.alicdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1761752391&t=9d85136483c31600c81d6dfc20595979"), + // Collections.singletonMap("text", "将珍珠戴在美女模特身上") + // )).build(); + // 多图编辑示例 - // MultiModalMessage userMessage = MultiModalMessage.builder().role(Role.USER.getValue()) - // .content(Arrays.asList( - // Collections.singletonMap("image", "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250923/znhvuj/shoes1.webp"), - // Collections.singletonMap("image", "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250923/alubtv/shoes2.webp"), - // Collections.singletonMap("text", "用图中黄色的鞋替换图中白色的鞋") - // )).build(); + MultiModalMessage userMessage = MultiModalMessage.builder().role(Role.USER.getValue()) + .content(Arrays.asList( + Collections.singletonMap("image", "https://help-static-aliyun-doc.aliyuncs.com/assets/img/zh-CN/2971781571/p942929.png"), + Collections.singletonMap("image", "https://gw.alicdn.com/bao/uploaded/i2/2219725998116/O1CN01c1MATl29pBWjNu3MS_!!2219725998116.jpg_.webp"), + Collections.singletonMap("text", "将图2配戴在图1上") + )).build(); Map parameters = new HashMap<>(); parameters.put("watermark", false); diff --git a/downloaded_audio.wav b/downloaded_audio.wav new file mode 100644 index 0000000..5b924f9 Binary files /dev/null and b/downloaded_audio.wav differ diff --git a/pom.xml b/pom.xml index 8e0d818..d04bd0f 100644 --- a/pom.xml +++ b/pom.xml @@ -347,7 +347,7 @@ com.alibaba dashscope-sdk-java - 2.20.5 + 2.21.11