在Java中通过数据报发送桌面流

发布于 2024-10-05 15:51:26 字数 1108 浏览 3 评论 0原文

我想捕获流桌面并通过 Java 中的数据报将其发送(到客户端)。下面的例子制作了一个屏幕截图。

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;
public class Captura{
    static public void captureScreen(String fileName) throws Exception {
        Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle screenRectangle = new Rectangle(screenSize);
        Robot robot = new Robot();
        BufferedImage image = robot.createScreenCapture(screenRectangle);
        ImageIO.write(image, "png", new File(fileName));
    }
//----
    public static void main(String[] args) {
        try{
            System.out.println("[ Captura iniciada ]");
            //sleep 5 sg
            Thread.currentThread().sleep(5*1000);
            String FILENAME="/home/jose/Desktop/captura01.png";
            Captura.captureScreen(FILENAME);
            System.out.println("[ Captura finalizada ]");
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

我也必须使用 Robot 类吗?我如何发送流?

谢谢你的帮助。

问候!

I want to capture the stream desktop and send it (to a client) via datagrams in Java. The following example makes a screenshot.

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;
public class Captura{
    static public void captureScreen(String fileName) throws Exception {
        Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle screenRectangle = new Rectangle(screenSize);
        Robot robot = new Robot();
        BufferedImage image = robot.createScreenCapture(screenRectangle);
        ImageIO.write(image, "png", new File(fileName));
    }
//----
    public static void main(String[] args) {
        try{
            System.out.println("[ Captura iniciada ]");
            //sleep 5 sg
            Thread.currentThread().sleep(5*1000);
            String FILENAME="/home/jose/Desktop/captura01.png";
            Captura.captureScreen(FILENAME);
            System.out.println("[ Captura finalizada ]");
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

Do I have to use the Robot class too?, How I can send the stream?

Thank's for help.

Regards!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仅此而已 2024-10-12 15:51:26

我不会为此使用数据报。如果出现任何网络错误、拥塞或接收器无法跟上,数据报将丢失,您的屏幕截图将被损坏。

最好使用常规(例如 TCP)套接字,并让传输层处理丢失的数据包和恢复。

I wouldn't use datagrams for this. If there are any network errors, congestion or the receiver cannot keep up, datagrams will be lost and your screenshots will be corrupted.

It is better to use a regular (e.g. TCP) socket, and let the transport layer deal with lost packets and recovery.

渔村楼浪 2024-10-12 15:51:26

您可以通过 FileInputStream 读入刚刚编写的屏幕截图文件,也可以直接将图像写入 ByteArrayOutputStream:

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    ImageIO.write(image, "png", buffer);
    byte[] data = buffer.toByteArray();

然后您可以将数据拆分为多个数据包并通过 DatagramSocket (对于一个 UDP 包来说,它会太大)。

You can read in the just written screen shot file via an FileInputStream or you could directly write the image into a ByteArrayOutputStream:

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    ImageIO.write(image, "png", buffer);
    byte[] data = buffer.toByteArray();

Afterwards you can split the data into several packets and send them through a DatagramSocket (for one UDP package it will be too large).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文