From 0af4adbdc528a490114f75b0bb2ba6ae0ca29bc1 Mon Sep 17 00:00:00 2001 From: HuangHai <10402852@qq.com> Date: Mon, 12 May 2025 16:23:11 +0800 Subject: [PATCH] 'commit' --- .../dsideal/aiSupport/Test/JiMengImage.java | 9 ++ .../{Test/Sign.java => Util/JiMengUtil.java} | 101 +++++++----------- 2 files changed, 50 insertions(+), 60 deletions(-) create mode 100644 dsAiSupport/src/main/java/com/dsideal/aiSupport/Test/JiMengImage.java rename dsAiSupport/src/main/java/com/dsideal/aiSupport/{Test/Sign.java => Util/JiMengUtil.java} (70%) diff --git a/dsAiSupport/src/main/java/com/dsideal/aiSupport/Test/JiMengImage.java b/dsAiSupport/src/main/java/com/dsideal/aiSupport/Test/JiMengImage.java new file mode 100644 index 00000000..b7705507 --- /dev/null +++ b/dsAiSupport/src/main/java/com/dsideal/aiSupport/Test/JiMengImage.java @@ -0,0 +1,9 @@ +package com.dsideal.aiSupport.Test; +import com.dsideal.aiSupport.Util.JiMengUtil; + +public class JiMengImage { + public static void main(String[] args) throws Exception { + String prompt="制作一张vlog视频封面。马卡龙配色,美女旅游照片+色块的拼贴画风格,主文案是“威海旅游vlog”,副文案是“特种兵一日游 被低估的旅游城市”,海报主体是一个穿着短裙、梳双马尾的少女,人物白色描边"; + JiMengUtil.generateImage(prompt); + } +} diff --git a/dsAiSupport/src/main/java/com/dsideal/aiSupport/Test/Sign.java b/dsAiSupport/src/main/java/com/dsideal/aiSupport/Util/JiMengUtil.java similarity index 70% rename from dsAiSupport/src/main/java/com/dsideal/aiSupport/Test/Sign.java rename to dsAiSupport/src/main/java/com/dsideal/aiSupport/Util/JiMengUtil.java index b2f3e392..f93a9193 100644 --- a/dsAiSupport/src/main/java/com/dsideal/aiSupport/Test/Sign.java +++ b/dsAiSupport/src/main/java/com/dsideal/aiSupport/Util/JiMengUtil.java @@ -1,4 +1,4 @@ -package com.dsideal.aiSupport.Test; +package com.dsideal.aiSupport.Util; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; @@ -21,12 +21,31 @@ import java.security.MessageDigest; import java.text.SimpleDateFormat; import java.util.*; -public class Sign { +public class JiMengUtil { + + // 请求域名 + public static String host = "visual.volcengineapi.com"; + public static String path = "/"; // 路径,不包含 Query// 请求接口信息 + public static String service = "cv"; + public static String region = "cn-north-1"; + public static String schema = "https"; + public static String action = "CVProcess"; + public static String version = "2022-08-31"; + private static final BitSet URLENCODER = new BitSet(256); + private static final String CONST_ENCODE = "0123456789ABCDEF"; + public static final Charset UTF_8 = StandardCharsets.UTF_8; + private static final String ak; + private static final String sk; + public static Prop PropKit; + static { //加载配置文件 String configFile = "application_{?}.yaml".replace("{?}", getEnvPrefix()); PropKit = new YamlProp(configFile); System.out.println("当前环境: " + getEnvPrefix()); + // 火山官网密钥信息, 注意sk结尾有== + ak = PropKit.get("JiMeng.ak"); + sk = PropKit.get("JiMeng.sk"); } /** @@ -42,19 +61,6 @@ public class Sign { return myEnvVar; } - public static Prop PropKit; - - - private static final BitSet URLENCODER = new BitSet(256); - private static final String CONST_ENCODE = "0123456789ABCDEF"; - public static final Charset UTF_8 = StandardCharsets.UTF_8; - private final String region; - private final String service; - private final String schema; - private final String host; - private final String path; - private final String ak; - private final String sk; static { int i; @@ -73,18 +79,10 @@ public class Sign { URLENCODER.set('~'); } - public Sign(String region, String service, String schema, String host, String path, String ak, String sk) { - this.region = region; - this.service = service; - this.host = host; - this.schema = schema; - this.path = path; - this.ak = ak; - this.sk = sk; - } - public void doRequest(String method, Map queryList, byte[] body, - Date date, String action, String version) throws Exception { + private static void doRequest(String method, Map queryList, byte[] body, String saveImgPath) throws Exception { + + Date date = new Date(); if (body == null) { body = new byte[0]; } @@ -117,7 +115,7 @@ public class Sign { String credentialScope = shortXDate + "/" + region + "/" + service + "/request"; String signString = "HMAC-SHA256" + "\n" + xDate + "\n" + credentialScope + "\n" + hashcanonicalString; - byte[] signKey = genSigningSecretKeyV4(sk, shortXDate, region, service); + byte[] signKey = genSigningSecretKeyV4(shortXDate, region, service); String signature = HexFormat.of().formatHex(hmacSHA256(signKey, signString)); URL url = new URL(schema + "://" + host + path + "?" + querySB); @@ -151,23 +149,15 @@ public class Sign { String responseBody = new String(ByteStreams.toByteArray(is)); is.close(); - System.out.println(responseCode); - System.out.println(responseBody); JSONObject jo = JSON.parseObject(responseBody); String imgBase64 = jo.getJSONObject("data").getJSONArray("binary_data_base64").getFirst().toString(); - System.out.println(imgBase64); - - - - String filePath = "D:/temp/result.jpg"; // 目标文件路径,可以自行设置合适的路径和文件名 - // 对 Base64 字符串进行解码 Base64.Decoder decoder = Base64.getDecoder(); byte[] bytes = decoder.decode(imgBase64); - try (OutputStream os = new FileOutputStream(filePath)) { // 使用-with try-resources 自动关闭资源 + try (OutputStream os = new FileOutputStream(saveImgPath)) { // 使用-with try-resources 自动关闭资源 os.write(bytes); os.flush(); // 刷新缓冲区,确保数据写入文件 System.out.println("文件保存成功!"); @@ -176,7 +166,7 @@ public class Sign { } } - private String signStringEncoder(String source) { + private static String signStringEncoder(String source) { if (source == null) { return null; } @@ -200,7 +190,7 @@ public class Sign { return buf.toString(); } - public static String hashSHA256(byte[] content) throws Exception { + private static String hashSHA256(byte[] content) throws Exception { try { MessageDigest md = MessageDigest.getInstance("SHA-256"); return Hex.encodeHexString(md.digest(content)); @@ -209,7 +199,7 @@ public class Sign { } } - public static byte[] hmacSHA256(byte[] key, String content) throws Exception { + private static byte[] hmacSHA256(byte[] key, String content) throws Exception { try { Mac mac = Mac.getInstance("HmacSHA256"); mac.init(new SecretKeySpec(key, "HmacSHA256")); @@ -219,33 +209,24 @@ public class Sign { } } - private byte[] genSigningSecretKeyV4(String secretKey, String date, String region, String service) throws Exception { - byte[] kDate = hmacSHA256((secretKey).getBytes(), date); + private static byte[] genSigningSecretKeyV4(String date, String region, String service) throws Exception { + byte[] kDate = hmacSHA256((JiMengUtil.sk).getBytes(), date); byte[] kRegion = hmacSHA256(kDate, region); byte[] kService = hmacSHA256(kRegion, service); return hmacSHA256(kService, "request"); } - public static void main(String[] args) throws Exception { - // 火山官网密钥信息, 注意sk结尾有== - String AccessKeyID = PropKit.get("JiMeng.ak"); - String SecretAccessKey = PropKit.get("JiMeng.sk"); - - // 请求域名 - String endpoint = "visual.volcengineapi.com"; - String path = "/"; // 路径,不包含 Query// 请求接口信息 - String service = "cv"; - String region = "cn-north-1"; - String schema = "https"; - Sign sign = new Sign(region, service, schema, endpoint, path, AccessKeyID, SecretAccessKey); - // 参考接口文档Query参数 - String action = "CVProcess"; - String version = "2022-08-31"; - Date date = new Date(); - // 参考接口文档Body参数 + /** + * 生成图片 + * + * @param prompt 提示词 + * @throws Exception + */ + public static void generateImage(String prompt) throws Exception { + String saveImgPath = "d:/Temp/1.jpg"; JSONObject req = new JSONObject(); req.put("req_key", "jimeng_high_aes_general_v21_L"); - req.put("prompt", "制作一张vlog视频封面。马卡龙配色,美女旅游照片+色块的拼贴画风格,主文案是“威海旅游vlog”,副文案是“特种兵一日游 被低估的旅游城市”,海报主体是一个穿着短裙、梳双马尾的少女,人物白色描边"); - sign.doRequest("POST", new HashMap<>(), req.toString().getBytes(), date, action, version); + req.put("prompt", prompt); + doRequest("POST", new HashMap<>(), req.toString().getBytes(), saveImgPath); } }