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.CpAttachment; import com.pixelai.api.component.entity.CpPhoto; import com.pixelai.api.component.service.CpAttachmentService; import com.pixelai.api.component.service.CpPhotoService; import com.pixelai.api.pa.entity.*; import com.pixelai.api.pa.service.*; 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; import org.apache.commons.lang.StringUtils; 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; @Autowired private MbConsumptionService mbConsumptionService; @Autowired private CpAttachmentService cpAttachmentService; @SysLog(action = "AiPicture:GenerateImages", value = "AI制图通用接口") @ApiOperation(value = "AI制图通用接口",notes = "AI制图通用接口") @Unsecured @RequestMapping(value="GenerateImages",method = {RequestMethod.GET}) public Result 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); if (pay){ MbConsumption mbConsumption=new MbConsumption(userId,paServices.getName(),"sub",paServices.getPrice(),paServices.consume()); mbConsumptionService.save(mbConsumption); }else{ return new Result(false, "余额不足", "余额不足"); } //预记录生成图片位置 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); //预保存用户作品 PaCreation paCreation=new PaCreation(userId,"",cpAttachment.getId(),aiPictureVo.getServiceId(),"create"); paCreation.setManufacturers(paServices.getManufacturers()); paCreation.setParm(aiPictureVo.toString()); paCreationService.save(paCreation); //进行图片生成服务 String serviceApi=paServices.getApi(); switch(serviceApi){ //百度:黑白图片上色 case "ImageColoring" : aiPictureService.ImageColoring(aiPictureVo,paCreation); break; //百度:人物动漫化 case "CharacterAnime" : aiPictureService.CharacterAnime(aiPictureVo,paCreation); break; //百度:更换图片风格 case "styleTrans" : aiPictureService.styleTrans(aiPictureVo,paCreation); break; //阿里云:文字生成图片 case "textToImage" : aiPictureService.textToImage(aiPictureVo,paCreation); break; //阿里云:AI换衣 case "aiTryon" : if(!(StringUtils.isNotEmpty(aiPictureVo.getImageUrl()) && StringUtils.isNotEmpty(aiPictureVo.getTopImageUrl()))){ return new Result(false, "","人物形象和上衣必须选择"); } // //判断是否为预览模式,不走阿里云,读取服务器资源 // 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; // } //换衣自定义,走阿里云 aiPictureService.aiTryon(aiPictureVo,paCreation); break; //阿里云:人像风格重绘 case "portraitStyleRepainting" : aiPictureService.portraitStyleRepainting(aiPictureVo,paCreation); break; //阿里云:艺术字 case "texture" : aiPictureService.texture(aiPictureVo,paCreation); break; //火山引擎:文字生成图片 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; //// //火山引擎:3D游戏特效 // case "threeDGameCartoon" : // aiPictureService.threeDGameCartoon(aiPictureVo,paCreation); // break; //火山引擎:人像漫画风 case "jPCartoon" : aiPictureService.jPCartoon(aiPictureVo,paCreation); break; //火山引擎:单张pv写真 case "HighAesSmartDrawing" : aiPictureService.HighAesSmartDrawing(aiPictureVo,paCreation); break; //火山引擎:人像抠图 case "HumanSegmentDemo" : aiPictureService.HumanSegmentDemo(aiPictureVo,paCreation); break; } return new Result(true, "调用成功","",cpAttachment); } // @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 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)); // } }