162 lines
7.5 KiB
Java
162 lines
7.5 KiB
Java
|
package com.pixelai.api.picture.controller;
|
|||
|
|
|||
|
import com.base.annotation.SysLog;
|
|||
|
import com.base.annotation.Unsecured;
|
|||
|
import com.base.helper.Result;
|
|||
|
import com.pixelai.api.component.entity.CpPhoto;
|
|||
|
import com.pixelai.api.component.service.CpPhotoService;
|
|||
|
import com.pixelai.api.pa.entity.PaAiTryon;
|
|||
|
import com.pixelai.api.pa.entity.PaCreation;
|
|||
|
import com.pixelai.api.pa.entity.PaServices;
|
|||
|
import com.pixelai.api.pa.service.PaAiTryonService;
|
|||
|
import com.pixelai.api.pa.service.PaCreationService;
|
|||
|
import com.pixelai.api.pa.service.PaServicesService;
|
|||
|
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.pa.service.PaVipCurrencyService;
|
|||
|
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;
|
|||
|
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;
|
|||
|
|
|||
|
|
|||
|
@SysLog(action = "AiPicture:GenerateImages", value = "AI制图通用接口")
|
|||
|
@ApiOperation(value = "AI制图通用接口",notes = "AI制图通用接口")
|
|||
|
@Unsecured
|
|||
|
@RequestMapping(value="GenerateImages",method = {RequestMethod.POST})
|
|||
|
public Result<CpPhoto> GenerateImages(AiPictureVo aiPictureVo) throws Exception {
|
|||
|
//判断用户是否存在
|
|||
|
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, "该服务不存在", "该服务不存在");
|
|||
|
|
|||
|
//进行扣费
|
|||
|
Boolean pay=paVipCurrencyService.consume(userId,paServices.getPrice());
|
|||
|
if (pay==false)
|
|||
|
return new Result(false, "余额不足", "余额不足");
|
|||
|
|
|||
|
//预记录生成图片位置
|
|||
|
CpPhoto cpPhoto=new CpPhoto(userId,"","https://dashscope-result-bj.oss-cn-beijing.aliyuncs.com/7d/50/20241218/65a8eb47/20241218_151413_seed-59114_000_af3d55fa-bd0f-11ef-9c5b-00163e32aad3.jpg?Expires=1734592453&OSSAccessKeyId=LTAI5tQZd8AEcZX6KZV4G8qL&Signature=h12gb1FJYYPciRBnFvPHzpZ9wkE%3D",aiPictureVo.getServiceId());
|
|||
|
cpPhotoService.save(cpPhoto);
|
|||
|
//预保存用户作品
|
|||
|
PaCreation paCreation=new PaCreation(userId,"",cpPhoto.getId(),aiPictureVo.getServiceId(),"create");
|
|||
|
paCreationService.save(paCreation);
|
|||
|
|
|||
|
//进行图片生成服务
|
|||
|
String serviceApi=paServices.getApi();
|
|||
|
switch(serviceApi){
|
|||
|
//百度:黑白图片上色
|
|||
|
case "ImageColoring" :
|
|||
|
cpPhoto=aiPictureService.ImageColoring(aiPictureVo,paCreation);
|
|||
|
break;
|
|||
|
//百度:人物动漫化
|
|||
|
case "CharacterAnime" :
|
|||
|
cpPhoto=aiPictureService.CharacterAnime(aiPictureVo,paCreation);
|
|||
|
break;
|
|||
|
//百度:更换图片风格
|
|||
|
case "styleTrans" :
|
|||
|
cpPhoto=aiPictureService.styleTrans(aiPictureVo,paCreation);
|
|||
|
break;
|
|||
|
//阿里云:文字生成图片
|
|||
|
case "textToImage" :
|
|||
|
aiPictureService.textToImage(aiPictureVo,paCreation);
|
|||
|
break;
|
|||
|
//阿里云:AI换衣
|
|||
|
case "aiTryon" :
|
|||
|
aiPictureService.aiTryon(aiPictureVo,paCreation);
|
|||
|
break;
|
|||
|
//阿里云:人像风格重绘
|
|||
|
case "portraitStyleRepainting" :
|
|||
|
aiPictureService.portraitStyleRepainting(aiPictureVo,paCreation);
|
|||
|
break;
|
|||
|
//阿里云:艺术字
|
|||
|
case "texture" :
|
|||
|
aiPictureService.texture(aiPictureVo,paCreation);
|
|||
|
break;
|
|||
|
|
|||
|
// case value :
|
|||
|
// //语句
|
|||
|
// break; //可选
|
|||
|
// default :
|
|||
|
// //语句
|
|||
|
}
|
|||
|
|
|||
|
return new Result(true, "调用成功","",cpPhoto);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
// @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));
|
|||
|
// }
|
|||
|
|
|||
|
|
|||
|
}
|