Java 通过套接字发送ImageIcon并加载到JPanel
我可以发送和加载到JPanel已经和一切工作完美时,是在同一台计算机上。 当它在另一台计算机(LAN)上,屏幕捕获速度慢,每秒发送一次数据(imageIcon)时,问题就出现了。。。发送它确实需要很多时间。“鼠标/键盘”控件在本地主机/局域网上运行良好,几乎是实时的。唯一的问题是屏幕截图:/有人能帮我改进FPS吗 此文件是服务器处理程序,它将图像屏幕作为对象发送Java 通过套接字发送ImageIcon并加载到JPanel,java,swing,sockets,javax.imageio,event-dispatch-thread,Java,Swing,Sockets,Javax.imageio,Event Dispatch Thread,我可以发送和加载到JPanel已经和一切工作完美时,是在同一台计算机上。 当它在另一台计算机(LAN)上,屏幕捕获速度慢,每秒发送一次数据(imageIcon)时,问题就出现了。。。发送它确实需要很多时间。“鼠标/键盘”控件在本地主机/局域网上运行良好,几乎是实时的。唯一的问题是屏幕截图:/有人能帮我改进FPS吗 此文件是服务器处理程序,它将图像屏幕作为对象发送 Socket Client = null; Robot Robot = null; Rectangle Rectangle = nul
Socket Client = null;
Robot Robot = null;
Rectangle Rectangle = null;
boolean continueLoop = true;
String nom_pc;
public ServerHandler(Socket client, Robot robot, Rectangle rect,String n_pc) {
this.Client = client;
this.Robot = robot;
Rectangle = rect;
nom_pc = n_pc;
start();
}
@Override
public void run() {
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(Client.getOutputStream());
oos.writeObject(Rectangle);
} catch (IOException ex) {
}
while (continueLoop) {
BufferedImage image = Robot.createScreenCapture(Rectangle);
ImageIcon imageIcon = new ImageIcon(image);
try {
oos.writeObject(imageIcon);
oos.flush();
oos.reset();
System.out.println("Enviado Screen...");
} catch (IOException ex) {
try {
System.out.println("Se ha perdido conexion con el servidor remoto: "
+ Client.getInetAddress() + ":" + Client.getLocalPort());
continueLoop = false;
Client.close();
variables_saves.PCCN_ARRAYLIST.remove(Client);
variables_saves.setNombre_pc(nom_pc);
if (variables_saves.getNombre_pc().equals("ConflictoIP".toLowerCase())) {
variables_saves.PCCNNAME_ARRAYLIST.remove("Dirección IP Duplicada:" + Client.getInetAddress().getHostAddress());
} else if (variables_saves.getNombre_pc().equals("NoIP".toLowerCase())) {
variables_saves.PCCNNAME_ARRAYLIST.remove("Desconocido");
} else if (variables_saves.getNombre_pc().equals("Anonymous")) {
variables_saves.PCCNNAME_ARRAYLIST.remove("Anonymous");
} else {
variables_saves.PCCNNAME_ARRAYLIST.remove(variables_saves.getNombre_pc());
}
Home.Connected_pc_list.setListData(variables_saves.PCCNNAME_ARRAYLIST.toArray());
} catch (Exception e) {
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
}
}
这是客户端,它是一个“小程序”,它实际上接收屏幕并绘制成JPanel
import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
class ScreenReciever extends Thread {
private ObjectInputStream cObjectInputStream = null;
private JPanel cPanel = null;
private boolean continueLoop = true;
public ScreenReciever(ObjectInputStream ois,JPanel p){
cObjectInputStream = ois;
cPanel = p;
}
public void run(){
try {
//Read screenshots of the client then draw them
while(continueLoop){
//Recieve client screenshot and resize it to the current panel size
ImageIcon imageIcon = (ImageIcon) cObjectInputStream.readObject();
System.out.println("New image recieved");
Image image = imageIcon.getImage();
image = image.getScaledInstance(cPanel.getWidth(),cPanel.getHeight()
,Image.SCALE_FAST);
//Draw the recieved screenshot
Graphics graphics = cPanel.getGraphics();
graphics.drawImage(image, 0, 0, cPanel.getWidth(),cPanel.getHeight(),cPanel);
}
} catch (IOException ex) {
System.out.println("Connection with computer LOST");
try {
Thread.sleep(3000);
System.exit(0);
} catch (InterruptedException ex1) {
Logger.getLogger(ScreenReciever.class.getName()).log(Level.SEVERE, null, ex1);
}
} catch(ClassNotFoundException ex){
// ex.printStackTrace();
}
}
}
请帮助我改进代码,因为我不知道该做什么了,我必须在下周完成这个项目
有一件事。。。我以前确实使用过这种方法,而且没有问题,现在唯一的区别是服务器(是一个程序)和客户端是一个加载到浏览器中的小程序…为了更快地获得更好的帮助,请发布一个。另外,代码似乎试图发送
ImageIcon
,而不是Image
或BufferedImage
-我建议仅发送它们(甚至是byte[]
)。修改后,将JPG字节发送到套接字上:/但是仍然。。。同样的问题,即使图像从500kb压缩到76kb,我也会遇到同样的问题。。。它非常慢,每2到3秒刷新一次。你试着分析你的代码,看看时间花在哪里?如果您以前没有遇到过此问题,则问题可能出现在网络代码(或网络本身)中,但探查器会更准确地告诉您。发送每个图像大约需要半秒钟…:/0.500秒到0.700秒。。。很奇怪