发布任务,和跌倒检测,处理上传图片的接口
在网页上获取obs的虚拟摄像头进行跌倒检测的功能
package com.example.teamapplication.controller; import com.aliyun.com.viapi.FileUtils; import com.aliyun.facebody20191230.models.RecognizeActionResponse; import com.aliyun.facebody20191230.models.RecognizeActionResponseBody; import com.aliyun.tea.TeaModel; import com.example.teamapplication.common.Result; import com.example.teamapplication.entity.Task; import com.example.teamapplication.mapper.TaskMapper; import com.example.teamapplication.service.TaskService; import com.github.pagehelper.PageHelper; import jakarta.annotation.Resource; import com.github.pagehelper.PageInfo; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Base64; import java.util.List; /** * <p> * 前端控制器 * </p> * * @author xu * @since 2024-05-04 */ @RestController @RequestMapping("/task") public class TaskController { @Resource private TaskService taskService; private TaskMapper taskMapper; public TaskController(TaskMapper taskMapper) { this.taskMapper = taskMapper; } @PostMapping("add") public Result Add(@RequestBody Task task){ if(task != null){ taskService.save(task); return Result.suc(); } return Result.fail(); } @PostMapping("update") public Result Update(@RequestParam String id,@RequestParam String receiveid){ System.out.println(receiveid); List list = taskService.lambdaQuery().eq(Task::getId,id).list(); Task task = (Task) list.get(0); if(task!=null && task.getSentid() != Integer.valueOf(receiveid)){ task.setReceiveid(Integer.valueOf(receiveid)); task.setState(1); taskService.saveOrUpdate(task); return Result.suc(); } return Result.fail(); } @PostMapping("finish") public Result Finish(@RequestParam String id,@RequestParam String receiveid){ List list = taskService.lambdaQuery().eq(Task::getId,id).list(); Task task = (Task) list.get(0); if(task!=null && task.getSentid() != Integer.valueOf(receiveid)){ task.setReceiveid(Integer.valueOf(receiveid)); task.setState(task.getState()+1); taskService.saveOrUpdate(task); return Result.suc(); } return Result.fail(); } @PostMapping("confirm") public Result Confitm(@RequestParam String id,@RequestParam String sentid){ List list = taskService.lambdaQuery().eq(Task::getId,id).list(); Task task = (Task) list.get(0); if(task!=null && task.getSentid() == Integer.valueOf(sentid)){ task.setState(task.getState()+1); taskService.saveOrUpdate(task); return Result.suc(); } return Result.fail(); } @PostMapping("listPage") public Result listPage(){ List list = taskService.lambdaQuery().list(); ArrayList<Task> taskArrayList = new ArrayList<>(); for(int i = 0;i<list.size();i++){ taskArrayList.add((Task) list.get(i)); } return Result.suc(taskArrayList); } @GetMapping("/selectPage") public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize, Task task){ List list = taskService.lambdaQuery().list(); List<Task> tasks = new ArrayList<>(); for(int i = 0;i<list.size();i++){ Task task1 = (Task) list.get(i); tasks.add(task1); } for (Task task1 : tasks){ System.out.println(task1.getDescription()); } PageInfo<Task> pageInfo = PageInfo.of(tasks); PageHelper.startPage(pageNum,pageSize); return Result.success(pageInfo); } @PostMapping("/uploadImage") public String uploadImage(@RequestBody String base64Image) { try { // 解码Base64图像数据 byte[] decodedBytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(base64Image.split(",")[1]); System.out.println(decodedBytes); // 将图像保存到服务器 File file = new File("D:\\project\\android\\stud\\picture\\image.png"); System.out.println(file); FileOutputStream outputStream = new FileOutputStream(file); outputStream.write(decodedBytes); outputStream.close(); sync(); return "Image uploaded successfully."; } catch (IOException e) { e.printStackTrace(); return "Failed to upload image."; } catch (Exception e) { e.printStackTrace(); return "Failed to judge image"; } } @PostMapping("findByTip") public Result findByTip(@RequestParam String tip){ List list = taskService.lambdaQuery().like(Task::getTip1,tip).like(Task::getTip2,tip) .like(Task::getTip3,tip).list(); ArrayList<Task> taskArrayList = new ArrayList<>(); for(int i = 0;i<list.size();i++){ taskArrayList.add((Task) list.get(i)); } return Result.suc(taskArrayList); } @PostMapping("sync") public void sync() throws Exception{ String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"); String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"); com.aliyun.facebody20191230.Client client = ActionController.createClient(accessKeyId, accessKeySecret); // 场景一,使用本地文件 String file = "D:\\project\\android\\stud\\picture\\image.png"; byte[] img = convertImageToByteArray(file); FileUtils fileUtils = FileUtils.getInstance(accessKeyId, accessKeySecret); String ossUrl = fileUtils.upload(file); com.aliyun.facebody20191230.models.RecognizeActionRequest.RecognizeActionRequestURLList URLList0 = new com.aliyun.facebody20191230.models.RecognizeActionRequest.RecognizeActionRequestURLList() .setURL(ossUrl); com.aliyun.facebody20191230.models.RecognizeActionRequest.RecognizeActionRequestURLList URLList1 = new com.aliyun.facebody20191230.models.RecognizeActionRequest.RecognizeActionRequestURLList() .setURL(ossUrl); com.aliyun.facebody20191230.models.RecognizeActionRequest.RecognizeActionRequestURLList URLList2 = new com.aliyun.facebody20191230.models.RecognizeActionRequest.RecognizeActionRequestURLList() .setURL(ossUrl); com.aliyun.facebody20191230.models.RecognizeActionRequest.RecognizeActionRequestURLList URLList3 = new com.aliyun.facebody20191230.models.RecognizeActionRequest.RecognizeActionRequestURLList() .setURL(ossUrl); com.aliyun.facebody20191230.models.RecognizeActionRequest recognizeActionRequest = new com.aliyun.facebody20191230.models.RecognizeActionRequest() .setType(1) .setURLList(java.util.Arrays.asList( URLList0, URLList1, URLList2, URLList3 )); com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions(); try { RecognizeActionResponse recognizeActionResponse = client.recognizeActionWithOptions(recognizeActionRequest, runtime); // 获取整体结果 System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(recognizeActionResponse))); // 假设recognizeActionResponse是您从API调用中获得的响应对象 // 假设recognizeExpressionResponse是您从API调用中获得的RecognizeExpressionResponse对象 // 假设recognizeActionResponse是您从API调用中获得的RecognizeActionResponse对象 RecognizeActionResponseBody body = recognizeActionResponse.getBody(); // 获取Elements列表 List<RecognizeActionResponseBody.RecognizeActionResponseBodyDataElements> elements = body.getData().elements; // 遍历Elements列表,获取Labels属性,即标签信息 for (RecognizeActionResponseBody.RecognizeActionResponseBodyDataElements element : elements) { List<String> labels = element.getLabels(); for (String label : labels) { if(label.equals("跌倒")){ System.out.println("成功"); //添加新的任务 Task task = new Task(); task.setSentid(10); task.setName("跌倒"); task.setDescription("有人跌倒了,请尽快前往"); task.setTip1("医疗"); task.setPosition("教学楼"); task.setTime(LocalDateTime.now()); task.setBlob(img); taskService.saveOrUpdate(task); System.out.println("成功"); } System.out.println("标签: " + label); } } } catch (com.aliyun.tea.TeaException teaException) { // 获取整体报错信息 // 获取单个字段 System.out.println(teaException.getCode()); } } public static byte[] convertImageToByteArray(String filePath) throws IOException { File file = new File(filePath); FileInputStream fis = new FileInputStream(file); byte[] imageData = new byte[(int) file.length()]; fis.read(imageData); fis.close(); return imageData; } }
标签:Task,return,list,冲刺,task,Result,import,第二阶段 From: https://www.cnblogs.com/azure011328/p/18247445