截图逻辑
This commit is contained in:
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 {
|
||||
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 + "/");
|
||||
if (!file.exists()) {
|
||||
file.mkdirs();
|
||||
|
||||
@@ -9,24 +9,82 @@ import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
public class RtspUtil {
|
||||
public static String screenshot(String rtspUrl, String basePath) throws Exception {
|
||||
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(rtspUrl);
|
||||
// 🔑 等价于:-rtsp_transport tcp
|
||||
|
||||
private static final int DEFAULT_TIMEOUT_MS = 5000;
|
||||
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("stimeout", "3000000"); // 3 秒
|
||||
grabber.setOption("stimeout", "3000000");
|
||||
grabber.setOption("buffer_size", "1024000");
|
||||
grabber.start();
|
||||
Frame frame;
|
||||
Java2DFrameConverter converter = new Java2DFrameConverter();
|
||||
frame = grabber.grabImage();
|
||||
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("/")) {
|
||||
basePath = basePath + "/";
|
||||
}
|
||||
String filename = uuid() + "_snapshot.jpg";
|
||||
String savePathUrl = basePath + filename;
|
||||
// 多抓几帧,避免黑屏
|
||||
for (int i = 0; i < 10; i++) {
|
||||
frame = grabber.grabImage();
|
||||
if (frame != null) {
|
||||
@@ -35,94 +93,120 @@ public class RtspUtil {
|
||||
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();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
if (grabber != null) {
|
||||
try {
|
||||
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 {
|
||||
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(rtspUrl);
|
||||
// 🔑 等价于:-rtsp_transport tcp
|
||||
return screenshot(rtspUrl, DEFAULT_SCREENSHOT_TIMEOUT_MS);
|
||||
}
|
||||
|
||||
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("stimeout", "3000000"); // 3 秒
|
||||
grabber.setOption("stimeout", "3000000");
|
||||
grabber.setOption("buffer_size", "1024000");
|
||||
grabber.start();
|
||||
Frame frame;
|
||||
Java2DFrameConverter converter = new Java2DFrameConverter();
|
||||
converter = new Java2DFrameConverter();
|
||||
byte[] bytes = new byte[0];
|
||||
// 多抓几帧,避免黑屏
|
||||
for (int i = 0; i < 10; i++) {
|
||||
frame = grabber.grabImage();
|
||||
if (frame != null) {
|
||||
BufferedImage img = converter.getBufferedImage(frame);
|
||||
// 如果你只想要 byte[](不落盘)
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ImageIO.write(img, "jpg", baos);
|
||||
bytes = baos.toByteArray();
|
||||
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();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
if (grabber != null) {
|
||||
try {
|
||||
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() {
|
||||
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) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user