main
HuangHai 2 months ago
parent c3863c606a
commit 13aab13b71

Binary file not shown.

After

Width:  |  Height:  |  Size: 139 KiB

@ -8,11 +8,6 @@ import com.dsideal.aiSupport.Util.JiMeng.Kit.JmErrorCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
public class JmImg2Video extends JmCommon {
@ -44,49 +39,6 @@ public class JmImg2Video extends JmCommon {
return JSON.parseObject(responseBody);
}
/**
* URL
*
* @param fileUrl URL
* @param saveFilePath
* @throws Exception
*/
public static void downloadFile(String fileUrl, String saveFilePath) throws Exception {
URL url = new URL(fileUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(60000);
// 确保目录存在
File file = new File(saveFilePath);
File parentDir = file.getParentFile();
if (parentDir != null && !parentDir.exists()) {
parentDir.mkdirs();
log.info("创建目录: {}", parentDir.getAbsolutePath());
}
// 获取输入流
try (InputStream in = connection.getInputStream();
FileOutputStream out = new FileOutputStream(saveFilePath)) {
byte[] buffer = new byte[4096];
int bytesRead;
// 读取数据并写入文件
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
log.info("文件下载成功,保存路径: {}", saveFilePath);
} catch (Exception e) {
log.error("文件下载失败: {}", e.getMessage(), e);
throw e;
} finally {
connection.disconnect();
}
}
public static void main(String[] args) throws Exception {
//玩法:
//https://www.volcengine.com/docs/85621/1544774

@ -8,8 +8,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.*;
public class JmText2Img extends JmCommon {
@ -51,40 +49,30 @@ public class JmText2Img extends JmCommon {
log.info("创建目录: {}", parentDir.getAbsolutePath());
}
// 对 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(); // 刷新缓冲区,确保数据写入文件
log.info("文件保存成功!文件位置: {}", saveImgPath);
} catch (Exception e) {
log.error("文件保存失败!{}", e.getMessage(), e);
throw new Exception("文件保存失败: " + e.getMessage(), e);
}
// 对 Base64 字符串进行解码并保存为文件
byte[] bytes = Base64.getDecoder().decode(imgBase64);
cn.hutool.core.io.FileUtil.writeBytes(bytes, saveImgPath);
log.info("文件保存成功!文件位置: {}", saveImgPath);
}
public static void main(String[] args) throws Exception {
//更多例子参考https://www.volcengine.com/docs/85621/1537648
//String prompt = "制作一张vlog视频封面。马卡龙配色美女旅游照片+色块的拼贴画风格,主文案是"威海旅游vlog",副文案是"特种兵一日游 被低估的旅游城市",海报主体是一个穿着短裙、梳双马尾的少女,人物白色描边";
//String prompt = "过曝强对比夜晚雪地里巨大的黄色浴缸小狗泡澡带墨镜在喝红酒胶片摄影毛刺质感复古滤镜夜晚过度曝光古早70年代摄影复古老照片闪光灯拍摄闪光灯效果过曝过度曝光闪光灯过曝极简高饱和复古色70s vintage photography, vintage, retro style";
String prompt="南瓜羹->画面展现一碗百合南瓜羹的一半,米黄色的糯米粉勾芡,块块橙色南瓜在橙色粥中,南瓜丝丝沙沙质感,紫白色百合点缀";
//String prompt="南瓜羹->画面展现一碗百合南瓜羹的一半,米黄色的糯米粉勾芡,块块橙色南瓜在橙色粥中,南瓜丝丝沙沙质感,紫白色百合点缀";
//String prompt="一张海报,画面上方有手写涂鸦风格的文字写着:新年快乐";
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";
String saveImagePath = basePath + "Text2Img.jpg";
log.info("保存图片路径:{}", saveImagePath);
// 添加重试逻辑处理API并发限制错误
int retryCount = 0;
int maxRetries = 5; // 最大重试次数
int maxRetries = 1000; // 最大重试次数
int retryInterval = 5000; // 重试间隔(毫秒)
while (retryCount < maxRetries) {
while (true) {
try {
JmText2Img.generateImage(prompt, saveImagePath);
// 成功生成图片,跳出循环

@ -8,10 +8,6 @@ import com.jfinal.kit.StrKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
public class JmText2Video extends JmCommon {
@ -34,42 +30,6 @@ public class JmText2Video extends JmCommon {
return JSON.parseObject(responseBody);
}
/**
* URL
*
* @param fileUrl URL
* @param saveFilePath
* @throws Exception
*/
public static void downloadFile(String fileUrl, String saveFilePath) throws Exception {
URL url = new URL(fileUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(60000);
// 获取输入流
try (InputStream in = connection.getInputStream();
FileOutputStream out = new FileOutputStream(saveFilePath)) {
byte[] buffer = new byte[4096];
int bytesRead;
// 读取数据并写入文件
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
log.info("文件下载成功,保存路径: {}", saveFilePath);
} catch (Exception e) {
log.error("文件下载失败: {}", e.getMessage(), e);
throw e;
} finally {
connection.disconnect();
}
}
public static void main(String[] args) throws Exception {
String prompt = "蓝色毛绒玩具在超市里拖地,结果拖把洒出好多五颜六色的粉末,接着把粉末洒向镜头前,镜头随之穿过粉末";
//保存的文件名称

@ -1,14 +1,22 @@
package com.dsideal.aiSupport.Util.JiMeng.Kit;
import cn.hutool.http.Method;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dsideal.aiSupport.Plugin.YamlProp;
import com.google.common.io.ByteStreams;
import com.jfinal.kit.Prop;
import org.apache.commons.codec.binary.Hex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
@ -27,6 +35,8 @@ import static com.dsideal.aiSupport.AiSupportApplication.getEnvPrefix;
* API
*/
public class JmCommon {
private static final Logger log = LoggerFactory.getLogger(JmCommon.class);
// 请求域名
public static String host = "visual.volcengineapi.com";
public static String path = "/"; // 路径,不包含 Query
@ -35,20 +45,21 @@ public class JmCommon {
public static String region = "cn-north-1"; // 区域固定为cn-north-1
public static String schema = "https"; // 协议使用https
public static String version = "2022-08-31"; // API版本号
// URL编码相关常量
private static final BitSet URLENCODER = new BitSet(256);
private static final String CONST_ENCODE = "0123456789ABCDEF";
public static final Charset UTF_8 = StandardCharsets.UTF_8;
// API访问凭证
protected static final String ak; // Access Key
protected static final String sk; // Secret Key
public static Prop PropKit; // 配置文件工具
// 获取项目根目录路径
protected static String projectRoot = System.getProperty("user.dir").replace("\\","/")+"/dsAiSupport";
protected static String projectRoot = System.getProperty("user.dir").replace("\\", "/") + "/dsAiSupport";
// 拼接相对路径
protected static String basePath = projectRoot + "/src/main/java/com/dsideal/aiSupport/Util/JiMeng/Example/";
/**
*
* URL
@ -77,14 +88,13 @@ public class JmCommon {
URLENCODER.set('~');
}
/**
* HTTPAPI
*
* @param method GETPOST
* 使HutoolHTTPAPI
*
* @param method GETPOST
* @param queryList
* @param body
* @param action API
* @param body
* @param action API
* @return
* @throws Exception
*/
@ -95,13 +105,13 @@ public class JmCommon {
}
// 计算请求体的SHA256哈希值
String xContentSha256 = hashSHA256(body);
// 格式化日期时间,用于请求头
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
String xDate = sdf.format(date);
String shortXDate = xDate.substring(0, 8); // 提取日期部分,用于凭证范围
String contentType = "application/json"; // 内容类型
String signHeader = "host;x-date;x-content-sha256;content-type"; // 签名头部列表
@ -124,10 +134,10 @@ public class JmCommon {
"\n" +
signHeader + "\n" +
xContentSha256;
// 计算规范请求字符串的哈希值
String hashcanonicalString = hashSHA256(canonicalStringBuilder.getBytes());
// 构建凭证范围和签名字符串
String credentialScope = shortXDate + "/" + region + "/" + service + "/request";
String signString = "HMAC-SHA256" + "\n" + xDate + "\n" + credentialScope + "\n" + hashcanonicalString;
@ -135,50 +145,70 @@ public class JmCommon {
// 生成签名密钥并计算签名
byte[] signKey = genSigningSecretKeyV4(shortXDate, region, service);
String signature = HexFormat.of().formatHex(hmacSHA256(signKey, signString));
// 构建URL并创建HTTP连接
URL url = new URL(schema + "://" + host + path + "?" + querySB);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(method);
// 构建URL
String url = schema + "://" + host + path + "?" + querySB;
// 使用Hutool发送HTTP请求
HttpRequest request = HttpRequest.of(url);
// 将字符串方法名转换为Method枚举
Method httpMethod = Method.valueOf(method.toUpperCase());
request.method(httpMethod);
// 设置请求头
conn.setRequestProperty("Host", host);
conn.setRequestProperty("X-Date", xDate);
conn.setRequestProperty("X-Content-Sha256", xContentSha256);
conn.setRequestProperty("Content-Type", contentType);
conn.setRequestProperty("Authorization", "HMAC-SHA256" +
request.header("Host", host);
request.header("X-Date", xDate);
request.header("X-Content-Sha256", xContentSha256);
request.header("Content-Type", contentType);
request.header("Authorization", "HMAC-SHA256" +
" Credential=" + ak + "/" + credentialScope +
", SignedHeaders=" + signHeader +
", Signature=" + signature);
// 如果不是GET请求写入请求体
if (!Objects.equals(conn.getRequestMethod(), "GET")) {
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
os.write(body);
os.flush();
os.close();
}
// 建立连接并获取响应
conn.connect();
int responseCode = conn.getResponseCode();
InputStream is;
if (responseCode == 200) {
is = conn.getInputStream(); // 成功响应
} else {
is = conn.getErrorStream(); // 错误响应
// 如果不是GET请求设置请求体
if (!method.equals("GET") && body.length > 0) {
request.body(body);
}
// 读取响应内容并关闭流
String responseBody = new String(ByteStreams.toByteArray(is));
is.close();
// 发送请求并获取响应
HttpResponse response = request.execute();
String responseBody = response.body();
// 关闭响应
response.close();
return responseBody;
}
/**
* URL
*
* @param fileUrl URL
* @param saveFilePath
* @throws Exception
*/
public static void downloadFile(String fileUrl, String saveFilePath) throws Exception {
try {
// 确保目录存在
File file = new File(saveFilePath);
File parentDir = file.getParentFile();
if (parentDir != null && !parentDir.exists()) {
parentDir.mkdirs();
log.info("创建目录: {}", parentDir.getAbsolutePath());
}
// 使用Hutool下载文件
long fileSize = HttpUtil.downloadFile(fileUrl, FileUtil.file(saveFilePath));
log.info("文件下载成功,保存路径: {}, 文件大小: {}字节", saveFilePath, fileSize);
} catch (Exception e) {
log.error("文件下载失败: {}", e.getMessage(), e);
throw new Exception("文件下载失败: " + e.getMessage(), e);
}
}
/**
* URL
*
*
* @param source
* @return
*/
@ -211,7 +241,7 @@ public class JmCommon {
/**
* SHA256
*
*
* @param content
* @return
* @throws Exception
@ -227,8 +257,8 @@ public class JmCommon {
/**
* 使HMAC-SHA256
*
* @param key
*
* @param key
* @param content
* @return
* @throws Exception
@ -246,9 +276,9 @@ public class JmCommon {
/**
* V4
* 使AWSV4
*
* @param date
* @param region
*
* @param date
* @param region
* @param service
* @return
* @throws Exception

@ -1,8 +1,11 @@
package com.dsideal.aiSupport.Util.JiMeng.Kit;
import lombok.Getter;
/**
* API
*/
@Getter
public enum JmErrorCode {
SUCCESS(10000, "请求成功"),
@ -18,15 +21,7 @@ public enum JmErrorCode {
this.code = code;
this.message = message;
}
public int getCode() {
return code;
}
public String getMessage() {
return message;
}
/**
*
* @param code

Loading…
Cancel
Save