main
HuangHai 2 months ago
parent 0af4adbdc5
commit 4f4701f153

@ -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");
}
}

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

@ -80,7 +80,7 @@ public class JiMengUtil {
}
private static void doRequest(String method, Map<String, String> queryList, byte[] body, String saveImgPath) throws Exception {
private static String doRequest(String method, Map<String, String> 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());
}
}

Loading…
Cancel
Save