This commit is contained in:
lu7213 committed 2026-03-07 21:51:23 +08:00
1 parent 1639b1e5fd
commit 7883a39de8
4 files changed
+292

No files matched your search

@@ -74,6 +74,8 @@ public interface IEcDeviceService extends IService<EcDevicePlus> {
String snapshop(String deviceCode) throws Exception;
String MjpegSnapshop(String deviceCode) throws Exception;
Map<Long, EcDevicePlus> getMapByIds(List<Long> ids, SFunction<EcDevicePlus, ?>... columns);
List<EcDevicePlus> getByIds(List<Long> ids);
@@ -10,6 +10,7 @@ import com.luqj.ec.domain.dto.EcDeviceHeartbeatDto;
import com.luqj.ec.domain.dto.EcDeviceReportDto;
import com.luqj.ec.mapper.EcDeviceMapper;
import com.luqj.ec.service.IEcDeviceService;
import com.luqj.javacv.utils.MjpegUtil;
import com.luqj.javacv.utils.RtspUtil;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.select.SelectItem;
@@ -72,6 +73,19 @@ public class EcDeviceServiceImpl extends ServiceImpl<EcDeviceMapper, EcDevicePlu
return ruoYiConfig.getReqUrl() + "/ec/" + deviceCode + "/" + fileName;
}
public String MjpegSnapshop(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();
}
String fileName = MjpegUtil.snapshot("http://" + device.getIpAddress() + ":8080/stream.mjpeg?clientId=" + deviceCode, RuoYiConfig.getProfile() + "/ec/" + deviceCode + "/");
return ruoYiConfig.getReqUrl() + "/ec/" + deviceCode + "/" + fileName;
}
@Override
public EcDevicePlus getById(Long id, SFunction<EcDevicePlus, ?>... columns) {
LambdaQueryWrapper<EcDevicePlus> lambdaQueryWrapper = new LambdaQueryWrapper<EcDevicePlus>()
+16
View File
@@ -18,6 +18,22 @@
</properties>
<dependencies>
<dependency>
<groupId>org.jcodec</groupId>
<artifactId>jcodec-javase</artifactId>
<version>0.2.5</version>
</dependency>
<dependency>
<groupId>org.jcodec</groupId>
<artifactId>jcodec</artifactId>
<version>0.2.5</version>
</dependency>
<dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
@@ -0,0 +1,260 @@
package com.luqj.javacv.utils;
import org.bytedeco.ffmpeg.global.avcodec;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;
public class MjpegUtil {
private static final int DEFAULT_TIMEOUT_MS = 10000;
public String snapshop(String basePath) throws Exception {
return "";
}
public static void ffmpegRecord(String mjpegUrl, String outputFile, int seconds) throws Exception {
ffmpegRecord(mjpegUrl, outputFile, seconds, DEFAULT_TIMEOUT_MS);
}
public static void ffmpegRecord(String mjpegUrl, String outputFile, int seconds, int timeoutMs) throws Exception {
AtomicReference<Exception> error = new AtomicReference<>();
Thread thread = new Thread(() -> {
FFmpegFrameGrabber grabber = null;
FFmpegFrameRecorder recorder = null;
Frame frame = null;
try {
grabber = new FFmpegFrameGrabber(mjpegUrl);
grabber.start();
int width = grabber.getImageWidth();
int height = grabber.getImageHeight();
recorder = new FFmpegFrameRecorder(outputFile, width, height);
recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
recorder.setFormat("mp4");
recorder.setFrameRate(25);
recorder.start();
long startTime = System.currentTimeMillis();
long duration = seconds * 1000L;
long frameInterval = 40;
while (System.currentTimeMillis() - startTime < duration) {
frame = grabber.grabImage();
if (frame != null) {
long timestamp = System.currentTimeMillis() - startTime;
recorder.setTimestamp(timestamp * 1000);
recorder.record(frame);
frame.close();
frame = null;
}
Thread.sleep(frameInterval);
}
} catch (Exception e) {
error.set(e);
} finally {
if (frame != null) {
try {
frame.close();
} catch (Exception ignored) {
}
}
if (recorder != null) {
try {
recorder.stop();
recorder.release();
} catch (Exception ignored) {
}
}
if (grabber != null) {
try {
grabber.stop();
grabber.release();
} catch (Exception ignored) {
}
}
}
});
thread.setDaemon(true);
thread.start();
try {
thread.join(timeoutMs);
if (thread.isAlive()) {
throw new Exception("ffmpegRecord timeout");
}
if (error.get() != null) {
throw error.get();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new Exception("ffmpegRecord interrupted");
}
}
public static String ffmepgeSnapshot(String mjpegUrl, String path) throws Exception {
return ffmepgeSnapshot(mjpegUrl, path, DEFAULT_TIMEOUT_MS);
}
public static String ffmepgeSnapshot(String mjpegUrl, 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(mjpegUrl);
grabber.setOption("rtsp_transport", "tcp");
grabber.start();
for (int i = 0; i < 5; i++) {
frame = grabber.grabImage();
if (frame != null) break;
}
if (frame != null) {
converter = new Java2DFrameConverter();
BufferedImage img = converter.getBufferedImage(frame);
String basePath = path;
if (!basePath.endsWith("/")) {
basePath = basePath + "/";
}
String filename = uuid() + "_snapshot.jpg";
String savePathUrl = basePath + filename;
ImageIO.write(img, "jpg", new File(savePathUrl));
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("ffmepgeSnapshot timeout");
}
if (error.get() != null) {
throw error.get();
}
return result.get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new Exception("ffmepgeSnapshot interrupted");
}
}
public static String uuid() {
return UUID.randomUUID().toString().replaceAll("-", "");
}
public static String snapshot(String mjpegUrl, String path) throws Exception {
return snapshot(mjpegUrl, path, DEFAULT_TIMEOUT_MS);
}
public static String snapshot(String mjpegUrl, String path, int timeoutMs) throws Exception {
AtomicReference<String> result = new AtomicReference<>();
AtomicReference<Exception> error = new AtomicReference<>();
Thread thread = new Thread(() -> {
HttpURLConnection conn = null;
InputStream input = null;
try {
URL url = new URL(mjpegUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(5000);
input = conn.getInputStream();
ByteArrayOutputStream jpgBuffer = new ByteArrayOutputStream();
int prev = 0;
int cur;
boolean start = false;
while ((cur = input.read()) != -1) {
if (!start) {
if (prev == 0xFF && cur == 0xD8) {
start = true;
jpgBuffer.write(0xFF);
}
}
if (start) {
jpgBuffer.write(cur);
if (prev == 0xFF && cur == 0xD9) {
break;
}
}
prev = cur;
}
String basePath = path;
if (!basePath.endsWith("/")) {
basePath = basePath + "/";
}
byte[] jpgBytes = jpgBuffer.toByteArray();
String filename = uuid() + "_snapshot.jpg";
String savePathUrl = basePath + filename;
BufferedImage image = ImageIO.read(new ByteArrayInputStream(jpgBytes));
ImageIO.write(image, "jpg", new File(savePathUrl));
result.set(filename);
} catch (Exception e) {
error.set(e);
} finally {
if (input != null) {
try {
input.close();
} catch (Exception ignored) {
}
}
if (conn != null) {
conn.disconnect();
}
}
});
thread.setDaemon(true);
thread.start();
try {
thread.join(timeoutMs);
if (thread.isAlive()) {
throw new Exception("snapshot timeout");
}
if (error.get() != null) {
throw error.get();
}
return result.get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new Exception("snapshot interrupted");
}
}
}