使用javacv 结合swing,写一个操作简单明了的录屏小软件
时间: 2024-02-11 07:03:47 浏览: 241
好的,下面是一个使用JavaCv结合Swing编写的简单录屏小软件示例。该示例基本实现了录屏、暂停、继续、停止等基本功能。
首先,需要引入JavaCv和FFmpeg的库文件,例如:
```xml
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>ffmpeg</artifactId>
<version>4.4</version>
</dependency>
```
然后,创建一个Swing界面,添加开始录制、暂停录制、继续录制、停止录制等按钮,以及显示录制状态的标签:
```java
import org.bytedeco.ffmpeg.global.avcodec;
import org.bytedeco.ffmpeg.global.avutil;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
public class ScreenRecorderApp extends JFrame {
private FFmpegFrameRecorder recorder;
private Java2DFrameConverter converter;
private Robot robot;
private Rectangle screenRect;
private BufferedImage image;
private boolean isRecording;
private boolean isPaused;
private JLabel statusLabel;
public ScreenRecorderApp() {
super("屏幕录制");
// 初始化录制参数
int width = 1920;
int height = 1080;
int fps = 30;
try {
recorder = new FFmpegFrameRecorder("output.mp4", width, height);
recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
recorder.setFormat("mp4");
recorder.setFrameRate(fps);
recorder.setPixelFormat(avutil.AV_PIX_FMT_YUV420P);
recorder.start();
converter = new Java2DFrameConverter();
robot = new Robot();
screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
isRecording = false;
isPaused = false;
// 初始化界面
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JButton startButton = new JButton("开始录制");
startButton.addActionListener((e) -> {
if (!isRecording) {
isRecording = true;
isPaused = false;
statusLabel.setText("正在录制...");
}
});
JButton pauseButton = new JButton("暂停录制");
pauseButton.addActionListener((e) -> {
if (isRecording && !isPaused) {
isPaused = true;
statusLabel.setText("已暂停录制");
}
});
JButton resumeButton = new JButton("继续录制");
resumeButton.addActionListener((e) -> {
if (isRecording && isPaused) {
isPaused = false;
statusLabel.setText("正在录制...");
}
});
JButton stopButton = new JButton("停止录制");
stopButton.addActionListener((e) -> {
if (isRecording) {
isRecording = false;
statusLabel.setText("已停止录制");
try {
recorder.stop();
recorder.release();
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
statusLabel = new JLabel("就绪");
statusLabel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
statusLabel.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(startButton, BorderLayout.WEST);
panel.add(pauseButton);
panel.add(resumeButton);
panel.add(stopButton, BorderLayout.EAST);
add(panel, BorderLayout.NORTH);
add(statusLabel, BorderLayout.CENTER);
setSize(400, 150);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public void startRecording() {
new Thread(() -> {
long startTime = System.currentTimeMillis();
while (isRecording) {
if (!isPaused) {
try {
image = robot.createScreenCapture(screenRect);
Frame frame = converter.convert(image);
recorder.record(frame);
} catch (Exception e) {
e.printStackTrace();
}
}
try {
Thread.sleep(1000 / recorder.getFrameRate());
} catch (InterruptedException e) {
e.printStackTrace();
}
if (System.currentTimeMillis() - startTime > 60 * 1000) {
isRecording = false;
statusLabel.setText("已停止录制");
try {
recorder.stop();
recorder.release();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).start();
}
public static void main(String[] args) {
new ScreenRecorderApp().startRecording();
}
}
```
以上代码中,通过创建一个新的线程来进行录制,每隔一定时间获取屏幕截图并转换成JavaCv的Frame对象,写入到视频文件中。可以通过控制isRecording和isPaused的值来控制录制状态。在录制完成或停止时,需要停止录制并释放资源。
注意,由于录制屏幕需要操作系统的权限,所以需要以管理员身份运行程序。
希望这个简单的录屏小软件示例能够帮到你。
阅读全文