新增按钮

This commit is contained in:
sulv 2024-10-10 23:35:58 +08:00
parent 8a0924f649
commit 7738b2fdb2
3 changed files with 84 additions and 8 deletions

View File

@ -106,11 +106,15 @@ public class VideoInferenceApp extends JFrame {
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 5));
JButton recognizeButton = new JButton("识别图片");
recognizeButton.setPreferredSize(new Dimension(120, 30));
controlPanel.add(recognizeButton);
// 创建控制按钮
JButton playButton = new JButton("播放");
JButton pauseButton = new JButton("暂停");
JButton replayButton = new JButton("重播");
JButton fastForward5sButton = new JButton("快进5");
JButton fastForward5sButton = new JButton("快进1");
JButton rewind5sButton = new JButton("后退5秒");
// 设置按钮的统一高度
@ -212,19 +216,32 @@ public class VideoInferenceApp extends JFrame {
// 重播按钮
replayButton.addActionListener(e -> {
try {
// videoPlayer.loadVideo(videoPlayer.getCurrentVideoPath());
videoPlayer.playVideo();
videoPlayer.replayVideo(); // 调用重播方法
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "重播视频失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
}
});
// // 后退5秒
// rewind5sButton.addActionListener(e -> videoPlayer.rewind(5000));
//
// // 快进5秒
// fastForward5sButton.addActionListener(e -> videoPlayer.fastForward(5000));
// 后退5秒
rewind5sButton.addActionListener(e -> {
try {
videoPlayer.rewind(5000); // 后退5000毫秒5秒
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "后退失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
}
});
// 快进5秒
fastForward5sButton.addActionListener(e -> {
try {
videoPlayer.fastForward(1000); // 快进5000毫秒5秒
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "快进失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
}
});
// 开始播放按钮的行为
startPlayButton.addActionListener(e -> {
@ -264,6 +281,7 @@ public class VideoInferenceApp extends JFrame {
}
}
// 选择图片文件
// 选择图片文件
private void selectImageFile() {
File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
@ -279,6 +297,7 @@ public class VideoInferenceApp extends JFrame {
File selectedFile = fileChooser.getSelectedFile();
try {
videoPlayer.loadImage(selectedFile.getAbsolutePath());
JOptionPane.showMessageDialog(this, "图片加载成功,请点击“识别图片”按钮进行识别。", "提示", JOptionPane.INFORMATION_MESSAGE);
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "加载图片失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);

View File

@ -66,9 +66,15 @@ public class ModelManager extends JPanel {
deleteMenuItem.addActionListener(e -> {
int selectedIndex = modelList.getSelectedIndex();
if (selectedIndex != -1) {
ModelInfo modelInfo = modelListModel.getElementAt(selectedIndex); // 获取要删除的模型信息
int confirmation = JOptionPane.showConfirmDialog(null, "确定要删除此模型吗?", "确认删除", JOptionPane.YES_NO_OPTION);
if (confirmation == JOptionPane.YES_OPTION) {
modelListModel.remove(selectedIndex); // 删除选中的模型
// VideoPlayer 中移除对应的推理引擎
if (videoPlayer != null) {
videoPlayer.removeInferenceEngine(modelInfo.getModelFilePath());
}
}
}
});

View File

@ -59,6 +59,8 @@ public class VideoPlayer {
this.modelManager = modelManager;
}
// 加载视频或流
public void loadVideo(String videoFilePathOrStreamUrl) throws Exception {
stopVideo();
@ -376,12 +378,61 @@ public class VideoPlayer {
frameDataQueue.clear();
}
// 删除推理引擎
public void removeInferenceEngine(String modelPath) {
inferenceEngines.removeIf(engine -> engine.getModelPath().equals(modelPath));
}
public void addInferenceEngines(InferenceEngine inferenceEngine) {
this.inferenceEngines.add(inferenceEngine);
}
// 添加重播方法
// 添加重播方法
public void replayVideo() throws Exception {
if (videoCapture != null && videoCapture.isOpened()) {
boolean success = videoCapture.set(Videoio.CAP_PROP_POS_FRAMES, 0);
if (!success) {
throw new Exception("无法重置视频到起始位置。");
}
currentTimestamp = 0;
} else {
throw new Exception("视频未加载或未打开。");
}
}
// 添加 rewind fastForward 方法
public void rewind(long millis) throws Exception {
if (videoCapture == null || !videoCapture.isOpened()) {
throw new Exception("视频未加载或未打开");
}
long newTimestamp = currentTimestamp - millis;
if (newTimestamp < 0) newTimestamp = 0;
boolean success = videoCapture.set(Videoio.CAP_PROP_POS_MSEC, newTimestamp);
if (!success) {
throw new Exception("无法后退视频。");
}
currentTimestamp = newTimestamp;
}
public synchronized void fastForward(long millis) throws Exception {
if (videoCapture == null || !videoCapture.isOpened()) {
throw new Exception("视频未加载或未打开");
}
long newTimestamp = currentTimestamp + millis;
if (videoDuration > 0 && newTimestamp > videoDuration) {
newTimestamp = videoDuration;
}
boolean success = videoCapture.set(Videoio.CAP_PROP_POS_MSEC, newTimestamp);
if (!success) {
throw new Exception("无法快进视频。");
}
currentTimestamp = newTimestamp;
}
// 加载并处理图片
public void loadImage(String imagePath) throws Exception {
// 停止任何正在播放的视频
stopVideo();