一段截屏的代码

  1. import java.awt.Dimension;
  2.  import java.awt.Rectangle;
  3.  import java.awt.Robot;
  4.  import java.awt.Toolkit;
  5.  import java.awt.image.BufferedImage;
  6.  import java.io.File; <p>import javax.imageio.ImageIO;
  7. /*******************************************************************
  8. * 该JavaBean可以直接在其他Java应用程序中调用,实现屏幕的"拍照"
  9. * This JavaBean is used to snapshot the GUI in a
  10. * Java application! You can embeded
  11. * it in to your java application source code, and us
  12. * it to snapshot the right GUI of the application
  13. * @see javax.ImageIO
  14. * @author liluqun (<a href='mailto:liluqun@263.net'>liluqun@263.net</a>)
  15. * @version 1.0
  16. *
  17. *****************************************************/
  18. public class GuiCamera
  19.  {
  20.  private String fileName; //文件的前缀
  21.  private String defaultName = "GuiCamera";
  22.  static int serialNum=0;
  23.  private String imageFormat; //图像文件的格式
  24.  private String defaultImageFormat="png";
  25.  Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  26. /****************************************************************
  27. * 默认的文件前缀为GuiCamera,文件格式为PNG格式
  28. * The default construct will use the default
  29. * Image file surname "GuiCamera",
  30. * and default image format "png"
  31. ****************************************************************/
  32.  public GuiCamera() {
  33.  fileName = defaultName;
  34.  imageFormat=defaultImageFormat;
  35.  }
  36. /****************************************************************
  37. * @param s the surname of the snapshot file
  38. * @param format the format of the image file,
  39. * it can be "jpg" or "png"
  40. * 本构造支持JPG和PNG文件的存储
  41. ****************************************************************/
  42.  public GuiCamera(String s,String format) {
  43.  fileName = s;
  44.  imageFormat=format;
  45.  }
  46.  /****************************************************************
  47. * 对屏幕进行拍照
  48. * snapShot the Gui once
  49. ****************************************************************/
  50.  public void snapShot() {
  51.  try {
  52.  //拷贝屏幕到一个BufferedImage对象screenshot
  53.  BufferedImage screenshot = (new Robot()).createScreenCapture(new
  54.  Rectangle(0, 0, (int) d.getWidth(), (int) d.getHeight()));
  55.  serialNum++;
  56.  //根据文件前缀变量和文件格式变量,自动生成文件名
  57.  String name=fileName+String.valueOf(serialNum)+"."+imageFormat;
  58.  File f = new File(name);
  59.  System.out.print("Save File "+name);
  60.  //将screenshot对象写入图像文件
  61.  ImageIO.write(screenshot, imageFormat, f);
  62.  System.out.print("..Finished!/n");
  63.  }
  64.  catch (Exception ex) {
  65.  System.out.println(ex);
  66.  }
  67.  }
  68. public static void main(String[] args)
  69.  {
  70.  GuiCamera cam= new GuiCamera("d://Hello", "png");//
  71. cam.snapShot();
  72.  }
  73.  }</p>
引用通告地址: http://tmsoft.lsxy.com/trackback.php?tbID=60&extra=2e16b1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值