在Java中通过数据报发送桌面流
我想捕获流桌面并通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不会为此使用数据报。如果出现任何网络错误、拥塞或接收器无法跟上,数据报将丢失,您的屏幕截图将被损坏。
最好使用常规(例如 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.
您可以通过 FileInputStream 读入刚刚编写的屏幕截图文件,也可以直接将图像写入 ByteArrayOutputStream:
然后您可以将数据拆分为多个数据包并通过 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:
Afterwards you can split the data into several packets and send them through a DatagramSocket (for one UDP package it will be too large).