码迷,mamicode.com
首页 > 编程语言 > 详细

用JAVA捕获屏幕、屏幕录像、播放

时间:2016-04-23 07:29:53      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:

 

http://blog.csdn.net/njchenyi/article/details/447554

 

用JAVA捕获屏幕、屏幕录像、播放

标签: javaexceptionimageimportnullstring
技术分享 分类:
 
//记录屏幕、类似录像
package com.wnetw.project.media;
 
import java.awt.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;
import java.io.*;

 
public class WnetWScreenRecorder extends Thread{

  private Dimension screenSize;
  private Rectangle rectangle;
  private Robot robot;
  private long i = 0;
  private JPEGImageEncoder encoder;
 
  public WnetWScreenRecord() {
    screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    rectangle = new Rectangle(screenSize);//可以指定捕获屏幕区域
    try{
      robot = new Robot();
    }catch(Exception e){
      e.printStackTrace();
      System.out.println(e);
    }
  }
 
  public static void main(String[] args) {
    new WnetWScreenRecord().start();
  }
 
  public void run(){
    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);
  }
}

 
 

用JAVA捕获屏幕、屏幕录像、播放

标签:

原文地址:http://www.cnblogs.com/donaldlee2008/p/5423670.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!