diff --git a/dsAiSupport/src/main/java/com/dsideal/aiSupport/Test/JiMengImage.java b/dsAiSupport/src/main/java/com/dsideal/aiSupport/Test/JiMengImage.java index b7705507..d29367cc 100644 --- a/dsAiSupport/src/main/java/com/dsideal/aiSupport/Test/JiMengImage.java +++ b/dsAiSupport/src/main/java/com/dsideal/aiSupport/Test/JiMengImage.java @@ -4,6 +4,6 @@ import com.dsideal.aiSupport.Util.JiMengUtil; public class JiMengImage { public static void main(String[] args) throws Exception { String prompt="制作一张vlog视频封面。马卡龙配色,美女旅游照片+色块的拼贴画风格,主文案是“威海旅游vlog”,副文案是“特种兵一日游 被低估的旅游城市”,海报主体是一个穿着短裙、梳双马尾的少女,人物白色描边"; - JiMengUtil.generateImage(prompt); + JiMengUtil.generateImage(prompt,"d:/Temp/1.jpg"); } } diff --git a/dsAiSupport/src/main/java/com/dsideal/aiSupport/Test/JiMengVideo.java b/dsAiSupport/src/main/java/com/dsideal/aiSupport/Test/JiMengVideo.java new file mode 100644 index 00000000..48a224c6 --- /dev/null +++ b/dsAiSupport/src/main/java/com/dsideal/aiSupport/Test/JiMengVideo.java @@ -0,0 +1,10 @@ +package com.dsideal.aiSupport.Test; + +import com.dsideal.aiSupport.Util.JiMengUtil; + +public class JiMengVideo { + public static void main(String[] args) throws Exception { + String prompt = "蓝色毛绒玩具在超市里拖地,结果拖把洒出好多五颜六色的粉末,接着把粉末洒向镜头前,镜头随之穿过粉末"; + //JiMengUtil.generateVideo(prompt); + } +} diff --git a/dsAiSupport/src/main/java/com/dsideal/aiSupport/Util/JiMengUtil.java b/dsAiSupport/src/main/java/com/dsideal/aiSupport/Util/JiMengUtil.java index f93a9193..ebb10de5 100644 --- a/dsAiSupport/src/main/java/com/dsideal/aiSupport/Util/JiMengUtil.java +++ b/dsAiSupport/src/main/java/com/dsideal/aiSupport/Util/JiMengUtil.java @@ -80,7 +80,7 @@ public class JiMengUtil { } - private static void doRequest(String method, Map queryList, byte[] body, String saveImgPath) throws Exception { + private static String doRequest(String method, Map queryList, byte[] body) throws Exception { Date date = new Date(); if (body == null) { @@ -149,21 +149,7 @@ public class JiMengUtil { String responseBody = new String(ByteStreams.toByteArray(is)); is.close(); - - JSONObject jo = JSON.parseObject(responseBody); - String imgBase64 = jo.getJSONObject("data").getJSONArray("binary_data_base64").getFirst().toString(); - - // 对 Base64 字符串进行解码 - Base64.Decoder decoder = Base64.getDecoder(); - byte[] bytes = decoder.decode(imgBase64); - - try (OutputStream os = new FileOutputStream(saveImgPath)) { // 使用-with try-resources 自动关闭资源 - os.write(bytes); - os.flush(); // 刷新缓冲区,确保数据写入文件 - System.out.println("文件保存成功!"); - } catch (Exception e) { - e.printStackTrace(); - } + return responseBody; } private static String signStringEncoder(String source) { @@ -222,11 +208,37 @@ public class JiMengUtil { * @param prompt 提示词 * @throws Exception */ - public static void generateImage(String prompt) throws Exception { - String saveImgPath = "d:/Temp/1.jpg"; + public static void generateImage(String prompt, String saveImgPath) throws Exception { JSONObject req = new JSONObject(); req.put("req_key", "jimeng_high_aes_general_v21_L"); req.put("prompt", prompt); - doRequest("POST", new HashMap<>(), req.toString().getBytes(), saveImgPath); + String responseBody = doRequest("POST", new HashMap<>(), req.toString().getBytes()); + JSONObject jo = JSON.parseObject(responseBody); + String imgBase64 = jo.getJSONObject("data").getJSONArray("binary_data_base64").getFirst().toString(); + // 对 Base64 字符串进行解码 + Base64.Decoder decoder = Base64.getDecoder(); + byte[] bytes = decoder.decode(imgBase64); + + try (OutputStream os = new FileOutputStream(saveImgPath)) { // 使用-with try-resources 自动关闭资源 + os.write(bytes); + os.flush(); // 刷新缓冲区,确保数据写入文件 + System.out.println("文件保存成功!"); + } catch (Exception e) { + e.printStackTrace(); + } + + } + + /** + * 生成视频 + * + * @param prompt 提示词 + * @throws Exception + */ + public static void generateVideo(String prompt) throws Exception { + JSONObject req = new JSONObject(); + req.put("req_key", "jimeng_vgfm_t2v_l20"); + req.put("prompt", prompt); + doRequest("POST", new HashMap<>(), req.toString().getBytes()); } }