|
|
|
@ -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; // 达到最大重试次数,抛出异常
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|