环境和配置请看<二>Sping-AI alibaba 入门-记忆聊天及持久化
源代码:https://github.com/springaialibaba/spring-ai-alibaba-examples/blob/main/spring-ai-alibaba-image-example/dashscope-image/src/main/java/com/alibaba/cloud/ai/example/image/dashscope/controller/DashScopeImageController.java
这里我只是给一个解释版本,并稍微修改了一下
git:https://gitee.com/Yee99/spring-ai-alibaba-demo
Controller
@RestController
@RequestMapping("/image")
public class ImageController {
// 注入的图像生成模型,用于调用AI生成图片的核心功能,在 org.springframework.ai.image.*包下
private final ImageModel imageModel;
// 默认提示语,用于基础图像生成场景,可不要
//private static final String DEFAULT_PROMPT = "为人工智能生成一张富有科技感的图片!";
// 构造器注入ImageModel实例
public ImageController(ImageModel imageModel) {
this.imageModel = imageModel;
}
// 通过单个提示词生成1张图片
@GetMapping("/signalImage")
public void image(HttpServletResponse response,
@RequestParam(value = "prompt") String prompt) {
// 使用提示词生成图片
ImageResponse imageResponse = imageModel.call(new ImagePrompt(prompt));
// 提取生成的图片URL
String imageUrl = imageResponse.getResult().getOutput().getUrl();
try {
// 将图片URL转换为可读流
URL url = URI.create(imageUrl).toURL();
InputStream in = url.openStream();
// 设置响应头为PNG格式
response.setHeader("Content-Type", MediaType.IMAGE_PNG_VALUE);
// 写出图片字节到HTTP响应输出流
response.getOutputStream().write(in.readAllBytes());
response.getOutputStream().flush();
} catch (IOException e) {
// IO异常处理:设置500错误状态码
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
/**
* 通过单个提示词生成多张图片
*/
// 多图生成接口:GET请求,路径为/multiPrompt
@GetMapping("/multiImage")
public ResponseEntity<Collection<String>> generateImageWithMultiPrompt(
@RequestParam(value = "prompt") String prompt,
// 图片数量参数,默认生成2张
@RequestParam(defaultValue = "2") int count) {
// 创建图片生成选项,指定生成数量
ImageOptions options = ImageOptionsBuilder.builder()
.N(count)
.build();
// 调用模型生成图片
ImageResponse response = imageModel.call(new ImagePrompt(prompt, options));
// 提取所有图片URL组成集合返回
Set<String> imageSet = response.getResults().stream().map(result -> result.getOutput().getUrl()).collect(Collectors.toSet());
return ResponseEntity.ok(imageSet);
}
/**
* 多条件安全生成图片
*/
// 多条件生成接口:GET请求,路径为/multipleConditions
@GetMapping("/multipleConditions")
public ResponseEntity<?> multipleConditions(
// 主题参数,默认值为"一只会编程的猫"
@RequestParam(value = "subject", defaultValue = "一只会编程的猫") String subject,
// 环境参数,默认值为"办公室"
@RequestParam(value = "environment", defaultValue = "办公室") String environment,
// 图片高度参数,默认1024像素
@RequestParam(value = "height", defaultValue = "1024") Integer height,
// 图片宽度参数,默认1024像素
@RequestParam(value = "width", defaultValue = "1024") Integer width,
// 风格参数,默认"生动"
@RequestParam(value = "style", defaultValue = "生动") String style) {
// 组合构建详细提示词
String prompt = String.format(
"一个%s,置身于%s的环境中,使用%s的艺术风格,高清4K画质,细节精致",
subject, environment, style
);
// 创建图片生成选项,指定尺寸
ImageOptions options = ImageOptionsBuilder.builder()
.height(height)
.width(width)
.build();
try {
// 调用模型生成图片
ImageResponse response = imageModel.call(new ImagePrompt(prompt, options));
// 返回生成的第一张图片的URL
return ResponseEntity.ok(response.getResult().getOutput().getUrl());
} catch (Exception e) {
// 异常处理:返回包含错误信息的500响应
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(Map.of(
"error", "图像生成失败",
"message", e.getMessage(),
"timestamp", LocalDateTime.now()
));
}
}
}
注意:
- 如果使用signalImage,但是让它生成两张图片的时候,仍旧生成一张图片,并且融合你描述的两个图片
- http://localhost:8080/image/signalImage?prompt=生成两张图片,一张是小猫,一张是小狗