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(); } }