From a71c445a780db388f4353e165901c8a3472a7431 Mon Sep 17 00:00:00 2001 From: chenfei <18955928961@163.com> Date: Fri, 19 Aug 2022 17:39:47 +0800 Subject: [PATCH] =?UTF-8?q?ai=E5=A4=87=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/controller/api/AppController.java | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/AppController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/AppController.java index 153ab6d..91e7b2b 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/AppController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/AppController.java @@ -4,6 +4,8 @@ import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.serializer.SerializerFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.ruoyi.common.annotation.Log; +import com.ruoyi.common.config.RuoYiConfig; +import com.ruoyi.common.constant.Constants; import com.ruoyi.common.constant.UserConstants; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; @@ -15,6 +17,7 @@ import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.file.FileUtils; import com.ruoyi.common.utils.http.HttpUtils; +import com.ruoyi.framework.config.ServerConfig; import com.ruoyi.system.domain.*; import com.ruoyi.system.domain.req.*; import com.ruoyi.system.domain.resp.TRecordResp; @@ -26,8 +29,14 @@ import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; +import sun.misc.BASE64Encoder; + import javax.annotation.Resource; +import javax.imageio.stream.FileImageInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.lang.reflect.Field; import java.util.*; import java.util.stream.Collectors; @@ -61,6 +70,9 @@ public class AppController extends BaseController @Resource private ITImageService imageService; + @Autowired + private ServerConfig serverConfig; + @Value("${aiPost.ip}") private String ip; @Value("${aiPost.port}") @@ -410,6 +422,122 @@ public class AppController extends BaseController return AjaxResult.success(images); } + + @PostMapping("/ai2/{recordId}") + @Log(title = "ai接口备用") + @ApiOperation("ai接口备用") + public AjaxResult aiPost2(@PathVariable("recordId") Long recordId, @RequestBody AiPostReq aiPostReq) + { + //删除同一次预约调用多次ai接口的图片数据 + imageService.deleteTImageByThirdId(recordId); +// saveImage(aiPostReq,recordId); +// revertImageToBase64(aiPostReq); + ObjectMapper mapper = new ObjectMapper(); + String URL = RequestParamsUtil.HTTP + ip + RequestParamsUtil.SEPARATOR + port + url; + JSONObject jsonObject = new JSONObject(); + TRecord tRecord = new TRecord(); + tRecord.setId(recordId); + tRecord.setStatus("1"); + tRecordService.updateTRecord(tRecord); + String result; + Date start; + Date end; + try + { + start = new Date(); + result = HttpUtils.sendPost(URL, mapper.writeValueAsString(delNullData(aiPostReq))); + end = new Date(); + } + catch (Exception e) + { + throw new ServiceException(e.getMessage()); + } + if (!StringUtils.isEmpty(result)) + { + JSONObject data = JSONObject.parseObject(result); + if(data.getInteger("code") != 200){ + TRecord tRecord1 = new TRecord(); + tRecord1.setId(recordId); + tRecord1.setStatus("0"); + tRecordService.updateTRecord(tRecord1); + throw new ServiceException(data.getString("msg")); + } + jsonObject = removeBase64Image(data,recordId); + tRecord.setUploadTime(start); + tRecord.setAiResultReturnTime(end); + tRecord.setAiResult(JSONObject.toJSONString(jsonObject, SerializerFeature.WriteMapNullValue)); + tRecord.setAiResult2(JSONObject.toJSONString(jsonObject, SerializerFeature.WriteMapNullValue)); + tRecordService.updateTRecord(tRecord); + } + Map data = new HashMap<>(); + data.put("result",jsonObject); + TImage tImage = new TImage(); + tImage.setThirdId(recordId); + List images = imageService.selectTImageList(tImage); + data.put("image", images); + return AjaxResult.success(data); + } + + public AiPostReq revertImageToBase64(AiPostReq aiPostReq,Long recordId) + { + Base64ResizedRmgSm base64_resized_img_sm = aiPostReq.getBase64_resized_img_sm(); + + JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(base64_resized_img_sm)); + + + jsonObject.forEach((key,value)->{ + if(value instanceof String){ + jsonObject.put(key,toBase64((String) value)); + TImage tImage = new TImage(); + tImage.setThirdId(recordId); + tImage.setPath((String) value); + tImage.setCreateTime(new Date()); + tImage.setName("sm_"+key); + imageService.insertTImage(tImage); + } + if(value instanceof List){ + ((List) value).forEach(o -> { + jsonObject.put(key,toBase64((String) o)); + TImage tImage = new TImage(); + tImage.setThirdId(recordId); + tImage.setPath((String) o); + tImage.setCreateTime(new Date()); + tImage.setName("sm_"+ key+"_" +((List) value).indexOf(o)); + imageService.insertTImage(tImage); + }); + + } + Base64ResizedRmgSm base64ResizedRmgSm = JSONObject.parseObject(jsonObject.toJSONString(), Base64ResizedRmgSm.class); + aiPostReq.setBase64_resized_img_sm(base64ResizedRmgSm); + }); + return null; + } + + public String toBase64(String path){ + return sourceToBase64(RuoYiConfig.getProfile() + StringUtils.substringAfter(path, Constants.RESOURCE_PREFIX)); + } + + + public static String sourceToBase64(String path) { + byte[] data = null; + FileImageInputStream input = null; + try { + input = new FileImageInputStream(new File(path)); + ByteArrayOutputStream output = new ByteArrayOutputStream(); + byte[] buf = new byte[1024]; + int numBytesRead = 0; + while((numBytesRead = input.read(buf)) != -1){ + output.write(buf, 0, numBytesRead); + } + data = output.toByteArray(); + output.close(); + input.close(); + } catch (Exception e) { + e.printStackTrace(); + } + BASE64Encoder base64Encoder = new BASE64Encoder(); + return base64Encoder.encode(data); + } private void saveImage(AiPostReq aiPostReq,Long recordId) {