用JAVA捕获屏幕、屏幕录像、播放
http://blog.csdn.net/njchenyi/article/details/447554
版权声明:本文为博主原创文章,未经博主允许不得转载。
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;
import java.io.*;
private Dimension screenSize;
private Rectangle rectangle;
private Robot robot;
private long i = 0;
private JPEGImageEncoder encoder;
screenSize = Toolkit.getDefaultToolkit().getScreenSize();
rectangle = new Rectangle(screenSize);//可以指定捕获屏幕区域
try{
robot = new Robot();
}catch(Exception e){
e.printStackTrace();
System.out.println(e);
}
}
new WnetWScreenRecord().start();
}
FileOutputStream fos = null;
while (true){
try{
BufferedImage image = robot.createScreenCapture(rectangle);//捕获制定屏幕矩形区域
fos = new FileOutputStream("C://records//" + i + ".jpg");
JPEGCodec.createJPEGEncoder(fos).encode(image);//图像编码成JPEG
fos.close();
i = i + 1;
Thread.sleep(40);//每秒25帧
}catch(Exception e){
e.printStackTrace();
System.out.println(e);
try{
if (fos != null)fos.close();
}catch(Exception e1){}
}
}
}
}
//播放屏幕录像
package com.wnetw.project.media;
import java.awt.*;
import javax.swing.*;
import java.io.*;
public class WnetWScreenRecordPlayer extends JFrame{
BorderLayout borderLayout1 = new BorderLayout();
Dimension screenSize;
public WnetWScreenRecordPlayer() {
super();
screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setSize(screenSize);
Screen p = new Screen();
Container c = this.getContentPane();
c.setLayout(borderLayout1);
c.add(p,"Center");
new Thread(p).start();
this.show();
}
public static void main(String[] args){
new WnetWScreenRecordPlayer();
}
}
class Screen extends JPanel implements Runnable{
private BorderLayout borderLayout1 = new BorderLayout();
private Image cimage;
public void run(){
int i = 0;
while(true){
try{
cimage = loadImage(i + ".jpg");
i = i + 1;
repaint();
Thread.sleep(40);//与录像时每秒帧数一致
}catch(Exception e){
e.printStackTrace();
System.out.println(e);
}
}
}
public Image loadImage(String name) {
Toolkit tk = Toolkit.getDefaultToolkit();
Image image = null;
image = tk.getImage("C:/records/" + name);
MediaTracker mt = new MediaTracker(this);
mt.addImage(image, 0);
try {
mt.waitForID(0);
}catch (Exception e) {
e.printStackTrace();
System.out.println(e);
}
return image;
}
public Screen() {
super();
this.setLayout(null);
}
public void paint(Graphics g){
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(cimage, 0, 0, null);
}
}