我通过 Java 中的套接字发送图像,当客户端和服务器都在一台计算机上时一切都很好,但是当我通过 LAN 运行它时,生成的图像是黑色的。我使用 Robot 类拍摄屏幕图像,然后使用 AffineTransform 将其缩小。客户端代码:
BufferedImage s = null;
socket = new Socket("AJ-PC", 4444);
out = new ObjectOutputStream(socket.getOutputStream());
Robot r = new Robot();
while(true) {
s = r.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
double scale = 0.75;
int w = (int) (s.getWidth() * scale);
int h = (int) (s.getHeight() * scale);
BufferedImage outImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
AffineTransform trans = new AffineTransform();
trans.scale(scale, scale);
Graphics2D g = outImage.createGraphics();
g.drawImage(s, trans, null);
g.dispose();
Thread.sleep(400);
out.writeObject(new SerializableImage(outImage));
}
服务器代码:
server = new ServerSocket(4444);
socket = server.accept();
System.out.println(socket);
in = new ObjectInputStream(socket.getInputStream());
while(running) {
bi = (SerializableImage) in.readObject();
b = true;
System.out.println("new");
panel.repaint();
}