SWT 獲取屏幕圖像

          Posted on 2009-08-09 23:40 songk 閱讀(385) 評論(0)  編輯  收藏
             以下方式可獲取(利用GC類的copyArea方法):

          Image screenImage = new Image(display, display.getBounds());
          new GC(display).copyArea(screenImage, 00);

              以下一個小Demo,運行后可將桌面圖像存入當前目錄:
           1 import java.io.ByteArrayOutputStream;
           2 import java.io.File;
           3 import java.io.FileOutputStream;
           4 import java.io.IOException;
           5 
           6 import org.eclipse.swt.SWT;
           7 import org.eclipse.swt.graphics.GC;
           8 import org.eclipse.swt.graphics.Image;
           9 import org.eclipse.swt.graphics.ImageData;
          10 import org.eclipse.swt.graphics.ImageLoader;
          11 import org.eclipse.swt.widgets.Display;
          12 
          13 public class ScreenCamera {
          14 
          15     Image image;
          16 
          17     public ScreenCamera(Display display) {
          18         image = new Image(display, display.getBounds());
          19     }
          20 
          21     public void work() throws IOException {
          22         new GC(image.getDevice()).copyArea(image, 00);
          23         save();
          24     }
          25 
          26     /*
          27      * 保存文件
          28      */
          29     void save() throws IOException {
          30         File target = new File("screen.png");
          31         FileOutputStream out = new FileOutputStream(target);
          32         out.write(turnPNGData(image.getImageData()));
          33         out.flush();
          34         out.close();
          35         System.out.println("屏幕圖片位置:" + target.getAbsolutePath());
          36     }
          37 
          38     /*
          39      * 工具方法:將圖片數據轉化為png格式的文件數據
          40      */
          41     byte[] turnPNGData(ImageData imageData) {
          42         ImageLoader imageLoader = new ImageLoader();
          43         imageLoader.data = new ImageData[] { imageData };
          44         ByteArrayOutputStream stream = new ByteArrayOutputStream();
          45         imageLoader.save(stream, SWT.IMAGE_PNG);
          46         return stream.toByteArray();
          47     }
          48 
          49     public static void main(String[] args) throws IOException {
          50         new ScreenCamera(new Display()).work();
          51     }
          52 }
          53 



          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          posts - 1, comments - 0, trackbacks - 0, articles - 1

          Copyright © songk

          主站蜘蛛池模板: 万宁市| 阿坝县| 渭源县| 石棉县| 化隆| 右玉县| 宝丰县| 临高县| 宁津县| 沈阳市| 河池市| 吉隆县| 建平县| 甘孜| 宣城市| 虹口区| 罗平县| 汉寿县| 镇平县| 漳平市| 武穴市| 吕梁市| 孟连| 嘉兴市| 四平市| 普定县| 晋城| 鸡西市| 厦门市| 安陆市| 康保县| 灵石县| 嵩明县| 淮安市| 南华县| 郎溪县| 北碚区| 平南县| 巴林右旗| 什邡市| 兰州市|