截图逻辑

This commit is contained in:
lu7213 committed 2026-03-07 16:17:48 +08:00
1 parent 30da249b6b
commit 1639b1e5fd
2 files changed
+160 -72

No files matched your search

@@ -60,6 +60,10 @@ public class EcDeviceServiceImpl extends ServiceImpl<EcDeviceMapper, EcDevicePlu
} }
public String snapshop(String deviceCode) throws Exception { public String snapshop(String deviceCode) throws Exception {
EcDevicePlus device = selectEcDeviceByDeviceCode(deviceCode);
if (device == null || device.getStatus() == null || device.getStatus() == 0) {
return ruoYiConfig.getReqUrl() + "/ec/default/offline.png";
}
File file = new File(RuoYiConfig.getProfile() + "/ec/" + deviceCode + "/"); File file = new File(RuoYiConfig.getProfile() + "/ec/" + deviceCode + "/");
if (!file.exists()) { if (!file.exists()) {
file.mkdirs(); file.mkdirs();
@@ -9,24 +9,82 @@ import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;
public class RtspUtil { public class RtspUtil {
public static String screenshot(String rtspUrl, String basePath) throws Exception {
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(rtspUrl); private static final int DEFAULT_TIMEOUT_MS = 5000;
// 🔑 等价于:-rtsp_transport tcp private static final int DEFAULT_SCREENSHOT_TIMEOUT_MS = 15000;
public static boolean isStreamAvailable(String rtspUrl) {
return isStreamAvailable(rtspUrl, DEFAULT_TIMEOUT_MS);
}
public static boolean isStreamAvailable(String rtspUrl, int timeoutMs) {
AtomicReference<Boolean> result = new AtomicReference<>(false);
Thread thread = new Thread(() -> {
FFmpegFrameGrabber grabber = null;
Frame frame = null;
try {
grabber = new FFmpegFrameGrabber(rtspUrl);
grabber.setOption("rtsp_transport", "tcp"); grabber.setOption("rtsp_transport", "tcp");
// 降低首帧延迟 grabber.setOption("stimeout", "3000000");
grabber.setOption("stimeout", "3000000"); // 3 秒
grabber.setOption("buffer_size", "1024000"); grabber.setOption("buffer_size", "1024000");
grabber.start(); grabber.start();
Frame frame; frame = grabber.grabImage();
Java2DFrameConverter converter = new Java2DFrameConverter(); result.set(frame != null);
} catch (Exception ignored) {
} finally {
if (frame != null) {
try {
frame.close();
} catch (Exception ignored) {
}
}
if (grabber != null) {
try {
grabber.stop();
grabber.close();
} catch (Exception ignored) {
}
}
}
});
thread.setDaemon(true);
thread.start();
try {
thread.join(timeoutMs);
return result.get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return false;
}
}
public static String screenshot(String rtspUrl, String basePath) throws Exception {
return screenshot(rtspUrl, basePath, DEFAULT_SCREENSHOT_TIMEOUT_MS);
}
public static String screenshot(String rtspUrl, String path, int timeoutMs) throws Exception {
AtomicReference<String> result = new AtomicReference<>();
AtomicReference<Exception> error = new AtomicReference<>();
Thread thread = new Thread(() -> {
FFmpegFrameGrabber grabber = null;
Frame frame = null;
Java2DFrameConverter converter = null;
try {
grabber = new FFmpegFrameGrabber(rtspUrl);
grabber.setOption("rtsp_transport", "tcp");
grabber.setOption("stimeout", "3000000");
grabber.setOption("buffer_size", "1024000");
grabber.start();
converter = new Java2DFrameConverter();
String basePath = path;
if (!basePath.endsWith("/")) { if (!basePath.endsWith("/")) {
basePath = basePath + "/"; basePath = basePath + "/";
} }
String filename = uuid() + "_snapshot.jpg"; String filename = uuid() + "_snapshot.jpg";
String savePathUrl = basePath + filename; String savePathUrl = basePath + filename;
// 多抓几帧,避免黑屏
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
frame = grabber.grabImage(); frame = grabber.grabImage();
if (frame != null) { if (frame != null) {
@@ -35,94 +93,120 @@ public class RtspUtil {
break; break;
} }
} }
result.set(filename);
} catch (Exception e) {
error.set(e);
} finally {
if (frame != null) {
try {
frame.close();
} catch (Exception ignored) {
}
}
if (converter != null) {
try {
converter.close(); converter.close();
} catch (Exception ignored) {
}
}
if (grabber != null) {
try {
grabber.stop(); grabber.stop();
return filename; grabber.close();
} catch (Exception ignored) {
}
}
}
});
thread.setDaemon(true);
thread.start();
try {
thread.join(timeoutMs);
if (thread.isAlive()) {
throw new Exception("screenshot timeout");
}
if (error.get() != null) {
throw error.get();
}
return result.get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new Exception("screenshot interrupted");
}
} }
public static byte[] screenshot(String rtspUrl) throws Exception { public static byte[] screenshot(String rtspUrl) throws Exception {
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(rtspUrl); return screenshot(rtspUrl, DEFAULT_SCREENSHOT_TIMEOUT_MS);
// 🔑 等价于:-rtsp_transport tcp }
public static byte[] screenshot(String rtspUrl, int timeoutMs) throws Exception {
AtomicReference<byte[]> result = new AtomicReference<>(new byte[0]);
AtomicReference<Exception> error = new AtomicReference<>();
Thread thread = new Thread(() -> {
FFmpegFrameGrabber grabber = null;
Frame frame = null;
Java2DFrameConverter converter = null;
try {
grabber = new FFmpegFrameGrabber(rtspUrl);
grabber.setOption("rtsp_transport", "tcp"); grabber.setOption("rtsp_transport", "tcp");
// 降低首帧延迟 grabber.setOption("stimeout", "3000000");
grabber.setOption("stimeout", "3000000"); // 3 秒
grabber.setOption("buffer_size", "1024000"); grabber.setOption("buffer_size", "1024000");
grabber.start(); grabber.start();
Frame frame; converter = new Java2DFrameConverter();
Java2DFrameConverter converter = new Java2DFrameConverter();
byte[] bytes = new byte[0]; byte[] bytes = new byte[0];
// 多抓几帧,避免黑屏
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
frame = grabber.grabImage(); frame = grabber.grabImage();
if (frame != null) { if (frame != null) {
BufferedImage img = converter.getBufferedImage(frame); BufferedImage img = converter.getBufferedImage(frame);
// 如果你只想要 byte[](不落盘)
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img, "jpg", baos); ImageIO.write(img, "jpg", baos);
bytes = baos.toByteArray(); bytes = baos.toByteArray();
break; break;
} }
} }
result.set(bytes);
} catch (Exception e) {
error.set(e);
} finally {
if (frame != null) {
try {
frame.close();
} catch (Exception ignored) {
}
}
if (converter != null) {
try {
converter.close(); converter.close();
} catch (Exception ignored) {
}
}
if (grabber != null) {
try {
grabber.stop(); grabber.stop();
return bytes; grabber.close();
} catch (Exception ignored) {
}
}
}
});
thread.setDaemon(true);
thread.start();
try {
thread.join(timeoutMs);
if (thread.isAlive()) {
throw new Exception("screenshot timeout");
}
if (error.get() != null) {
throw error.get();
}
return result.get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new Exception("screenshot interrupted");
}
} }
public static String uuid() { public static String uuid() {
return UUID.randomUUID().toString().replaceAll("-", ""); return UUID.randomUUID().toString().replaceAll("-", "");
} }
public static boolean isStreamAvailable(String rtspUrl) {
FFmpegFrameGrabber grabber = null;
try {
grabber = new FFmpegFrameGrabber(rtspUrl);
grabber.setOption("rtsp_transport", "tcp");
grabber.setOption("stimeout", "5000000");
grabber.setOption("buffer_size", "1024000");
grabber.start();
for (int i = 0; i < 5; i++) {
Frame frame = grabber.grabImage();
if (frame != null) {
return true;
}
}
return false;
} catch (Exception e) {
return false;
} finally {
if (grabber != null) {
try {
grabber.stop();
} catch (Exception ignored) {
}
}
}
}
public static boolean isStreamAvailable(String rtspUrl, int timeoutMs) {
FFmpegFrameGrabber grabber = null;
try {
grabber = new FFmpegFrameGrabber(rtspUrl);
grabber.setOption("rtsp_transport", "tcp");
grabber.setOption("stimeout", String.valueOf(timeoutMs * 1000));
grabber.setOption("buffer_size", "1024000");
grabber.start();
for (int i = 0; i < 5; i++) {
Frame frame = grabber.grabImage();
if (frame != null) {
return true;
}
}
return false;
} catch (Exception e) {
return false;
} finally {
if (grabber != null) {
try {
grabber.stop();
} catch (Exception ignored) {
}
}
}
}
} }