隨筆-46  評論-64  文章-2  trackbacks-0
          今天稍微花了點時間實現了一個java的屏幕截圖程序,功能簡單,支持全屏截圖和選擇截圖

          用JSmooth做了個.exe程序,直接運行就可以了,

          附:

          程序下載 http://www.aygfsteel.com/Files/jht/MyScreenSnap.zip

          JRE1.4版本的可執(zhí)行程序 http://www.aygfsteel.com/Files/jht/MyScreenSnap_jre1.4.zip

          關鍵的有幾點
          一、做一個透明效果的窗體
          二、調用Robot類的捕獲屏幕方法

          代碼比較粗糙,暫時就這樣。

          代碼如下:
          package?cn.heapstack.MyScreenSnap;

          import?java.awt.AWTException;
          import?java.awt.Color;
          import?java.awt.Dimension;
          import?java.awt.DisplayMode;
          import?java.awt.FlowLayout;
          import?java.awt.GraphicsDevice;
          import?java.awt.GraphicsEnvironment;
          import?java.awt.Rectangle;
          import?java.awt.Robot;
          import?java.awt.event.ActionEvent;
          import?java.awt.event.ActionListener;
          import?java.awt.image.BufferedImage;
          import?java.io.File;
          import?java.io.IOException;

          import?javax.imageio.ImageIO;
          import?javax.swing.JButton;
          import?javax.swing.JFrame;
          import?javax.swing.JPanel;

          public?class?MenuFrame?{
          ????
          ????
          private?JFrame?frame;
          ????
          private?TranslucentFrame?tframe;
          ????
          private?JPanel?panel;
          ????
          private?JButton?button1;
          ????
          private?JButton?button2;
          ????
          private?JButton?button3;
          ????
          private?FormListener?formListener;
          ????
          private?Robot?robot;
          ????
          ????
          public?static?????GraphicsEnvironment?graphenv?=?GraphicsEnvironment.getLocalGraphicsEnvironment?();
          ????
          public?static?????GraphicsDevice?[]?screens?=?graphenv.getScreenDevices?();????
          ????
          public?static?????DisplayMode?mode?=?screens?[0].getDisplayMode?();????
          ????
          ????
          public?MenuFrame()
          ????
          {
          ????????initComponents();
          ????????
          ????}

          ????
          ????
          public?void?initComponents()
          ????
          {
          ????????frame?
          =?new?JFrame("MyScreenSnap?Version:1.0?Author:jacky.jihao@gmail.com");
          ????????
          ????????tframe?
          =?new?TranslucentFrame();
          ????????panel?
          =?new?JPanel();
          ????????
          ????????
          try?{
          ????????????
          ????????????robot?
          =?new?Robot();
          ????????}
          ?catch?(AWTException?e)?{
          ????????????e.printStackTrace();
          ????????}
          ?
          ????????
          ????????button1?
          =?new?JButton("Capture?Full?Screen");
          ????????button2?
          =?new?JButton("Capture?Selected?Screen");
          ????????button3?
          =?new?JButton("Exit");
          ????????formListener?
          =?new?FormListener();
          ????????button1.addActionListener(formListener);????
          ????????button2.addActionListener(formListener);????
          ????????button3.addActionListener(formListener);????
          ????????
          ????????
          //fullScreenFrame.setVisible(false);
          ????????tframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          ????????tframe.setUndecorated(
          true);
          ????????tframe.setAlwaysOnTop(
          true);
          ????????tframe.setSize(
          2000,2000);
          ????????
          ????????frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          ????????
          ????????frame.setLayout(
          null);
          ????????Dimension?size?
          =?new?Dimension(500,80);
          ????????frame.setPreferredSize(size?);
          ????????frame.setSize(size);
          ????????frame.setLocation(
          300,300);
          ????????frame.setBackground(Color.black);
          ????????
          ????????panel.setLocation(
          0,?0);
          ????????panel.setSize(size);
          ????????panel.setPreferredSize(size);
          ????????panel.setLayout(
          new?FlowLayout());
          ????????
          ????????panel.setOpaque(
          false);
          ????????
          ????????panel.add(button1);
          ????????panel.add(button2);
          ????????panel.add(button3);
          ????????
          ????????
          ????????frame.add(panel);
          ????????
          ????????frame.pack();
          ????????
          ????????frame.show();
          ????????
          ????}


          ????
          class?FormListener?implements?ActionListener
          ????
          {
          ????????
          ????????
          public?void?actionPerformed(ActionEvent?e)?{
          ????????????
          if(e.getSource()?==?button1)
          ????????????
          {
          ????????????????System.out.println(
          "Just?capture?the?screen?using?a?robot");
          ????????????????frame.hide();
          ????????????????Rectangle?bounds?
          =?new?Rectangle?(0,?0,?mode.getWidth?(),?mode.getHeight?());????
          ????????????????
          ????????????????BufferedImage?bimg?
          =?robot.createScreenCapture(bounds);
          ????????????????????????????????
          ????????????????String?fileName?
          =?String.valueOf(System.currentTimeMillis())+".png";
          ????????????????File?outputFile?
          =?new?File(fileName);
          ????????????????
          try?
          ????????????????
          {
          ????????????????????ImageIO.write(bimg,?
          "png",?outputFile);
          ????????????????}
          ?
          ????????????????
          catch?(IOException?e1)?
          ????????????????
          {
          ????????????????????e1.printStackTrace();
          ????????????????}


          ????????????????frame.show();
          ????????????????System.out.println(
          "File?save?as?"+fileName);
          ????????????????
          ????????????}

          ????????????
          else?if(e.getSource()?==?button2)
          ????????????
          {
          ????????????????System.out.println(
          "Create?a?full?screen?frame");
          ????????????????
          if(!tframe.isShowing())
          ????????????????
          {
          ????????????????????frame.hide();
          ????????????????????tframe.updateBackground();
          ????????????????????tframe.show();
          ????????????????????frame.show();
          ????????????????????frame.toBack();
          ????????????????}

          ????????????????
          else
          ????????????????????tframe.show();
          ????????????????
          ????????????}

          ????????????
          else?if(e.getSource()?==?button3)
          ????????????
          {
          ????????????????System.exit(
          0);
          ????????????}

          ????????????
          ????????}

          ????????
          ????}

          ????
          ????
          public?static?void?main(String[]?args)?{
          ????????
          new?MenuFrame();
          ????}


          }


          package?cn.heapstack.MyScreenSnap;

          import?java.awt.AWTException;
          import?java.awt.Color;
          import?java.awt.Dimension;
          import?java.awt.Graphics;
          import?java.awt.Point;
          import?java.awt.Rectangle;
          import?java.awt.Robot;
          import?java.awt.Toolkit;
          import?java.awt.event.ComponentEvent;
          import?java.awt.event.ComponentListener;
          import?java.awt.event.MouseEvent;
          import?java.awt.event.MouseListener;
          import?java.awt.event.MouseMotionListener;
          import?java.awt.event.WindowEvent;
          import?java.awt.event.WindowFocusListener;
          import?java.awt.image.BufferedImage;
          import?java.awt.image.ImageFilter;
          import?java.io.File;
          import?java.io.FileFilter;
          import?java.io.IOException;
          import?java.util.Iterator;

          import?javax.imageio.IIOImage;
          import?javax.imageio.ImageIO;
          import?javax.imageio.ImageWriteParam;
          import?javax.imageio.ImageWriter;
          import?javax.imageio.stream.ImageOutputStream;
          import?javax.swing.ImageIcon;
          import?javax.swing.JFileChooser;
          import?javax.swing.JFrame;
          import?javax.swing.JOptionPane;

          public?class?TranslucentFrame?extends?JFrame?implements?ComponentListener,
          ????????WindowFocusListener,?MouseListener,?MouseMotionListener?
          {

          ????
          private?boolean?flag_prepare?=?true;

          ????
          private?BufferedImage?background;

          ????
          private?Robot?robot;

          ????
          private?Dimension?size;

          ????
          private?Point?startPoint;

          ????
          private?Point?lastPoint;

          ????
          private?int?width?=?0;
          ????
          private?int?w?=?0;
          ????
          private?int?h?=?0;
          ????
          private?int?height?=?0;

          ????
          public?TranslucentFrame()?{
          ????????init();
          ????}


          ????
          private?void?init()?{
          ????????
          try?{
          ????????????robot?
          =?new?Robot();
          ????????????size?
          =?Toolkit.getDefaultToolkit().getScreenSize();
          ????????}
          ?catch?(AWTException?e)?{
          ????????????e.printStackTrace();
          ????????}

          ????????startPoint?
          =?new?Point();
          ????????lastPoint?
          =?new?Point();
          ????????
          this.addWindowFocusListener(this);
          ????????
          this.addComponentListener(this);
          ????????
          this.addMouseListener(this);
          ????????
          this.addMouseMotionListener(this);

          ????????
          this.updateBackground();
          ????}


          ????
          public?void?updateBackground()?{
          ????????background?
          =?robot.createScreenCapture(new?Rectangle(0,?0,?(int)?size
          ????????????????.getWidth(),?(
          int)?size.getHeight()));
          ????}


          ????
          public?void?refresh()?{

          ????????
          this.repaint();
          ????}


          ????
          public?void?repaint()?{
          ????????Graphics?g?
          =?this.getGraphics();

          ????????g.setColor(Color.red);
          ????????w?
          =?lastPoint.x?-?startPoint.x;
          ????????h?
          =?lastPoint.y?-?startPoint.y;
          ????????width?
          =?Math.abs(w);
          ????????height?
          =?Math.abs(h);

          ????????
          //?don't?need?to?clear?Rect?now,?just?paint?the?background,?it?feels
          ????????
          //?good
          ????????
          //?g.clearRect(startPoint.x,?startPoint.y,?width,?height);

          ????????
          this.paintComponents(g);

          ????????
          if?(!this.flag_prepare)?{

          ????????????
          if?(((w)?<?0)?&&?((h)?<?0))?{
          ????????????????g.drawRect(lastPoint.x,?lastPoint.y,?width,?height);

          ????????????}
          ?else?if?(((w)?>?0)?&&?((h)?<?0))?{
          ????????????????g.drawRect(startPoint.x,?lastPoint.y,?width,?height);

          ????????????}
          ?else?if?(((w)?<?0)?&&?((h)?>?0))?{
          ????????????????g.drawRect(lastPoint.x,?startPoint.y,?width,?height);

          ????????????}
          ?else?if?(((w)?>?0)?&&?((h)?>?0))?{
          ????????????????g.drawRect(startPoint.x,?startPoint.y,?width,?height);

          ????????????}

          ????????}
          ?else?{
          ????????????g.drawLine(
          0,?lastPoint.y,?size.width,?lastPoint.y);
          ????????????g.drawLine(lastPoint.x,?
          0,?lastPoint.x,?size.height);
          ????????}

          ????}


          ????
          public?void?paintComponents(Graphics?g)?{
          ????????Point?pos?
          =?this.getLocationOnScreen();
          ????????Point?offset?
          =?new?Point(-pos.x,?-pos.y);
          ????????g.drawImage(background,?offset.x,?offset.y,?
          null);
          ????}


          ????
          private?static?final?long?serialVersionUID?=?3690836343560995785L;

          ????
          public?static?void?main(String[]?args)?{
          ????????TranslucentFrame?frame?
          =?new?TranslucentFrame();
          ????????frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          ????????frame.setUndecorated(
          true);
          ????????frame.setAlwaysOnTop(
          true);
          ????????frame.setSize(
          2000,?2000);
          ????????frame.show();

          ????}


          ????
          public?void?componentHidden(ComponentEvent?e)?{
          ????}


          ????
          public?void?componentMoved(ComponentEvent?e)?{
          ????????
          this.refresh();
          ????}


          ????
          public?void?componentResized(ComponentEvent?e)?{
          ????}


          ????
          public?void?componentShown(ComponentEvent?e)?{
          ????????
          this.refresh();
          ????}


          ????
          public?void?windowGainedFocus(WindowEvent?e)?{
          ????????
          this.refresh();
          ????}


          ????
          public?void?windowLostFocus(WindowEvent?e)?{
          ????}


          ????
          public?void?mouseMoved(MouseEvent?e)?{
          ????????
          //?System.out.println(e.getX()?+?":"?+?e.getY());
          ????????this.lastPoint.x?=?e.getX();
          ????????
          this.lastPoint.y?=?e.getY();
          ????????repaint();
          ????}


          ????
          public?void?mousePressed(MouseEvent?e)?{
          ????????System.out.println(
          "Mouse?pressed?,?set?the?start?point,x:"?+?e.getX()
          ????????????????
          +?"?y:"?+?e.getY());
          ????????
          if?(e.getButton()?==?MouseEvent.BUTTON3)?{
          ????????????System.out.println(
          "Get?right?mouse");
          ????????????
          this.hide();
          ????????}
          ?else?{
          ????????????
          this.flag_prepare?=?false;
          ????????????
          this.startPoint.x?=?e.getX();
          ????????????
          this.startPoint.y?=?e.getY();
          ????????}

          ????}


          ????
          public?void?mouseReleased(MouseEvent?e)?{
          ????????System.out.println(
          "Mouse?released?,?set?the?last?point,x:"?+?e.getX()
          ????????????????
          +?"?y:"?+?e.getY());
          ????????
          this.flag_prepare?=?true;
          ????????
          //?save?the?picture
          ????????if?(e.getButton()?==?MouseEvent.BUTTON1)?{
          ????????????
          ????????????BufferedImage?bimg?
          =?null;
          ????????????
          if?(((w)?<?0)?&&?((h)?<?0))?{
          ????????????????
          //g.drawRect(lastPoint.x,?lastPoint.y,?width,?height);
          ????????????????bimg?=?robot.createScreenCapture(new?Rectangle(lastPoint.x+1,?lastPoint.y+1,?width-1,?height-1));

          ????????????}
          ?else?if?(((w)?>?0)?&&?((h)?<?0))?{
          ????????????????
          //g.drawRect(startPoint.x,?lastPoint.y,?width,?height);
          ????????????????bimg?=?robot.createScreenCapture(new?Rectangle(startPoint.x+1,?lastPoint.y+1,?width-1,?height-1));

          ????????????}
          ?else?if?(((w)?<?0)?&&?((h)?>?0))?{
          ????????????????
          //g.drawRect(lastPoint.x,?startPoint.y,?width,?height);
          ????????????????bimg?=?robot.createScreenCapture(new?Rectangle(lastPoint.x+1,?startPoint.y+1,?width-1,?height-1));

          ????????????}
          ?else?if?(((w)?>?0)?&&?((h)?>?0))?{
          ????????????????
          //g.drawRect(startPoint.x,?startPoint.y,?width,?height);
          ????????????????bimg?=?robot.createScreenCapture(new?Rectangle(startPoint.x+1,?startPoint.y+1,?width-1,?height-1));

          ????????????}

          ????????????
          ????????????
          ????????????JFileChooser?fcSave?
          =?new?JFileChooser();
          ????????????fcSave.setCurrentDirectory(
          new?File(System.getProperty("user.dir")));
          ????????????fcSave.setSelectedFile(
          null);
          ????????????
          if?(fcSave.showSaveDialog(this)?!=?JFileChooser.APPROVE_OPTION)
          ????????????????
          return;

          ????????????File?file?
          =?fcSave.getSelectedFile();
          ????????????String?path?
          =?file.getAbsolutePath().toLowerCase();
          ????????????
          if?(!path.endsWith(".png"))
          ????????????????file?
          =?new?File(path?+=?".png");
          ????????????
          ????????????
          ????????????
          try?{
          ????????????????ImageIO.write(bimg,?
          "png",?file);
          ????????????}
          ?catch?(IOException?e1)?{
          ????????????????e1.printStackTrace();
          ????????????}

          ????????}


          ????????
          if?(this.isShowing())
          ????????????repaint();
          ????}


          ????
          public?void?mouseClicked(MouseEvent?e)?{

          ????}


          ????
          public?void?mouseEntered(MouseEvent?e)?{

          ????}


          ????
          public?void?mouseExited(MouseEvent?e)?{

          ????}


          ????
          public?void?mouseDragged(MouseEvent?e)?{
          ????????
          this.lastPoint.x?=?e.getX();
          ????????
          this.lastPoint.y?=?e.getY();
          ????????repaint();
          ????}


          }


          posted on 2007-03-29 19:19 jht 閱讀(1690) 評論(5)  編輯  收藏 所屬分類: J2SESwing Tips

          評論:
          # re: MyScreenSnap 一個簡單的截圖程序 2007-04-01 09:24 | zhenting
          對jdk版本有沒有什么要求?
          我的是1.4  回復  更多評論
            
          # re: MyScreenSnap 一個簡單的截圖程序 2007-09-13 15:09 | jht
          稍微修改了一下,做了個支持jre1.4的  回復  更多評論
            
          # re: MyScreenSnap 一個簡單的截圖程序 2007-09-13 15:37 | 千里冰封
          呵呵,支持一下,不錯:)  回復  更多評論
            
          # re: MyScreenSnap 一個簡單的截圖程序 2009-11-30 09:58 | 消毒粉
          能否給加點注釋,看的好看點!  回復  更多評論
            
          # re: MyScreenSnap 一個簡單的截圖程序 2011-09-09 08:14 | tb
          有注釋么   回復  更多評論
            
          主站蜘蛛池模板: 灵宝市| 清水河县| 武功县| 茌平县| 宁明县| 蒙城县| 万安县| 田东县| 南岸区| 阿巴嘎旗| 基隆市| 南部县| 松滋市| 潍坊市| 鹿泉市| 扶沟县| 白河县| 厦门市| 类乌齐县| 栾川县| 班戈县| 明水县| 施甸县| 台东市| 盘锦市| 永泰县| 赣州市| 屏山县| 大竹县| 左贡县| 天水市| 如东县| 稻城县| 彭山县| 昌宁县| 洱源县| 海南省| 广饶县| 普格县| 金湖县| 秀山|