main
HuangHai 2 months ago
parent 2198fad7e0
commit 6da8a2b22d

@ -1,17 +1,15 @@
package com.dsideal.aiSupport.Util.JiMeng;
import lombok.Getter;
/**
* API
*/
@Getter
public enum JmErrorCode {
SUCCESS(10000, "请求成功"),
TEXT_RISK_NOT_PASS(50412, "输入文本前审核未通过"),
POST_TEXT_RISK_NOT_PASS(50413, "输入文本NER、IP、Blocklist等拦截"),
INTERNAL_ERROR(50500, "输出视频审核未通过");
INTERNAL_ERROR(50500, "输出视频审核未通过"),
API_CONCURRENT_LIMIT(50430, "请求已达到API并发限制请稍后重试");
private final int code;
private final String message;
@ -20,7 +18,15 @@ public enum JmErrorCode {
this.code = code;
this.message = message;
}
public int getCode() {
return code;
}
public String getMessage() {
return message;
}
/**
*
* @param code

@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.*;
@ -19,15 +20,35 @@ public class JmText2Image extends JmCommon {
*
*
* @param prompt
* @throws Exception
* @param saveImgPath
* @throws Exception
*/
public static void generateImage(String prompt, String saveImgPath) throws Exception {
JSONObject req = new JSONObject();
req.put("req_key", req_key);
req.put("prompt", prompt);
String responseBody = doRequest("POST", new HashMap<>(), req.toString().getBytes(), action);
JSONObject jo = JSON.parseObject(responseBody);
// 检查响应状态码
int code = jo.getInteger("code");
if (!JmErrorCode.isSuccess(code)) {
log.error("生成图片失败: 错误码={}, 错误信息={}", code, JmErrorCode.getMessageByCode(code));
throw new Exception("生成图片失败: " + JmErrorCode.getMessageByCode(code));
}
// 获取图片Base64数据
String imgBase64 = jo.getJSONObject("data").getJSONArray("binary_data_base64").getFirst().toString();
// 确保目录存在
File file = new File(saveImgPath);
File parentDir = file.getParentFile();
if (parentDir != null && !parentDir.exists()) {
parentDir.mkdirs();
log.info("创建目录: {}", parentDir.getAbsolutePath());
}
// 对 Base64 字符串进行解码
Base64.Decoder decoder = Base64.getDecoder();
byte[] bytes = decoder.decode(imgBase64);
@ -35,19 +56,48 @@ public class JmText2Image extends JmCommon {
try (OutputStream os = new FileOutputStream(saveImgPath)) { // 使用-with try-resources 自动关闭资源
os.write(bytes);
os.flush(); // 刷新缓冲区,确保数据写入文件
log.info("文件保存成功!文件位置:" + saveImgPath);
log.info("文件保存成功!文件位置: {}", saveImgPath);
} catch (Exception e) {
log.error("文件保存失败!" + e.getMessage());
log.error("文件保存失败!{}", e.getMessage(), e);
throw new Exception("文件保存失败: " + e.getMessage(), e);
}
}
public static void main(String[] args) throws Exception {
//更多例子参考https://www.volcengine.com/docs/85621/1537648
//String prompt = "制作一张vlog视频封面。马卡龙配色美女旅游照片+色块的拼贴画风格,主文案是“威海旅游vlog”副文案是“特种兵一日游 被低估的旅游城市”,海报主体是一个穿着短裙、梳双马尾的少女,人物白色描边";
//String prompt = "制作一张vlog视频封面。马卡龙配色美女旅游照片+色块的拼贴画风格,主文案是"威海旅游vlog",副文案是"特种兵一日游 被低估的旅游城市",海报主体是一个穿着短裙、梳双马尾的少女,人物白色描边";
//String prompt = "过曝强对比夜晚雪地里巨大的黄色浴缸小狗泡澡带墨镜在喝红酒胶片摄影毛刺质感复古滤镜夜晚过度曝光古早70年代摄影复古老照片闪光灯拍摄闪光灯效果过曝过度曝光闪光灯过曝极简高饱和复古色70s vintage photography, vintage, retro style";
String prompt="南瓜羹->画面展现一碗百合南瓜羹的一半,米黄色的糯米粉勾芡,块块橙色南瓜在橙色粥中,南瓜丝丝沙沙质感,紫白色百合点缀";
// 获取项目根目录路径
String projectRoot = System.getProperty("user.dir");
// 拼接相对路径
String basePath = projectRoot + "/src/main/java/com/dsideal/aiSupport/Util/JiMeng/Example/";
String saveImagePath = basePath + "3.jpg";
log.info("保存图片路径:" + saveImagePath);
JmText2Image.generateImage(prompt, saveImagePath);
log.info("保存图片路径:{}", saveImagePath);
// 添加重试逻辑处理API并发限制错误
int retryCount = 0;
int maxRetries = 5; // 最大重试次数
int retryInterval = 5000; // 重试间隔(毫秒)
while (retryCount < maxRetries) {
try {
JmText2Image.generateImage(prompt, saveImagePath);
// 成功生成图片,跳出循环
break;
} catch (Exception e) {
log.error("生成图片异常: {}", e.getMessage());
retryCount++;
if (retryCount < maxRetries) {
log.warn("等待{}毫秒后重试...", retryInterval);
Thread.sleep(retryInterval);
} else {
log.error("已达到最大重试次数: {}", maxRetries);
throw e; // 达到最大重试次数,抛出异常
}
}
}
}
}

@ -85,12 +85,44 @@ public class JmText2Video extends JmCommon {
log.info("创建目录: {}", basePath);
}
JSONObject jo = JmText2Video.generateVideo(prompt);
log.info("结果:{}", jo);
// 添加重试逻辑处理API并发限制错误
JSONObject jo = null;
int generateRetryCount = 0;
int maxGenerateRetries = 5; // 最大重试次数
int generateRetryInterval = 5000; // 重试间隔(毫秒)
int code = jo.getInteger("code");
if (!JmErrorCode.isSuccess(code)) {
log.error("生成视频失败: 错误码={}, 错误信息={}", code, JmErrorCode.getMessageByCode(code));
while (generateRetryCount < maxGenerateRetries) {
try {
jo = JmText2Video.generateVideo(prompt);
log.info("结果:{}", jo);
int code = jo.getInteger("code");
if (code == JmErrorCode.API_CONCURRENT_LIMIT.getCode()) {
log.warn("API并发限制等待{}毫秒后重试...", generateRetryInterval);
Thread.sleep(generateRetryInterval);
generateRetryCount++;
continue;
} else if (!JmErrorCode.isSuccess(code)) {
log.error("生成视频失败: 错误码={}, 错误信息={}", code, JmErrorCode.getMessageByCode(code));
return;
}
// 成功获取结果,跳出循环
break;
} catch (Exception e) {
log.error("生成视频异常: {}", e.getMessage(), e);
generateRetryCount++;
if (generateRetryCount < maxGenerateRetries) {
log.warn("等待{}毫秒后重试...", generateRetryInterval);
Thread.sleep(generateRetryInterval);
} else {
throw e; // 达到最大重试次数,抛出异常
}
}
}
if (jo == null || generateRetryCount >= maxGenerateRetries) {
log.error("生成视频失败,已达到最大重试次数: {}", maxGenerateRetries);
return;
}
@ -106,7 +138,7 @@ public class JmText2Video extends JmCommon {
JSONObject result = queryTaskResult(taskId);
log.info("查询结果:{}", result);
code = result.getInteger("code");
int code = result.getInteger("code");
if (!JmErrorCode.isSuccess(code)) {
log.error("查询失败: 错误码={}, 错误信息={}", code, JmErrorCode.getMessageByCode(code));
break;

Loading…
Cancel
Save