Java实现远程屏幕监视

时间:2014-08-07 05:40:09
【文件属性】:

文件名称:Java实现远程屏幕监视

文件大小:3KB

文件格式:ZIP

更新时间:2014-08-07 05:40:09

Java 实现 远程 屏幕 监视

package cn.com.origin.net; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class Server { public Server() { } public void listen() throws IOException { ServerSocket server = new ServerSocket(8002); while (true) { try { Socket client = server.accept(); if (client != null) { ServerProcessor processor = new ServerProcessor(client); processor.start(); } } catch (Throwable e) { e.printStackTrace(); } } } public static void main(String[] args) throws IOException { Server s = new Server(); s.listen(); } } package cn.com.origin.net; import java.awt.AWTException; import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.Socket; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import com.sun.image.codec.jpeg.ImageFormatException; import com.sun.image.codec.jpeg.JPEGCodec; public class ServerProcessor extends Thread { // private Socket client; public static final int SEND_IMAGE_SYMBOL = 0x00000000; // private InputStream is; private DataOutputStream dos; private Robot robot; private Rectangle rect; private BufferedImage bm; public ServerProcessor(Socket client) throws IOException, AWTException { // this.client = client; // is = client.getInputStream(); dos = new DataOutputStream(client.getOutputStream()); // bm = ImageIO.read(new File("E:\\Caokai\\001.png")); robot = new Robot(); rect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); } public void run() { while (true) { try { bm = robot.createScreenCapture(rect); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ZipOutputStream zis = new ZipOutputStream(bos); zis.setLevel(9); zis.putNextEntry(new ZipEntry("ScreenCapture.jpg")); JPEGCodec.createJPEGEncoder(zis).encode(bm); // ImageIO.write(bm, "jpg", zis); zis.closeEntry(); bos.flush(); byte[] data = bos.toByteArray(); dos.write(SEND_IMAGE_SYMBOL); // System.out.println("Server - data.length = " + data.length); dos.writeInt(data.length); dos.write(data); dos.flush(); // 每秒2帧 Thread.sleep(500); } catch (ImageFormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); break; } catch (InterruptedException e) { e.printStackTrace(); } } } }


【文件预览】:
src
----cn()
--------com()

网友评论

  • 非常好的内容非常有用,有价值,挺好玩的
  • 代码有很大参考价值。这个代码的原理主要是通过截取客户端当前频幕,以一定的频率向服务端发送,连续的图像组成实时画面。
  • 只能用作参考。
  • 本来就是网上的例子。不能嵌入jsp
  • 顺便说一下,import com.sun.image.codec.jpeg.JPEGCodec;以及相关的该类的地方,eclipse有可能会报错,只要把Windows-Preferences-Java-Complicer-Errors/Warnings里面的Deprecated and restricted API中的Forbidden references(access rules)选为Warning就可以编译通过。原因就是eclipse默认把受限访问的包设置为编译错误。
  • 代码给我了一定的提示,我现在顺利完成任务了,感谢楼主