Browse Source

feature-img-1.0:新增AI绘图相关内容

feature-1.0-img
Penny 3 years ago
parent
commit
58859d13d1
  1. 26
      bnyer-api/bnyer-api-file/pom.xml
  2. 40
      bnyer-api/bnyer-api-file/src/main/java/com/bnyer/file/api/RemoteFileService.java
  3. 40
      bnyer-api/bnyer-api-file/src/main/java/com/bnyer/file/api/factory/RemoteFileFallbackFactory.java
  4. 2
      bnyer-api/bnyer-api-file/src/main/resources/META-INF/spring.factories
  5. 6
      bnyer-api/bnyer-api-system/src/main/java/com/bnyer/system/api/RemoteSystemFileService.java
  6. 10
      bnyer-api/bnyer-api-system/src/main/java/com/bnyer/system/api/factory/RemoteFileFallbackFactory.java
  7. 1
      bnyer-api/pom.xml
  8. 9
      bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/constant/ServiceNameConstants.java
  9. 122
      bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/domain/AiPaint.java
  10. 23
      bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/dto/AiPaintPageDto.java
  11. 16
      bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/dto/TextToImgDto.java
  12. 84
      bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/utils/file/Base64ToMultipartFileUtils.java
  13. 3
      bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/utils/file/ImageUtils.java
  14. 9
      bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/vo/TextToImgVo.java
  15. 2
      bnyer-services/bnyer-file/src/main/java/com/bnyer/file/controller/SysFileController.java
  16. 3
      bnyer-services/bnyer-file/src/main/java/com/bnyer/file/service/impl/MinioSysFileServiceImpl.java
  17. 7
      bnyer-services/bnyer-img/pom.xml
  18. 11
      bnyer-services/bnyer-img/src/main/java/com/bnyer/img/controller/TiktokMiniController.java
  19. 29
      bnyer-services/bnyer-img/src/main/java/com/bnyer/img/mapper/AiPaintMapper.java
  20. 40
      bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/AiPaintService.java
  21. 44
      bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/impl/AiPaintServiceImpl.java
  22. 55
      bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/impl/StableDiffusionServiceImpl.java
  23. 50
      bnyer-services/bnyer-img/src/main/java/com/bnyer/img/vo/AiPaintVo.java
  24. 40
      bnyer-services/bnyer-img/src/main/resources/com/bnyer/img/mapper/AiPaintMapper.xml
  25. 5
      bnyer-services/bnyer-system/src/main/java/com/bnyer/system/controller/SysProfileController.java

26
bnyer-api/bnyer-api-file/pom.xml

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>bnyer-api</artifactId>
<groupId>com.dimensionalnode</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>bnyer-api-file</artifactId>
<description>
bnyer-api-file文件服务接口模块
</description>
<dependencies>
<!-- bnyer Common Core-->
<dependency>
<groupId>com.dimensionalnode</groupId>
<artifactId>bnyer-common-core</artifactId>
</dependency>
</dependencies>
</project>

40
bnyer-api/bnyer-api-file/src/main/java/com/bnyer/file/api/RemoteFileService.java

@ -0,0 +1,40 @@
package com.bnyer.file.api;
import com.bnyer.common.core.constant.ServiceNameConstants;
import com.bnyer.common.core.domain.R;
import com.bnyer.file.api.factory.RemoteFileFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
/**
* 文件服务
*
* @author penny
* @date 2023/04/15 11:03
*/
@FeignClient(contextId = "remoteFileService", value = ServiceNameConstants.FILE_SERVICE, fallbackFactory = RemoteFileFallbackFactory.class)
public interface RemoteFileService {
/**
* 上传文件到minio
* @param file 文件
* @return -
*/
@PostMapping(value = "/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
R<String> uploadBanner(@RequestPart(name = "file") MultipartFile file);
/**
* 批量上传文件到七牛云
* @param files 文件
* @return -
*/
@PostMapping("/uploadBatch")
R<List<String>> uploadBatch(MultipartFile[] files);
}

40
bnyer-api/bnyer-api-file/src/main/java/com/bnyer/file/api/factory/RemoteFileFallbackFactory.java

@ -0,0 +1,40 @@
package com.bnyer.file.api.factory;
import com.bnyer.common.core.domain.R;
import com.bnyer.file.api.RemoteFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
/**
* 文件服务降级处理
*
* @author penny
*/
@Component
public class RemoteFileFallbackFactory implements FallbackFactory<RemoteFileService>
{
private static final Logger log = LoggerFactory.getLogger(RemoteFileFallbackFactory.class);
@Override
public RemoteFileService create(Throwable throwable) {
log.error("api文件服务调用失败:{}", throwable.getMessage());
return new RemoteFileService()
{
@Override
public R<String> uploadBanner(MultipartFile file) {
return R.fail("远程调用minio文件上传失败:" + throwable.getMessage());
}
@Override
public R<List<String>> uploadBatch(MultipartFile[] files) {
return R.fail("远程调用七牛云批量文件上传失败:" + throwable.getMessage());
}
};
}
}

2
bnyer-api/bnyer-api-file/src/main/resources/META-INF/spring.factories

@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration = com.bnyer.file.api.factory.RemoteFileFallbackFactory

6
bnyer-api/bnyer-api-system/src/main/java/com/bnyer/system/api/RemoteFileService.java → bnyer-api/bnyer-api-system/src/main/java/com/bnyer/system/api/RemoteSystemFileService.java

@ -12,11 +12,11 @@ import com.bnyer.system.api.domain.SysFile;
/**
* 文件服务
*
*
* @author ruoyi
*/
@FeignClient(contextId = "remoteFileService", value = ServiceNameConstants.FILE_SERVICE, fallbackFactory = RemoteFileFallbackFactory.class)
public interface RemoteFileService
@FeignClient(contextId = "remoteSystemFileService", value = ServiceNameConstants.SYSTEM_FILE_SERVICE, fallbackFactory = RemoteFileFallbackFactory.class)
public interface RemoteSystemFileService
{
/**
* 上传文件

10
bnyer-api/bnyer-api-system/src/main/java/com/bnyer/system/api/factory/RemoteFileFallbackFactory.java

@ -6,24 +6,24 @@ import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import com.bnyer.common.core.domain.R;
import com.bnyer.system.api.RemoteFileService;
import com.bnyer.system.api.RemoteSystemFileService;
import com.bnyer.system.api.domain.SysFile;
/**
* 文件服务降级处理
*
*
* @author ruoyi
*/
@Component
public class RemoteFileFallbackFactory implements FallbackFactory<RemoteFileService>
public class RemoteFileFallbackFactory implements FallbackFactory<RemoteSystemFileService>
{
private static final Logger log = LoggerFactory.getLogger(RemoteFileFallbackFactory.class);
@Override
public RemoteFileService create(Throwable throwable)
public RemoteSystemFileService create(Throwable throwable)
{
log.error("文件服务调用失败:{}", throwable.getMessage());
return new RemoteFileService()
return new RemoteSystemFileService()
{
@Override
public R<SysFile> upload(MultipartFile file)

1
bnyer-api/pom.xml

@ -11,6 +11,7 @@
<modules>
<module>bnyer-api-system</module>
<module>bnyer-api-img</module>
<module>bnyer-api-file</module>
</modules>
<artifactId>bnyer-api</artifactId>

9
bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/constant/ServiceNameConstants.java

@ -18,12 +18,17 @@ public class ServiceNameConstants
public static final String SYSTEM_SERVICE = "bnyer-system";
/**
* 文件服务的serviceid
* 系统文件服务的serviceid
*/
public static final String FILE_SERVICE = "bnyer-file";
public static final String SYSTEM_FILE_SERVICE = "bnyer-system-file";
/**
* 图文服务的serviceid
*/
public static final String IMG_SERVICE = "bnyer-img";
/**
* 文件服务的serviceid
*/
public static final String FILE_SERVICE = "bnyer-file";
}

122
bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/domain/AiPaint.java

@ -0,0 +1,122 @@
package com.bnyer.common.core.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import java.util.Date;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
/**
* ai绘画内容表
*/
@ApiModel(value="com-bnyer-common-core-domain-AiPaint")
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "img_ai_paint")
public class AiPaint implements Serializable {
/**
* 主键Id
*/
@TableId(value = "id", type = IdType.AUTO)
@ApiModelProperty(value="主键Id")
private Long id;
/**
* 作品编号
*/
@TableField(value = "paint_id")
@ApiModelProperty(value="作品编号")
private String paintId;
/**
* 绘图者id
*/
@TableField(value = "painter_id")
@ApiModelProperty(value="绘图者id")
private Long painterId;
/**
* 绘图者昵称
*/
@TableField(value = "painter_name")
@ApiModelProperty(value="绘图者昵称")
private String painterName;
/**
* 图片
*/
@TableField(value = "img_url")
@ApiModelProperty(value="图片")
private String imgUrl;
/**
* 关键词
*/
@TableField(value = "prompt")
@ApiModelProperty(value="关键词")
private String prompt;
/**
* 模型名称
*/
@TableField(value = "model")
@ApiModelProperty(value="模型风格名称")
private String model;
/**
* 风格名称
*/
@TableField(value = "style_name")
@ApiModelProperty(value="风格名称")
private String styleName;
/**
* 图片高度
*/
@TableField(value = "height")
@ApiModelProperty(value="图片高度")
private String height;
/**
* 图片宽度
*/
@TableField(value = "width")
@ApiModelProperty(value="图片宽度")
private String width;
/**
* 是否显示 (0->隐藏1->显示)
*/
@TableField(value = "is_show")
@ApiModelProperty(value="是否显示 (0->隐藏;1->显示)")
private String isShow;
/**
* 平台(0->Hub;1->抖音;2->快手;3->微信)
*/
@TableField(value = "`source`")
@ApiModelProperty(value="平台(0->Hub;1->抖音;2->快手;3->微信)")
private String source;
/**
* 创建时间
*/
@TableField(value = "create_time")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@ApiModelProperty(value="创建时间")
private Date createTime;
private static final long serialVersionUID = 1L;
}

23
bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/dto/AiPaintPageDto.java

@ -0,0 +1,23 @@
package com.bnyer.common.core.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
@Getter
@Setter
@ApiModel("ai绘画前端分页接收类")
public class AiPaintPageDto extends BasePageDto {
@NotEmpty(message = "平台渠道不能为空!")
@ApiModelProperty(value="平台渠道")
private String source;
@NotNull(message = "绘图者id不能为空!")
@ApiModelProperty(value="绘图者id")
private Long painterId;
}

16
bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/dto/TextToImgDto.java

@ -5,8 +5,6 @@ import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
@ -27,6 +25,16 @@ public class TextToImgDto implements Serializable {
@ApiModelProperty(value="提示词")
private String prompt;
@ApiModelProperty(value="风格")
private String samplerIndex;
@ApiModelProperty(value="模型")
private String modelName;
@ApiModelProperty(value="风格名称")
private String styleName;
@ApiModelProperty(value="绘图者id")
private Long painterId;
@ApiModelProperty(value="绘图者昵称")
private String painterName;
}

84
bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/utils/file/Base64ToMultipartFileUtils.java

@ -0,0 +1,84 @@
package com.bnyer.common.core.utils.file;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
/**
* @author
* @date 2023/04/16 16:40
* @description 转换base64为文件流
*/
public class Base64ToMultipartFileUtils implements MultipartFile {
private final byte[] fileContent;
private final String name;
private final String extension;
private final String contentType;
private final String originalFilename;
/**
* @param base64
* @param dataUri 格式类似于: data:image/png;base64
*/
public Base64ToMultipartFileUtils(String base64, String dataUri, String name, String originalfilename) {
this.fileContent = Base64.getDecoder().decode(base64.getBytes(StandardCharsets.UTF_8));
this.extension = dataUri.split(";")[0].split("/")[1];
this.contentType = dataUri.split(";")[0].split(":")[1];
this.originalFilename = originalfilename;
this.name = name;
}
/**
* 重要必须与请求接收方参数名称一致否则找不到参数
* @return
*/
@Override
public String getName() {
return this.name;
}
@Override
public String getOriginalFilename() {
return originalFilename;
}
@Override
public String getContentType() {
return contentType;
}
@Override
public boolean isEmpty() {
return fileContent == null || fileContent.length == 0;
}
@Override
public long getSize() {
return fileContent.length;
}
@Override
public byte[] getBytes() throws IOException {
return fileContent;
}
@Override
public ByteArrayInputStream getInputStream() throws IOException {
return new ByteArrayInputStream(fileContent);
}
@Override
public void transferTo(File file) throws IOException, IllegalStateException {
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write(fileContent);
}
}
}

3
bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/utils/file/ImageUtils.java

@ -1,6 +1,7 @@
package com.bnyer.common.core.utils.file;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
@ -8,6 +9,8 @@ import java.util.Arrays;
import org.apache.poi.util.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
/**
* 图片处理工具类

9
bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/vo/TextToImgVo.java

@ -1,11 +1,13 @@
package com.bnyer.common.core.vo;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
@ -17,5 +19,12 @@ public class TextToImgVo implements Serializable {
@ApiModelProperty(value="图片base64集合")
private List<String> images;
@ApiModelProperty(value="创作时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date paintTime;
@ApiModelProperty(value="作品id")
private String paintId;
private static final long serialVersionUID = 1L;
}

2
bnyer-services/bnyer-file/src/main/java/com/bnyer/file/controller/SysFileController.java

@ -107,7 +107,7 @@ public class SysFileController {
@ApiOperation(value="批量上传到minio")
@PostMapping("/upload")
@ResponseBody
public R uploadBanner(@RequestParam("file") MultipartFile file) {
public R<String> uploadBanner(@RequestParam("file") MultipartFile file) {
String url = null;
try {
url = minioService.uploadBanner(file);

3
bnyer-services/bnyer-file/src/main/java/com/bnyer/file/service/MinioSysFileServiceImpl.java → bnyer-services/bnyer-file/src/main/java/com/bnyer/file/service/impl/MinioSysFileServiceImpl.java

@ -1,7 +1,8 @@
package com.bnyer.file.service;
package com.bnyer.file.service.impl;
import com.bnyer.common.core.exception.ServiceException;
import com.bnyer.file.config.MinioConfig;
import com.bnyer.file.service.MinioService;
import com.bnyer.file.utils.FileUploadUtils;
import com.bnyer.file.utils.ImgUtil;
import io.minio.MinioClient;

7
bnyer-services/bnyer-img/pom.xml

@ -83,6 +83,13 @@
<artifactId>bnyer-common-swagger</artifactId>
</dependency>
<!-- bnyer Api File -->
<dependency>
<groupId>com.dimensionalnode</groupId>
<artifactId>bnyer-api-file</artifactId>
<version>1.0.0</version>
</dependency>
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>

11
bnyer-services/bnyer-img/src/main/java/com/bnyer/img/controller/TiktokMiniController.java

@ -89,6 +89,9 @@ public class TiktokMiniController extends BaseController {
@Autowired
private StableDiffusionService stableDiffusionService;
@Autowired
private AiPaintService aiPaintService;
@ApiOperation(value="查询banner列表")
@GetMapping(value = "/listBanner")
public AjaxResult listBanner(){
@ -423,4 +426,12 @@ public class TiktokMiniController extends BaseController {
public AjaxResult textToImg(@Validated @RequestBody @ApiParam("文生图对象") TextToImgDto param){
return AjaxResult.success(stableDiffusionService.textToImg(param));
}
@ApiOperation(value="获取绘画者ai绘画分页")
@PostMapping(value = "/getAiPaintPage")
public TableDataInfo getAiPaintPage(@Validated @RequestBody @ApiParam("ai绘画对象") AiPaintPageDto dto){
PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
List<AiPaintVo> aiPaintList = aiPaintService.queryPage(dto);
return getDataTable(aiPaintList);
}
}

29
bnyer-services/bnyer-img/src/main/java/com/bnyer/img/mapper/AiPaintMapper.java

@ -0,0 +1,29 @@
package com.bnyer.img.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.bnyer.common.core.domain.AiPaint;
import com.bnyer.common.core.dto.AiPaintPageDto;
import com.bnyer.img.vo.AiPaintVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface AiPaintMapper extends BaseMapper<AiPaint> {
/**
* 获取ai绘画分页
* @param source 来源
* @param painterId 绘画者id
* @return -
*/
List<AiPaintVo> queryPage(@Param("source") String source,@Param("painterId") Long painterId);
/**
* 获取ai绘画详情
* @param id 主键id
* @return -
*/
AiPaintVo queryDetails(Long id);
}

40
bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/AiPaintService.java

@ -0,0 +1,40 @@
package com.bnyer.img.service;
import com.bnyer.common.core.domain.AiPaint;
import com.bnyer.common.core.dto.AiPaintPageDto;
import com.bnyer.img.vo.AiPaintVo;
import java.util.List;
public interface AiPaintService {
/**
* 新增ai绘画
* @param aiPaint ai绘画
* @return -
*/
int insert(AiPaint aiPaint);
/**
* 批量删除ai绘画
* @param ids id数组
* @return -
*/
int delete(List<Long> ids);
/**
* 获取绘画者ai绘画分页
* @param params 分页参数
* @return -
*/
List<AiPaintVo> queryPage(AiPaintPageDto params);
/**
* 获取ai绘画详情
* @param id 主键id
* @return -
*/
AiPaintVo queryDetails(Long id);
}

44
bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/impl/AiPaintServiceImpl.java

@ -0,0 +1,44 @@
package com.bnyer.img.service.impl;
import cn.hutool.core.util.IdUtil;
import com.bnyer.common.core.domain.AiPaint;
import com.bnyer.common.core.dto.AiPaintPageDto;
import com.bnyer.common.core.utils.uuid.IdUtils;
import com.bnyer.img.mapper.AiPaintMapper;
import com.bnyer.img.service.AiPaintService;
import com.bnyer.img.vo.AiPaintVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
@Service
public class AiPaintServiceImpl implements AiPaintService {
@Autowired
private AiPaintMapper aiPaintMapper;
@Override
@Transactional(rollbackFor = Exception.class)
public int insert(AiPaint aiPaint) {
return aiPaintMapper.insert(aiPaint);
}
@Override
@Transactional(rollbackFor = Exception.class)
public int delete(List<Long> ids) {
return aiPaintMapper.deleteBatchIds(ids);
}
@Override
public List<AiPaintVo> queryPage(AiPaintPageDto params) {
return aiPaintMapper.queryPage(params.getSource(),params.getPainterId());
}
@Override
public AiPaintVo queryDetails(Long id) {
return aiPaintMapper.queryDetails(id);
}
}

55
bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/impl/StableDiffusionServiceImpl.java

@ -1,15 +1,25 @@
package com.bnyer.img.service.impl;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bnyer.common.core.domain.AiPaint;
import com.bnyer.common.core.domain.R;
import com.bnyer.common.core.dto.TextToImgDto;
import com.bnyer.common.core.utils.file.Base64ToMultipartFileUtils;
import com.bnyer.common.core.utils.file.ImageUtils;
import com.bnyer.common.core.vo.TextToImgVo;
import com.bnyer.file.api.RemoteFileService;
import com.bnyer.img.service.AiPaintService;
import com.bnyer.img.service.StableDiffusionService;
import com.bnyer.system.api.RemoteSystemFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -21,11 +31,18 @@ public class StableDiffusionServiceImpl implements StableDiffusionService {
@Autowired
private RestTemplate restTemplate;
@Autowired
private AiPaintService aiPaintService;
@Autowired
private RemoteFileService remoteFileService;
@Override
public TextToImgVo textToImg(TextToImgDto param) {
//TODO 根据选择的风格来选择模型
Map<String, Object> map = new HashMap<>();
map.put("width",param.getWidth());
map.put("height",param.getHeight());
map.put("width",param.getWidth() == null ? 512 : param.getWidth());
map.put("height",param.getHeight() == null ? 512 : param.getHeight());
map.put("prompt", param.getPrompt());
map.put("seed",-1);
map.put("batch_size",1);
@ -33,15 +50,45 @@ public class StableDiffusionServiceImpl implements StableDiffusionService {
map.put("restore_faces",false);
map.put("tiling",false);
map.put("eta",0);
map.put("sampler_index","Euler");
map.put("sampler_index","DPM++ 2S a Karras");
//map.put("sampler_index",param.getSamplerIndex());
map.put("steps",20);
map.put("negative_prompt","nsfw");
log.info("请求stable_diffusion请求体为:【{}】", JSON.toJSONString(map));
JSONObject jsonObject = restTemplate.postForObject("http://localhost:7860/sdapi/v1/txt2img", map, JSONObject.class);
log.info("请求stable_diffusion响应体的为:【{}】", JSON.toJSONString(jsonObject));
TextToImgVo img = new TextToImgVo();
if(jsonObject != null && jsonObject.getJSONArray("images").size() > 0){
img.setImages(jsonObject.getJSONArray("images").toJavaList(String.class));
List<String> images = jsonObject.getJSONArray("images").toJavaList(String.class);
img.setImages(images);
String paintId = null;
Date paintTime = null;
for (String image : images) {
//base64转file
MultipartFile file = new Base64ToMultipartFileUtils(image, "data:image/png;base64", "file", "tempSDImg");
//上传图片到七牛云/minio
String imgStr = remoteFileService.uploadBanner(file).getData();
//保存生辰该图片到ai绘画表
AiPaint paint = new AiPaint();
//paint.setId(); 主键改成雪花算法后启用
paintId = IdUtil.getSnowflakeNextIdStr();
paintTime = new Date();
paint.setPaintId(paintId);
paint.setCreateTime(paintTime);
paint.setImgUrl(imgStr);
paint.setPrompt(param.getPrompt());
paint.setModel(param.getModelName());
paint.setStyleName(param.getStyleName());
paint.setHeight(param.getHeight() == null ? "512" : String.valueOf(param.getHeight()));
paint.setWidth(param.getWidth() == null ? "512" : String.valueOf(param.getWidth()));
paint.setIsShow("1");
paint.setSource("1");
paint.setPainterId(param.getPainterId());
paint.setPainterName(param.getPainterName());
aiPaintService.insert(paint);
}
img.setPaintId(paintId);
img.setPaintTime(paintTime);
}
return img;

50
bnyer-services/bnyer-img/src/main/java/com/bnyer/img/vo/AiPaintVo.java

@ -0,0 +1,50 @@
package com.bnyer.img.vo;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
import java.util.Date;
@Getter
@Setter
@ApiModel("ai绘画响应体")
public class AiPaintVo implements Serializable {
@ApiModelProperty(value="主键Id")
private Long id;
@ApiModelProperty(value="作品编号")
private String paintId;
@ApiModelProperty(value="绘图者昵称")
private String painterName;
@ApiModelProperty(value="图片")
private String imgUrl;
@ApiModelProperty(value="关键词")
private String prompt;
@ApiModelProperty(value="风格名称")
private String styleName;
@ApiModelProperty(value="图片高度")
private String height;
@ApiModelProperty(value="图片宽度")
private String width;
@ApiModelProperty(value="平台(0->Hub;1->抖音;2->快手;3->微信)")
private String source;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@ApiModelProperty(value="创建时间")
private Date createTime;
private static final long serialVersionUID = 1L;
}

40
bnyer-services/bnyer-img/src/main/resources/com/bnyer/img/mapper/AiPaintMapper.xml

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bnyer.img.mapper.AiPaintMapper">
<resultMap id="BaseResultMap" type="com.bnyer.common.core.domain.AiPaint">
<!--@mbg.generated-->
<!--@Table img_ai_paint-->
<id column="id" jdbcType="BIGINT" property="id" />
<result column="paint_id" jdbcType="VARCHAR" property="paintId" />
<result column="painter_id" jdbcType="BIGINT" property="painterId" />
<result column="painter_name" jdbcType="VARCHAR" property="painterName" />
<result column="img_url" jdbcType="VARCHAR" property="imgUrl" />
<result column="prompt" jdbcType="LONGVARCHAR" property="prompt" />
<result column="model" jdbcType="VARCHAR" property="model" />
<result column="style_name" jdbcType="VARCHAR" property="styleName" />
<result column="height" jdbcType="VARCHAR" property="height" />
<result column="width" jdbcType="VARCHAR" property="width" />
<result column="is_show" jdbcType="CHAR" property="isShow" />
<result column="source" jdbcType="CHAR" property="source" />
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
</resultMap>
<sql id="Base_Column_List">
<!--@mbg.generated-->
id, paint_id, painter_id, painter_name, img_url, prompt, model,style_name, height, width, is_show,
`source`, create_time
</sql>
<select id="queryPage" resultType="com.bnyer.img.vo.AiPaintVo">
select
id, paint_id,painter_name,img_url, prompt, style_name,height, width,create_time,source
from img_ai_paint
where is_show = '1' and source = #{source} and painter_id = #{painterId}
order by create_time desc
</select>
<select id="queryDetails" resultType="com.bnyer.img.vo.AiPaintVo">
select
id, paint_id,painter_name,img_url, prompt, style_name,height, width,create_time,source
from img_ai_paint where id = #{id}
</select>
</mapper>

5
bnyer-services/bnyer-system/src/main/java/com/bnyer/system/controller/SysProfileController.java

@ -1,6 +1,8 @@
package com.bnyer.system.controller;
import java.io.IOException;
import com.bnyer.system.api.RemoteSystemFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@ -19,7 +21,6 @@ import com.bnyer.common.log.annotation.Log;
import com.bnyer.common.log.enums.BusinessType;
import com.bnyer.common.security.service.TokenService;
import com.bnyer.common.security.utils.SecurityUtils;
import com.bnyer.system.api.RemoteFileService;
import com.bnyer.system.api.domain.SysFile;
import com.bnyer.system.api.domain.SysUser;
import com.bnyer.system.api.model.LoginUser;
@ -41,7 +42,7 @@ public class SysProfileController extends BaseController
private TokenService tokenService;
@Autowired
private RemoteFileService remoteFileService;
private RemoteSystemFileService remoteFileService;
/**
* 个人信息

Loading…
Cancel
Save