工业互联网平台2.0版本后端代码
houzhongjian
2025-05-29 41499fd3c28216c1526a72b10fa98eb8ffee78cb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.iailab.framework.ai.image;
 
import cn.hutool.core.codec.Base64;
import cn.hutool.core.thread.ThreadUtil;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.ai.image.ImageOptions;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.openai.OpenAiImageOptions;
import org.springframework.ai.stabilityai.StabilityAiImageModel;
import org.springframework.ai.stabilityai.api.StabilityAiApi;
 
import javax.swing.*;
import java.awt.*;
import java.util.concurrent.TimeUnit;
 
/**
 * {@link StabilityAiImageModel} 集成测试类
 *
 * @author fansili
 */
public class StabilityAiImageModelTests {
 
    private final StabilityAiImageModel imageModel = new StabilityAiImageModel(
            new StabilityAiApi("sk-e53UqbboF8QJCscYvzJscJxJXoFcFg4iJjl1oqgE7baJETmx") // 密钥
    );
 
    @Test
    @Disabled
    public void testCall() {
        // 准备参数
        ImageOptions options = OpenAiImageOptions.builder()
                .withModel("stable-diffusion-v1-6")
                .withHeight(320).withWidth(320)
                .build();
        ImagePrompt prompt = new ImagePrompt("great wall", options);
 
        // 方法调用
        ImageResponse response = imageModel.call(prompt);
        // 打印结果
        String b64Json = response.getResult().getOutput().getB64Json();
        System.out.println(response);
        viewImage(b64Json);
    }
 
    public static void viewImage(String b64Json) {
        // 创建一个 JFrame
        JFrame frame = new JFrame("Byte Image Display");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);
 
        // 创建一个 JLabel 来显示图片
        byte[] imageBytes = Base64.decode(b64Json);
        JLabel label = new JLabel(new ImageIcon(imageBytes));
 
        // 将 JLabel 添加到 JFrame
        frame.getContentPane().add(label, BorderLayout.CENTER);
 
        // 显示 JFrame
        frame.setVisible(true);
        ThreadUtil.sleep(1, TimeUnit.HOURS);
    }
 
}