Java ImageIO 非常慢 [英] Java ImageIO is insanely slow
问题描述
我正在制作一个程序,将客户端屏幕发送到服务器并显示它,但速度非常慢.一帧需要2-3秒,上传/下载速度不是问题.有什么我做错的地方/我可以改变什么来加快速度吗?
I'm making a program that sends the clients screen to the server and displays it but it's being extremely slow. It's taking 2-3 seconds for one frame and the upload/download speed is not a problem. Is there anything I'm doing wrong/anything I can change to speed this up?
服务器:
import java.awt.BorderLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.zip.GZIPInputStream;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class Server {
public static void main(String[] args) {
try {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JLabel label = new JLabel();
label.setSize(800, 600);
label.setVisible(true);
JScrollPane scroll = new JScrollPane();
scroll.getViewport().add(label);
panel.add(scroll, BorderLayout.CENTER);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.setSize(800, 600);
frame.setVisible(true);
ServerSocket serverSocket = new ServerSocket(25565);
Socket socket = serverSocket.accept();
GZIPInputStream in = new GZIPInputStream(socket.getInputStream());
BufferedImage image = null;
while(socket.isConnected()) {
image = ImageIO.read(in);
if(image != null) {
label.setIcon(new ImageIcon(image));
label.repaint();
}
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户:
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.io.IOException;
import java.net.Socket;
import java.util.zip.GZIPOutputStream;
import javax.imageio.ImageIO;
public class Client {
public static void main(String[] args) {
try {
Robot robot = new Robot();
Toolkit toolkit = Toolkit.getDefaultToolkit();
Rectangle screen = new Rectangle((int) toolkit.getScreenSize().getWidth(), (int) toolkit.getScreenSize().getHeight());
Socket socket = new Socket("127.0.0.1", 25565);
GZIPOutputStream out = new GZIPOutputStream(socket.getOutputStream());
while(socket.isConnected()) {
ImageIO.write(robot.createScreenCapture(screen), "png", out);
}
out.close();
} catch (AWTException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
推荐答案
如果您将图像作为 png
发送,则 GZIPStream 可能是个坏主意.您不会获得显着的压缩增益,因为 png 已经具有良好的压缩性.根据图像的类型,您还可以考虑使用 jpeg
来实现更好的压缩.
If you are sending the image as png
the GZIPStream is probable a bad idea. You won't achieve a signicant compression gain as png already has good compression. Depending on the kind of image you can also consider jpeg
to achieve better compression.
但可能最重要的是用 BufferedOuputStream 包装输出流:
But probably the most important is wrapping the output stream with a BufferedOuputStream:
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
您可以在输入流中执行相同的操作.
You can do the same in the input stream.
这篇关于Java ImageIO 非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!