Простой сервер для шаринга экрана
- import java.awt.{Rectangle, Robot, Toolkit}
- import java.io.{ByteArrayOutputStream, PrintWriter}
- import java.net.{InetAddress, ServerSocket, Socket}
- import java.util.Base64
- import javax.imageio.ImageIO
- object DesktopSharer {
- val PORT = 7119
- val robot = new Robot()
- def main(args: Array[String]) {
- val serverSocket = new ServerSocket(PORT)
- println("Server started at " + InetAddress.getLocalHost.getHostAddress + ":" + PORT)
- while (true) {
- val socket = serverSocket.accept
- new Sharer(socket).start()
- }
- serverSocket.close
- }
- class Sharer(val socket: Socket) extends Thread {
- override def run() {
- val rectangle = new Rectangle(Toolkit.getDefaultToolkit.getScreenSize)
- val img = robot.createScreenCapture(rectangle)
- val imgStream = new ByteArrayOutputStream()
- ImageIO.write(img, "jpg", imgStream)
- imgStream.flush
- val html = toHtml(imgStream.toByteArray)
- imgStream.close
- println("Send " + html.length + " bytes to " + socket.getRemoteSocketAddress)
- val pw = new PrintWriter(socket.getOutputStream, true)
- pw.println("HTTP/1.1 200 OK")
- pw.println("Content-Type: text/html")
- pw.println("Cache-Control,no-store,no-cache,must-revalidate")
- pw.println("Content-Length: " + html.length)
- pw.println
- pw.println(html)
- pw.flush
- pw.close
- socket.close
- }
- private def toHtml(bytes: Array[Byte]) : String = {
- val src = Base64.getEncoder.encodeToString(bytes)
- return s"""
- |<html>
- |<body>
- | <img src="data:image/jpeg;base64,$src" />
- |</body>
- |</html>
- """.stripMargin
- }
- }
- }
Запускает сервер, который отправляет изображение экрана клиентам-браузерам.
- scala DesktopSharer.scala
Аналог на Java 14+
- import java.awt.Rectangle;
- import java.awt.Robot;
- import java.awt.Toolkit;
- import java.io.IOException;
- import java.io.ByteArrayOutputStream;
- import java.io.PrintWriter;
- import java.net.InetAddress;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.Base64;
- import javax.imageio.ImageIO;
- public class DesktopSharer {
- private static final int PORT = 7119;
- public static void main(String[] args) throws IOException {
- final var serverSocket = new ServerSocket(PORT);
- var host = InetAddress.getLocalHost().getHostAddress();
- host = "http://192.168.1.139";
- System.out.println("Server started at " + host + ":" + PORT);
- while (true) {
- var socket = serverSocket.accept();
- new Thread(() -> {
- try {
- share(socket);
- } catch(Exception ioe) {}
- }).start();
- }
- // serverSocket.close();
- }
- private static void share(Socket socket) throws Exception {
- final var robot = new Robot();
- final var rectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
- final var img = robot.createScreenCapture(rectangle);
- final var imgStream = new ByteArrayOutputStream();
- ImageIO.write(img, "jpg", imgStream);
- imgStream.flush();
- final var html = toHtml(imgStream.toByteArray());
- imgStream.close();
- System.out.println("Send " + html.length() + " bytes to " + socket.getRemoteSocketAddress());
- final var pw = new PrintWriter(socket.getOutputStream(), true);
- pw.println("HTTP/1.1 200 OK");
- pw.println("Content-Type: text/html");
- pw.println("Cache-Control,no-store,no-cache,must-revalidate");
- pw.println("Content-Length: " + html.length());
- pw.println();
- pw.println(html);
- pw.flush();
- pw.close();
- socket.close();
- }
- private static String toHtml(byte[] bytes) {
- final var src = Base64.getEncoder().encodeToString(bytes);
- return """
- <html>
- <body>
- <img src="data:image/jpeg;base64,$src" />
- </body>
- </html>
- """.stripIndent().replace("$src", src);
- }
- }