简体   繁体   English

通过套接字发送图像

[英]Sending an image over a socket

I am lately trying to create a program, just like teamviewer. 我最近正在尝试创建一个程序,就像teamviewer一样。 It's going kinda well, but I am currently facing a problem. 它有点好,但我目前面临一个问题。

I am trying to make my program send an image over the socket. 我试图让我的程序通过套接字发送图像。 When I run this code, it never outputs "Image should be sent!", so I think the problem is in the ImageIO.write line. 当我运行此代码时,它永远不会输出“应该发送图像!”,所以我认为问题出在ImageIO.write行中。

BufferedImage screencapture = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
try {
    ImageIO.write(screencapture, "jpg", socket.getOutputStream());
    System.out.println("Image should be sent!");
} catch (IOException ex){
    ex.printStackTrace();
} finally {
    if ( socket != null ){
        try { socket.close(); } catch (IOException ex){}
    }
    System.out.println("Image sent and socket closed!");
}

There is also a client on the other side, consuming the data being sent by the code above. 另一方面还有一个客户端,消耗上面代码发送的数据。 The code to do this is: 执行此操作的代码是:

BufferedImage image = ImageIO.read(socket.getInputStream());
JLabel label = new JLabel(new ImageIcon(image));
f.getContentPane().add(label);

Now my question is, what is wrong with this code, and how can I make this work? 现在我的问题是,这段代码有什么问题,我怎样才能使这项工作成功?

Actually the code looks ok. 实际上代码看起来还不错。 Here's a complete code, that works fine on my machine: 这是一个完整的代码,在我的机器上运行正常:

import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.*;

import javax.imageio.ImageIO;
import javax.swing.*;

public class Server {
  public static void main(String[] args) throws Exception {
    BufferedImage screencapture = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
    try (ServerSocket serv = new ServerSocket(25000)) {
      System.out.println("waiting...");
      try (Socket socket = serv.accept()) {
        System.out.println("client connected");
        ImageIO.write(screencapture, "jpg", socket.getOutputStream());
        System.out.println("sent");
      }
    }
  }
}

class Client {
  public static void main(String[] args) throws Exception {
    try(Socket socket = new Socket("localhost", 25000)){
      BufferedImage image = ImageIO.read(socket.getInputStream());
      JLabel label = new JLabel(new ImageIcon(image));
      JFrame f = new JFrame("vnc");
      f.getContentPane().add(label);
      f.pack();
      f.setVisible(true);
    }
  }
}

Though this will only work if you close the socket after sending the images. 虽然这只有在发送图像后关闭套接字才有效。 It will fail if you'll just try to send a few images over the same socket. 如果您只是尝试通过同一插槽发送一些图像,它将失败。 See https://stackoverflow.com/a/6973863/211205 . 请参阅https://stackoverflow.com/a/6973863/211205

I had worked on this earlier and posted the solution in my blog. 我早些时候曾在这方面工作,并在我的博客中发布了解决方案。 Please visit it for complete source code. 请访问它以获取完整的源代码。 Need your feed back too. 还需要你的反馈。

You need to read thread, socket and image writing to do this. 您需要读取线程,套接字和图像写入才能执行此操作。

http://javabelazy.blogspot.in/2013/10/sending-screenshot-from-client-to.html http://javabelazy.blogspot.in/2013/10/sending-screenshot-from-client-to.html

BufferedImage screenshot = robot.createScreenCapture(new Rectangle(dimensions));
ImageIO.write(screenshot,"png",serverSocket.getOutputStream());
ImageIO.write(img, "png", new File(fileName+".png"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM