每隔一定时间截屏回放:
截屏并保存图片:
package RecordAndRun;
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
/*
* 每隔一秒钟截一次屏,可以看到动态播放的效果,但想将BufferedImage bImage定义为数组则很快堆溢出
*/
public class Capture extends javax.swing.JFrame implements Runnable {
/** Creates new form Capture */
public Capture() {
initComponents();
try {
robot = new Robot();
} catch (AWTException ex) {
ex.printStackTrace();
}
dim = Toolkit.getDefaultToolkit().getScreenSize();
}
/**
* This method is called from within the constructor to
*
*
*
* initialize the form.
*
*
*
* WARNING: Do NOT modify this code. The content of this method is
*
*
*
* always regenerated by the Form Editor.
*/
// <editor-fold desc=" 生成的代码 " defaultstate="collapsed"></editor-fold>
private void initComponents() {
screenCanvas = new java.awt.Canvas();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
stop = true;
setResizable(false);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(screenCanvas, javax.swing.GroupLayout.PREFERRED_SIZE,
519, javax.swing.GroupLayout.PREFERRED_SIZE));
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(screenCanvas, javax.swing.GroupLayout.PREFERRED_SIZE,
434, javax.swing.GroupLayout.PREFERRED_SIZE)
);
setLocation(100, 100);
pack();
}//
/** * @param args the command line arguments */
public static void main(String args[]) {
final Capture capture = new Capture();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
capture.setVisible(true);
}
});
Thread cutThread = new Thread(capture);
cutThread.start();
}
public void run() {
stop = false;
while (!stop) {
BufferedImage bImage = robot.createScreenCapture(new Rectangle(
dim.width, dim.height));//这里是用来设定截屏的形状和位置,都是可以自己设定的
Graphics g = this.screenCanvas.getGraphics();
g.drawImage(bImage, 0, 0, this);
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
private synchronized void stop() {
stop = true;
}
// 变量声明 - 不进行修改
private java.awt.Canvas screenCanvas;
// 变量声明结束
private volatile boolean stop;
private Robot robot;
private Dimension dim;
}
截屏并保存图片:
package RecordAndRun;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
//抓屏并保存
/*******************************************************************
* 该JavaBean可以直接在其他Java应用程序中调用,实现屏幕的"拍照" This JavaBean is used to snapshot the
* GUI in a Java application! You can embeded it in to your java application
* source code, and us it to snapshot the right GUI of the application
*
* @see javax.ImageIO
* @author liluqun ([email]liluqun@263.net[/email])
* @version 1.0
*
*****************************************************/
public class GuiCamera {
private String fileName; // 文件的前缀
private String defaultName = "GuiCamera";
static int serialNum = 0;
private String imageFormat; // 图像文件的格式
private String defaultImageFormat = "png";
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
/****************************************************************
* 默认的文件前缀为GuiCamera,文件格式为PNG格式 The default construct will use the default
* Image file surname "GuiCamera", and default image format "png"
****************************************************************/
public GuiCamera() {
fileName = defaultName;
imageFormat = defaultImageFormat;
}
/****************************************************************
* @param s
* the surname of the snapshot file
* @param format
* the format of the image file, it can be "jpg" or "png"
* 本构造支持JPG和PNG文件的存储
****************************************************************/
public GuiCamera(String s, String format) {
fileName = s;
imageFormat = format;
}
/****************************************************************
* 对屏幕进行拍照 snapShot the Gui once
****************************************************************/
public void snapShot() {
try {
// 拷贝屏幕到一个BufferedImage对象screenshot
BufferedImage screenshot = (new Robot())
.createScreenCapture(new Rectangle(0, 0,
(int) d.getWidth(), (int) d.getHeight()));
serialNum++;
// 根据文件前缀变量和文件格式变量,自动生成文件名
String name = fileName + String.valueOf(serialNum) + "."
+ imageFormat;
File f = new File(name);
System.out.print("Save File " + name);
// 将screenshot对象写入图像文件
ImageIO.write(screenshot, imageFormat, f);
System.out.print("..Finished!\n");
} catch (Exception ex) {
System.out.println(ex);
}
}
public static void main(String[] args) {
GuiCamera cam = new GuiCamera("d:\\Hello", "png");//
cam.snapShot();
}
}