From c932c1c7a246ad1fef859cb19add5496c61d1e32 Mon Sep 17 00:00:00 2001 From: 16337 <1633794139@qq.com> Date: Sat, 28 Feb 2026 14:38:15 +0800 Subject: [PATCH] =?UTF-8?q?fix(aiot):=20snap=E7=AB=AF=E7=82=B9=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=9B=BE=E7=89=87=E7=9B=B4=E6=8E=A5=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=EF=BC=8C=E5=85=BC=E5=AE=B9=E5=89=8D=E7=AB=AF=E6=A0=87?= =?UTF-8?q?=E7=AD=BE=E7=9B=B4=E6=8E=A5=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 前端将snap API URL直接设为,浏览器以Accept: image/*请求, 但原接口返回JSON导致图片无法显示。现通过检测Accept头自动判断: - image/*请求:触发截图后直接返回JPEG字节流 - JSON请求:返回原有JSON格式 Co-Authored-By: Claude Opus 4.6 --- .../vmp/aiot/controller/AiRoiController.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/genersoft/iot/vmp/aiot/controller/AiRoiController.java b/src/main/java/com/genersoft/iot/vmp/aiot/controller/AiRoiController.java index 1b3b6604a..2d5a74096 100644 --- a/src/main/java/com/genersoft/iot/vmp/aiot/controller/AiRoiController.java +++ b/src/main/java/com/genersoft/iot/vmp/aiot/controller/AiRoiController.java @@ -85,10 +85,26 @@ public class AiRoiController { @Operation(summary = "获取摄像头截图(Edge截图 → COS URL)") @GetMapping("/snap") - public Map getSnap( + public ResponseEntity getSnap( @RequestParam String cameraCode, - @RequestParam(defaultValue = "false") boolean force) { - return screenshotService.requestScreenshot(cameraCode, force); + @RequestParam(defaultValue = "false") boolean force, + @RequestHeader(value = "Accept", defaultValue = "application/json") String accept) { + + Map result = screenshotService.requestScreenshot(cameraCode, force); + + // 如果是 标签发起的请求(Accept: image/*),直接返回图片字节 + if (accept.contains("image/") && "ok".equals(result.get("status"))) { + byte[] image = screenshotService.proxyScreenshotImage(cameraCode); + if (image != null) { + return ResponseEntity.ok() + .contentType(MediaType.IMAGE_JPEG) + .header("Cache-Control", "max-age=60") + .body(image); + } + } + + // 默认返回 JSON(XHR/fetch 请求) + return ResponseEntity.ok(result); } @Operation(summary = "Edge 截图回调(Edge 主动调用)")