文件名称: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()