提交修改

This commit is contained in:
quyixiao 2025-10-14 08:40:10 +08:00
parent 87a8c9bb2a
commit d648093ab5

View File

@ -0,0 +1,127 @@
package com.heyu.api.alibaba;// Copyright (c) Alibaba, Inc. and its affiliates.
import com.alibaba.dashscope.aigc.videosynthesis.VideoSynthesis;
import com.alibaba.dashscope.aigc.videosynthesis.VideoSynthesisParam;
import com.alibaba.dashscope.aigc.videosynthesis.VideoSynthesisResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.JsonUtils;
import com.alibaba.dashscope.utils.Constants;
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 Image2Video {
static {
// 以下为北京地域url若使用新加坡地域的模型需将url替换为https://dashscope-intl.aliyuncs.com/api/v1
Constants.baseHttpApiUrl = "https://dashscope.aliyuncs.com/api/v1";
}
// 若没有配置环境变量请用百炼API Key将下行替换为apiKey="sk-xxx"
// 新加坡和北京地域的API Key不同获取API Keyhttps://help.aliyun.com/zh/model-studio/get-api-key
//static String apiKey = System.getenv("DASHSCOPE_API_KEY");
/**
* 图像输入方式说明三选一即可
*
* 1. 使用公网URL - 适合已有公开可访问的图片
* 2. 使用本地文件 - 适合本地开发测试
* 3. 使用Base64编码 - 适合私有图片或需要加密传输的场景
*/
//方式一公网URL
static String imgUrl = "https://gw.alicdn.com/bao/uploaded/i4/2219725998116/O1CN01jsogzI29pBWXEdU7M_!!2219725998116.jpg_.webp";
//方式二本地文件路径file://+绝对路径
// static String imgUrl = "file://" + "/your/path/to/img.png"; // Linux/macOS
// static String imgUrl = "file://" + "C:/your/path/to/img.png"; // Windows
//方式三Base64编码
// static String imgUrl = Image2Video.encodeFile("/your/path/to/img.png");
public static void image2video() throws ApiException, NoApiKeyException, InputRequiredException {
// 设置parameters参数
Map<String, Object> parameters = new HashMap<>();
/***
* 是否开启prompt智能改写开启后使用大模型对输入prompt进行智能改写对于较短的prompt生成效果提升明显但会增加耗时
*
* true默认值开启智能改写
*
* false不开启智能改写
*
* 示例值true
*/
parameters.put("prompt_extend", true);
VideoSynthesis vs = new VideoSynthesis();
VideoSynthesisParam param =
VideoSynthesisParam.builder()
.apiKey(LLMUtils.apiKey)
.model("wan2.2-i2v-plus")
.prompt("基于项链生成一个商品宣传视频")
.imgUrl(imgUrl)
.parameters(parameters)
.resolution("1080P")
.build();
System.out.println("please wait...");
VideoSynthesisResult result = vs.call(param);
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) {
try {
image2video();
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.out.println(e.getMessage());
}
System.exit(0);
}
}