java鼠标轨迹_java屏幕录像(包含鼠标轨迹)生成avi媒体文件
时间: 2023-10-19 07:05:09 浏览: 166
要生成一个包含鼠标轨迹的Java屏幕录像并转换为AVI媒体文件,你可以使用Java AWT Robot类来捕获屏幕截图和鼠标事件,并使用Xuggle库将屏幕截图和鼠标事件合成为AVI文件。
以下是一个简单的示例代码,它使用Java AWT Robot类捕获屏幕截图和鼠标事件,并将它们作为帧添加到Xuggle的AVI编码器中:
```java
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import org.apache.commons.io.FileUtils;
import com.xuggle.mediatool.*;
import com.xuggle.mediatool.ToolFactory;
import com.xuggle.xuggler.ICodec;
import com.xuggle.xuggler.IContainer;
import com.xuggle.xuggler.IPixelFormat;
import com.xuggle.xuggler.IStream;
import com.xuggle.xuggler.IStreamCoder;
import com.xuggle.xuggler.IVideoPicture;
public class ScreenRecorder implements ActionListener {
private static final int FRAME_RATE = 30;
private static final int SCREEN_WIDTH = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
private static final int SCREEN_HEIGHT = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
private static final String OUTPUT_FILE = "output.avi";
private Robot robot;
private BufferedImage cursorImage;
private Point lastMousePoint = null;
private long startTime = 0;
private MediaWriter mediaWriter;
private Timer timer;
public ScreenRecorder() throws AWTException {
this.robot = new Robot();
this.cursorImage = createCursorImage();
}
public void start() {
try {
this.mediaWriter = ToolFactory.makeWriter(OUTPUT_FILE);
IContainer container = IContainer.make();
int codecId = ICodec.ID.CODEC_ID_MPEG4;
IStream stream = container.addNewStream(codecId);
IStreamCoder coder = stream.getStreamCoder();
coder.setWidth(SCREEN_WIDTH);
coder.setHeight(SCREEN_HEIGHT);
coder.setPixelType(IPixelFormat.Type.YUV420P);
coder.setFlag(IStreamCoder.Flags.FLAG_QSCALE, true);
coder.setGlobalQuality(0);
coder.setFrameRate(FRAME_RATE);
coder.setTimeBase(IRational.make(FRAME_RATE, 1));
coder.setCodec(ID.CODEC_ID_MPEG4);
coder.setBitRate(800000);
coder.setBitRateTolerance(0);
container.writeHeader();
this.startTime = System.nanoTime();
this.timer = new Timer(1000 / FRAME_RATE, this);
this.timer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
public void stop() {
if (this.timer != null) {
this.timer.stop();
}
if (this.mediaWriter != null) {
this.mediaWriter.close();
}
}
private BufferedImage createCursorImage() {
BufferedImage image = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setColor(Color.BLACK);
g.drawLine(0, 0, 15, 15);
g.drawLine(0, 15, 15, 0);
g.dispose();
return image;
}
public void actionPerformed(ActionEvent e) {
long now = System.nanoTime();
long timestamp = (now - this.startTime) / 1000000;
BufferedImage screenImage = this.robot.createScreenCapture(new Rectangle(SCREEN_WIDTH, SCREEN_HEIGHT));
Graphics2D graphics = screenImage.createGraphics();
Point mousePoint = MouseInfo.getPointerInfo().getLocation();
if (this.lastMousePoint != null && !mousePoint.equals(this.lastMousePoint)) {
graphics.drawImage(this.cursorImage, mousePoint.x, mousePoint.y, null);
}
this.lastMousePoint = mousePoint;
graphics.dispose();
try {
BufferedImage frameImage = new BufferedImage(screenImage.getWidth(), screenImage.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
frameImage.getGraphics().drawImage(screenImage, 0, 0, null);
IVideoPicture picture = IVideoPicture.make(frameImage.getColorModel(), frameImage.getWidth(), frameImage.getHeight());
picture.setTimeStamp(timestamp);
picture.setImage(frameImage);
this.mediaWriter.encodeVideo(0, picture);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
ScreenRecorder recorder = new ScreenRecorder();
recorder.start();
Thread.sleep(60000);
recorder.stop();
}
}
```
这个示例代码做了以下几件事情:
1. 初始化一个Robot对象来捕获屏幕截图和鼠标事件。
2. 创建一个AVI编码器,并设置编码器参数,如分辨率、帧率、像素格式等。
3. 使用Timer定期捕获屏幕截图和鼠标事件,并将它们作为帧添加到AVI编码器中。
4. 最后,将所有帧合成为AVI文件。
你可以使用Xuggle库中的其他类和方法来进一步自定义编码器参数和帧处理。注意,由于AVI编码器需要大量计算和存储资源,因此在录制长时间的视频时,你可能需要考虑使用更高效的编码格式或压缩算法来减小文件大小。
阅读全文