//捕获当前屏幕并向Server网络传输.
import java.awt.image.*;
import javax.swing.event.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.net.*;
import simage.*;
import com.sun.image.codec.jpeg.*;
import java.util.zip.*;
import java.net.Socket;
public class Capture extends JFrame {
   public static Image img1= null;
   int x=0,y=0;
   Dimension screenDims =Toolkit.getDefaultToolkit().getScreenSize();
   Robot r = null;
   JButton but;    GZIPSocket sst1;
   public Capture()
   {
       getContentPane().setLayout(new BorderLayout());
       try { r = new Robot(); }
       catch(Exception e) {}       but = new JButton("Capture");
       but.setBounds(10,10,100,25);
       try {
     jbInit();
   }
   catch(Exception e) {
     e.printStackTrace();
    }
       but.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
frequency fy=new frequency();
fy.frequency1(sst1);
Thread threadfy=new Thread(fy);
threadfy.start();
           }
       } );//but event is end;      getContentPane().add(but,BorderLayout.NORTH);
   }  /* public void paint(Graphics g) {
       super.paint(g);
       if (img != null) {           //g.drawImage(img,0,0,screenDims.width,screenDims.height,this);           Icon icon=new ImageIcon(img);
           jlabel.setIcon(icon);
       }
   }*/   public static void main(String argv[]) {
       Capture frame = new Capture();
       frame.addWindowListener(new WindowAdapter(){
           public void windowClosing(WindowEvent e) {
               System.exit(0);
           }
       } );
       frame.setBounds(0,0,400,400);
       frame.setVisible(true);
   }
  private void jbInit() throws Exception {
    this.addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        this_windowClosing(e);
      }
    });
  }  void this_windowClosing(WindowEvent e) {
System.exit(0);
  }
}
class frequency extends Capture implements Runnable{
  private JScrollPane jScrollPane1 = new JScrollPane();
   private JLabel jlabel = new JLabel();
  public void frequency1(GZIPSocket st){
    sst1=st;  } /* public void paint(Graphics g) {
       super.paint(g);
       if (img1!= null) {       g.drawImage(img1,0,0,screenDims.width,screenDims.height,this);         //  Icon icon=new ImageIcon(img1);
          // jlabel.setIcon(icon);
       }
   }*/
  public void run(){
    ObjectOutputStream out=null;
    BufferedImage img1=null;
    MyImage myimage=null;  int i=0;
  int j=0;
  int m=1;
 /* try{sst1=new GZIPSocket("127.0.0.1",8080);
  }
  catch(Exception e){}*/
  while(true){
      img1=r.createScreenCapture(new Rectangle(0,0,screenDims.width,screenDims.height));
m++;//r.delay(10);
//      SaveImage si=new SaveImage(img1,j);
 System.out.println(m);
j++;
//this.repaint();try{  sst1=new GZIPSocket("202.203.215.117",8080);
    //System.out .println(" connect  to server 202.203.215.117  on 8080...1");
   GZIPOutputStream gos=(GZIPOutputStream)sst1.getOutputStream();   out=new ObjectOutputStream(gos);
   myimage=new MyImage((Image)img1);
  //System.out .println("开始向网络中写第 "+(++i)+" 个Image 对象...") ;
      // System.out .println("要写的Image 宽="+myimage.getImageSize().width+"  高="+myimage.getImageSize().height  ) ;
   out.writeObject(myimage);
     out.close();   try{
     Thread.currentThread().sleep(1);
    }catch(Exception e){}     }  // catch(IOException e2){}
       catch(Exception e3){}
  }}}class GZIPSocket extends Socket {        private InputStream in;
        private OutputStream out;        public GZIPSocket() {
         super();
        }        public GZIPSocket(String host, int port) throws IOException {
                super(host, port);           }        public InputStream getInputStream() throws IOException {
                 if (in == null) {
                        in = new GZIPInputStream(super.getInputStream());
                }
                return in;           }         public OutputStream getOutputStream() throws IOException {
                if (out == null) {
                        out = new GZIPOutputStream(super.getOutputStream());                   }
                return out;
         }         public synchronized void close() throws IOException {               GZIPOutputStream o = (GZIPOutputStream)getOutputStream();               o.flush();               super.close();
        }
}

解决方案 »

  1.   

    import simage.*;
    出错,没有这个包
    核心代码是什么?
      

  2.   

    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.File;public class ScreenCapture {
        public ScreenCapture() {
            try {
                Toolkit tk = Toolkit.getDefaultToolkit();
                tk.sync();
                //创建屏幕高与宽区域
                Rectangle ecran = new Rectangle(tk.getScreenSize());
                Robot robot = new Robot();
                robot.setAutoDelay(0);
                robot.setAutoWaitForIdle(false);
                BufferedImage image = robot.createScreenCapture(ecran);
                File file = new File("ScreenCapture.jpg");
                javax.imageio.ImageIO.write(image, "jpg", file);
            }
            catch (Exception e) {
                e.printStackTrace();
            }    }    public static void main(String[] args) {
            ScreenCapture screenCapture1 = new ScreenCapture();
        }}
      

  3.   

    Java的截屏程序http://bbs.tongji.net/index.php?prog=topic::flat&tid=88775&go=newer
      

  4.   

    GJA106(中文字符) :代码除以下错误外能正常运行
     javax.imageio.ImageIO.write(image, "jpg", file);//image前应加入类型转换(RenderedImage)