@@ -32,17 +32,11 @@ import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.databind.ObjectMapper ;
import com.fasterxml.jackson.databind.JsonNode ;
import com.rj.service.MinIOService ;
import java.io.* ;
import java.net.URL ;
/**
* 图像模型控制器
*
* 提供图像模型相关的RESTful API接口, 包括:
* - 图像模型记录的创建、查询、更新、删除操作
* - 分页查询和条件查询功能
* - 根据各种条件查询图像模型记录
* - 统一的响应格式和异常处理
* 图像模型控制
*
* https://help.aliyun.com/zh/model-studio/text-to-image#20e9dbb913b9s
*
* 使用Swagger注解进行API文档生成, 支持在线API测试。
* 所有接口都包含完整的参数验证、异常处理和日志记录。
@@ -678,9 +672,9 @@ public class ImageModelController {
}
}
@PostMapping ( " /image-gen-byText " )
@PostMapping ( " /text-to-image " )
@Operation ( summary = " 文本生成图像 " , description = " 基于文本提示词生成图像 " )
public ResponseEntity < Map < String , Object > > i mageGen (
public ResponseEntity < Map < String , Object > > textToI mage(
@Parameter ( description = " 文本生成图像 " ) @Valid @RequestBody TextModelController . ImageProcessingRequest request ) {
log . info ( " 开始文本生成图像, request: {} " , request ) ;
@@ -906,36 +900,7 @@ public class ImageModelController {
}
}
/**
* 从URL下载图像
*
* @param imageUrl 图像URL
* @return 图像字节数组
* @throws IOException 下载失败时抛出异常
*/
private byte [ ] downloadImageFromUrl11 ( String imageUrl ) throws IOException {
try {
log . info ( " 开始下载图像: {} " , imageUrl ) ;
URL url = new URL ( imageUrl ) ;
try ( InputStream inputStream = url . openStream ( ) ;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream ( ) ) {
byte [ ] buffer = new byte [ 4096 ] ;
int bytesRead ;
while ( ( bytesRead = inputStream . read ( buffer ) ) ! = - 1 ) {
outputStream . write ( buffer , 0 , bytesRead ) ;
}
byte [ ] imageBytes = outputStream . toByteArray ( ) ;
log . info ( " 图像下载完成,大小: {} bytes " , imageBytes . length ) ;
return imageBytes ;
}
} catch ( Exception e ) {
log . error ( " 下载图像失败: {} " , imageUrl , e ) ;
throw new IOException ( " 下载图像失败: " + e . getMessage ( ) , e ) ;
}
}
/**
@@ -1281,5 +1246,54 @@ public class ImageModelController {
private String generateRequestId ( ) {
return " REQ_ " + System . currentTimeMillis ( ) + " _ " + ( int ) ( Math . random ( ) * 1000 ) ;
}
/**
* 调用阿里云 根据图片和指令,生成新图
*
* @return ResponseEntity 包含生成结果的响应实体
*/
@PostMapping ( " /image-to-image " )
@Operation ( summary = " 图生图 " , description = " 根据图片和指令,生成新图片 " )
public ResponseEntity < Map < String , Object > > generateimageToImage (
@Parameter ( description = " 文件 " , required = true )
@RequestParam ( " multipartFile " ) MultipartFile multipartFile ,
@Parameter ( description = " 参考提示词 " , required = true )
@RequestParam ( " refPrompt " ) String refPrompt ,
@Parameter ( description = " 提示词 " , required = true )
@RequestParam ( " prompt " ) String prompt ,
@Parameter ( description = " 所属人姓名 " , required = true )
@RequestParam ( " ownerName " ) String ownerName ,
@Parameter ( description = " 所属人电话 " , required = true )
@RequestParam ( " ownerPhone " ) String ownerPhone ,
@Parameter ( description = " 图片名称 " , required = true )
@RequestParam ( " imageName " ) String imageName ,
@Parameter ( description = " 模型名称 " , required = false )
@RequestParam ( value = " model " ) String model ,
@Parameter ( description = " 参数 " , required = false )
@RequestParam ( value = " parameters " , required = false ) String parameters ) {
try {
log . info ( " 开始图生图处理, ownerName: {}, imageName: {}, prompt: {} " ,
ownerName , imageName , prompt ) ;
// 调用业务层方法处理图生图逻辑
Map < String , Object > result = imageModelService . generateImageToImage (
multipartFile , prompt , ownerName , ownerPhone , imageName , model , parameters ) ;
return ResponseEntity . ok ( result ) ;
} catch ( Exception e ) {
log . error ( " 图生图API失败: {} " , e . getMessage ( ) , e ) ;
Map < String , Object > errorResult = new HashMap < > ( ) ;
errorResult . put ( " success " , false ) ;
errorResult . put ( " message " , " 图生图生成失败: " + e . getMessage ( ) ) ;
errorResult . put ( " error " , e . getClass ( ) . getSimpleName ( ) ) ;
errorResult . put ( " timestamp " , LocalDateTime . now ( ) ) ;
errorResult . put ( " requestId " , generateRequestId ( ) ) ;
return ResponseEntity . status ( HttpStatus . INTERNAL_SERVER_ERROR ) . body ( errorResult ) ;
}
}
}