mart-backend/src/main/java/com/pixelai/api/picture/controller/AiPictureController.java

207 lines
9.8 KiB
Java
Raw Normal View History

2024-12-18 17:03:05 +08:00
package com.pixelai.api.picture.controller;
import com.base.annotation.SysLog;
import com.base.annotation.Unsecured;
import com.base.helper.Result;
2025-01-19 23:33:05 +08:00
import com.pixelai.api.component.entity.CpAttachment;
2024-12-18 17:03:05 +08:00
import com.pixelai.api.component.entity.CpPhoto;
2025-01-19 23:33:05 +08:00
import com.pixelai.api.component.service.CpAttachmentService;
2024-12-18 17:03:05 +08:00
import com.pixelai.api.component.service.CpPhotoService;
2025-01-07 22:09:19 +08:00
import com.pixelai.api.pa.entity.*;
import com.pixelai.api.pa.service.*;
2024-12-18 17:03:05 +08:00
import com.pixelai.api.picture.config.ExternalResourceConfig;
import com.pixelai.api.picture.service.AiPictureService;
import com.pixelai.api.picture.util.FileSetUtil;
import com.pixelai.api.component.entity.CpArticle;
import com.pixelai.api.picture.vo.AiPictureVo;
import com.pixelai.api.picture.vo.AiViewVo;
import com.pixelai.api.user.service.AcUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
2025-01-07 22:09:19 +08:00
import org.apache.commons.lang.StringUtils;
2024-12-18 17:03:05 +08:00
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
@Api(tags = "AI制图")
@RestController
@RequestMapping("/AiPicture")
public class AiPictureController {
@Value("${external.resource.dir}")
private String externalResourceDir;
@Autowired
private AiPictureService aiPictureService;
@Autowired
private PaVipCurrencyService paVipCurrencyService;
@Autowired
private AcUserService acUserService;
@Autowired
private PaServicesService paServicesService;
@Autowired
private PaCreationService paCreationService;
@Autowired
private PaAiTryonService paAiTryonService;
@Autowired
private CpPhotoService cpPhotoService;
2025-01-07 22:09:19 +08:00
@Autowired
private MbConsumptionService mbConsumptionService;
2025-01-19 23:33:05 +08:00
@Autowired
private CpAttachmentService cpAttachmentService;
2024-12-18 17:03:05 +08:00
@SysLog(action = "AiPicture:GenerateImages", value = "AI制图通用接口")
@ApiOperation(value = "AI制图通用接口",notes = "AI制图通用接口")
@Unsecured
2025-01-07 22:09:19 +08:00
@RequestMapping(value="GenerateImages",method = {RequestMethod.GET})
2025-01-19 23:33:05 +08:00
public Result<CpAttachment> GenerateImages(AiPictureVo aiPictureVo) throws Exception {
2024-12-18 17:03:05 +08:00
//判断用户是否存在
Integer userId=acUserService.getUserId();
if (userId==null)
return new Result(false, "用户不存在", "用户不存在");
//判断服务是否存在
PaServices paServices=paServicesService.lambdaQuery()
.eq(PaServices::getId,aiPictureVo.getServiceId())
.eq(PaServices::getPublish,1)
.eq(PaServices::getState,"t").one();
if (paServices==null)
return new Result(false, "该服务不存在", "该服务不存在");
2025-01-07 22:09:19 +08:00
//进行扣费并记录日志
2025-01-19 23:33:05 +08:00
Boolean pay=paVipCurrencyService.consume(userId,paServices);
2025-01-07 22:09:19 +08:00
if (pay){
MbConsumption mbConsumption=new MbConsumption(userId,paServices.getName(),"sub",paServices.getPrice(),paServices.consume());
mbConsumptionService.save(mbConsumption);
}else{
2024-12-18 17:03:05 +08:00
return new Result(false, "余额不足", "余额不足");
2025-01-07 22:09:19 +08:00
}
2024-12-18 17:03:05 +08:00
//预记录生成图片位置
2025-01-19 23:33:05 +08:00
CpAttachment cpAttachment=new CpAttachment(userId,"","https://guojunjie.oss-cn-hangzhou.aliyuncs.com/%E5%8A%A0%E8%BD%BD%E7%AD%89%E5%BE%85_%E7%88%B1%E7%BB%99%E7%BD%91_aigei_com.gif",aiPictureVo.getServiceId());
cpAttachmentService.save(cpAttachment);
2024-12-18 17:03:05 +08:00
//预保存用户作品
2025-01-19 23:33:05 +08:00
PaCreation paCreation=new PaCreation(userId,"",cpAttachment.getId(),aiPictureVo.getServiceId(),"create");
paCreation.setManufacturers(paServices.getManufacturers());
paCreation.setParm(aiPictureVo.toString());
2024-12-18 17:03:05 +08:00
paCreationService.save(paCreation);
//进行图片生成服务
String serviceApi=paServices.getApi();
switch(serviceApi){
//百度:黑白图片上色
case "ImageColoring" :
2025-01-19 23:33:05 +08:00
aiPictureService.ImageColoring(aiPictureVo,paCreation);
2024-12-18 17:03:05 +08:00
break;
//百度:人物动漫化
case "CharacterAnime" :
2025-01-19 23:33:05 +08:00
aiPictureService.CharacterAnime(aiPictureVo,paCreation);
2024-12-18 17:03:05 +08:00
break;
//百度:更换图片风格
case "styleTrans" :
2025-01-19 23:33:05 +08:00
aiPictureService.styleTrans(aiPictureVo,paCreation);
2024-12-18 17:03:05 +08:00
break;
//阿里云:文字生成图片
case "textToImage" :
aiPictureService.textToImage(aiPictureVo,paCreation);
break;
//阿里云AI换衣
case "aiTryon" :
2025-01-07 22:09:19 +08:00
if(!(StringUtils.isNotEmpty(aiPictureVo.getImageUrl()) && StringUtils.isNotEmpty(aiPictureVo.getTopImageUrl()))){
return new Result(false, "","人物形象和上衣必须选择");
}
2025-01-19 23:33:05 +08:00
// //判断是否为预览模式,不走阿里云,读取服务器资源
// if(aiPictureVo.getImageUrl().contains("upload/picture") &&
// aiPictureVo.getTopImageUrl().contains("upload/picture") &&
// (StringUtils.isEmpty(aiPictureVo.getBottomImageUrl()) || aiPictureVo.getBottomImageUrl().contains("upload/picture") ) )
// {
// aiPictureService.aiTryonPreview(aiPictureVo,paCreation);
// break;
// }
2025-01-07 22:09:19 +08:00
//换衣自定义,走阿里云
2024-12-18 17:03:05 +08:00
aiPictureService.aiTryon(aiPictureVo,paCreation);
break;
//阿里云:人像风格重绘
case "portraitStyleRepainting" :
aiPictureService.portraitStyleRepainting(aiPictureVo,paCreation);
break;
//阿里云:艺术字
case "texture" :
aiPictureService.texture(aiPictureVo,paCreation);
break;
2025-01-07 22:09:19 +08:00
//火山引擎:文字生成图片
case "textToImageByVolcengine" :
aiPictureService.textToImageByVolcengine(aiPictureVo,paCreation);
break;
//火山引擎:表情编辑
case "emotionPortrait" :
aiPictureService.emotionPortrait(aiPictureVo,paCreation);
break;
//火山引擎:发型编辑
case "hairStyle" :
aiPictureService.hairStyle(aiPictureVo,paCreation);
break;
//火山引擎:年龄变换
case "ageChange" :
aiPictureService.ageChange(aiPictureVo,paCreation);
break;
2025-01-20 08:52:23 +08:00
//// //火山引擎3D游戏特效
// case "threeDGameCartoon" :
// aiPictureService.threeDGameCartoon(aiPictureVo,paCreation);
// break;
2025-01-19 23:33:05 +08:00
//火山引擎:人像漫画风
case "jPCartoon" :
aiPictureService.jPCartoon(aiPictureVo,paCreation);
break;
//火山引擎单张pv写真
case "HighAesSmartDrawing" :
aiPictureService.HighAesSmartDrawing(aiPictureVo,paCreation);
break;
//火山引擎:人像抠图
case "HumanSegmentDemo" :
aiPictureService.HumanSegmentDemo(aiPictureVo,paCreation);
break;
2024-12-18 17:03:05 +08:00
}
2025-01-19 23:33:05 +08:00
return new Result(true, "调用成功","",cpAttachment);
2024-12-18 17:03:05 +08:00
}
// @SysLog(action = "AiPicture:styleTrans", value = "ai图像增强更换图片风格")
// @ApiOperation(value = "更换图片风格",notes = "ai图像增强")
// @RequestMapping(value="styleTrans",method = {RequestMethod.POST})
// @ApiImplicitParams({
// @ApiImplicitParam(name = "userid", value = "id", paramType = "query"),//用户id用于生成base64的txt文件用以转储
// @ApiImplicitParam(name = "image", value = "图片文件必须是图片才行pngjpg皆可后缀名不限", paramType = "query"),
// @ApiImplicitParam(name = "options", value = "图片转换的风格,1:卡通画,2:铅笔风格,3:彩色铅笔画,4:彩色糖块油画,5:神奈川冲浪里油画,6:薰衣草油画,7:奇异油画,8:呐喊油画,9:哥特油画。", paramType = "query")})
// public ResponseEntity<FileSystemResource> styleTrans(@RequestParam("userid")Integer id, @RequestParam("image") MultipartFile image, @RequestParam("options") Integer options) throws Exception {
// String fileName= FileSetUtil.resetFileName(id,externalResourceDir,image);//生成的filename无后缀
// File file=FileSetUtil.saveFile(image,externalResourceDir,fileName);//生成的file为png文件
// System.out.println(fileName+"//////////"+externalResourceDir);
// String filePath=aiPictureService.styleTrans(fileName,externalResourceDir,options);//转码传输到baiduapi后写入fileName+.txt,所返回的filePath直接指向对应txt文件
// aiPictureService.Base64ToImage(fileName,filePath,externalResourceDir);
// HttpHeaders headers = new HttpHeaders();
// headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file.getName());
// return ResponseEntity.ok()
// .headers(headers)
// .contentLength(file.length())
// .contentType(MediaType.APPLICATION_OCTET_STREAM)
// .body(new FileSystemResource(file));
// }
}