java的imageio,Java ImageIO非常慢

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?

Server:

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();

}

}

}

Client:

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();

}

}

}

解决方案

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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值