截图逻辑
This commit is contained in:
1 parent
30da249b6b
commit
1639b1e5fd
2 files changed
+190
-102
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,120 +9,204 @@ 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;
|
||||||
grabber.setOption("rtsp_transport", "tcp");
|
|
||||||
// 降低首帧延迟
|
public static boolean isStreamAvailable(String rtspUrl) {
|
||||||
grabber.setOption("stimeout", "3000000"); // 3 秒
|
return isStreamAvailable(rtspUrl, DEFAULT_TIMEOUT_MS);
|
||||||
grabber.setOption("buffer_size", "1024000");
|
}
|
||||||
grabber.start();
|
|
||||||
Frame frame;
|
public static boolean isStreamAvailable(String rtspUrl, int timeoutMs) {
|
||||||
Java2DFrameConverter converter = new Java2DFrameConverter();
|
AtomicReference<Boolean> result = new AtomicReference<>(false);
|
||||||
if (!basePath.endsWith("/")) {
|
Thread thread = new Thread(() -> {
|
||||||
basePath = basePath + "/";
|
FFmpegFrameGrabber grabber = null;
|
||||||
}
|
Frame frame = null;
|
||||||
String filename = uuid() + "_snapshot.jpg";
|
try {
|
||||||
String savePathUrl = basePath + filename;
|
grabber = new FFmpegFrameGrabber(rtspUrl);
|
||||||
// 多抓几帧,避免黑屏
|
grabber.setOption("rtsp_transport", "tcp");
|
||||||
for (int i = 0; i < 10; i++) {
|
grabber.setOption("stimeout", "3000000");
|
||||||
frame = grabber.grabImage();
|
grabber.setOption("buffer_size", "1024000");
|
||||||
if (frame != null) {
|
grabber.start();
|
||||||
BufferedImage img = converter.getBufferedImage(frame);
|
frame = grabber.grabImage();
|
||||||
ImageIO.write(img, "jpg", new File(savePathUrl));
|
result.set(frame != null);
|
||||||
break;
|
} 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) {
|
||||||
|
BufferedImage img = converter.getBufferedImage(frame);
|
||||||
|
ImageIO.write(img, "jpg", new File(savePathUrl));
|
||||||
|
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();
|
||||||
|
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");
|
||||||
}
|
}
|
||||||
converter.close();
|
|
||||||
grabber.stop();
|
|
||||||
return filename;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
}
|
||||||
grabber.setOption("rtsp_transport", "tcp");
|
|
||||||
// 降低首帧延迟
|
public static byte[] screenshot(String rtspUrl, int timeoutMs) throws Exception {
|
||||||
grabber.setOption("stimeout", "3000000"); // 3 秒
|
AtomicReference<byte[]> result = new AtomicReference<>(new byte[0]);
|
||||||
grabber.setOption("buffer_size", "1024000");
|
AtomicReference<Exception> error = new AtomicReference<>();
|
||||||
grabber.start();
|
Thread thread = new Thread(() -> {
|
||||||
Frame frame;
|
FFmpegFrameGrabber grabber = null;
|
||||||
Java2DFrameConverter converter = new Java2DFrameConverter();
|
Frame frame = null;
|
||||||
byte[] bytes = new byte[0];
|
Java2DFrameConverter converter = null;
|
||||||
// 多抓几帧,避免黑屏
|
try {
|
||||||
for (int i = 0; i < 10; i++) {
|
grabber = new FFmpegFrameGrabber(rtspUrl);
|
||||||
frame = grabber.grabImage();
|
grabber.setOption("rtsp_transport", "tcp");
|
||||||
if (frame != null) {
|
grabber.setOption("stimeout", "3000000");
|
||||||
BufferedImage img = converter.getBufferedImage(frame);
|
grabber.setOption("buffer_size", "1024000");
|
||||||
// 如果你只想要 byte[](不落盘)
|
grabber.start();
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
converter = new Java2DFrameConverter();
|
||||||
ImageIO.write(img, "jpg", baos);
|
byte[] bytes = new byte[0];
|
||||||
bytes = baos.toByteArray();
|
for (int i = 0; i < 10; i++) {
|
||||||
break;
|
frame = grabber.grabImage();
|
||||||
|
if (frame != null) {
|
||||||
|
BufferedImage img = converter.getBufferedImage(frame);
|
||||||
|
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();
|
||||||
|
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");
|
||||||
}
|
}
|
||||||
converter.close();
|
|
||||||
grabber.stop();
|
|
||||||
return bytes;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user