简体   繁体   English

Java字节数组和带有套接字的图像

[英]Java Byte Array And Image With Socket

My program takes a picture of the users screen, when they are using my program, to make a screenshot, then sends it to a server. 当用户使用我的程序时,我的程序会为用户屏幕拍照,以制作屏幕截图,然后将其发送到服务器。 The image will load about 1/4 of the way and freeze. 图像将加载大约1/4的路径并冻结。

Sending the screenshot: 发送屏幕截图:

            BufferedImage buffimg = robot.createScreenCapture(captureSize);
            BufferedImage image = buffimg;
            byte[] imageInByte;


            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(image, "png", baos);
            baos.flush();
            imageInByte = baos.toByteArray();
            baos.close();

            DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
            PrintWriter out = new PrintWriter(socket.getOutputStream());

            out.println("!SCREENDATA!");
            out.flush();
            dos.writeInt(baos.toByteArray().length);
            dos.write(baos.toByteArray());
            dos.flush();

Getting the image: 获取图像:

                    if (input.startsWith("!SCREENDATA!")) {
                        System.out.println("reading");

                        DataInputStream dis = new DataInputStream(socket.getInputStream());
                        int len = dis.readInt();
                        System.out.println(len);
                        byte[] data = new byte[len];

                        dis.read(data);
                        InputStream in = new ByteArrayInputStream(data);
                        Image image = Toolkit.getDefaultToolkit().createImage(data);

                        v.repaint(image);
                    }

Displaying the image: 显示图像:

public void repaint(Image img) {

    frame.getContentPane().add(new JLabel(new ImageIcon(img)));
    frame.repaint();
    frame.pack();

}

If anyone could help me with this, I would appreciate it a lot! 如果有人可以帮助我,我将不胜感激!

You need to keep calling dis.read(data); 您需要继续调用dis.read(data); as this method on a TCP socket isn't designed to offer the entire buffer in one call (it's a stream socket, it keeps going). 因为TCP套接字上的此方法并非旨在在一次调用中提供整个缓冲区(它是流套接字,所以会一直运行)。 But be aware that the method call will block when the socket has no data to give. 但是请注意,当套接字没有要提供的数据时,方法调用将阻塞。 Also, it's best to send the file size in advance of the file so the other end knows how much to expect - it is less prone to protocol errors that can cause an infinite loop. 另外,最好在发送文件之前先发送文件大小,以便另一端知道要发送的文件大小-不太容易出现协议错误,而该错误可能导致无限循环。

Have a look at the answers at how to achieve transfer file between client and server using java socket . 看看如何使用java socket在客户端和服务器之间实现文件传输的答案。

Anyway, an analogy: the socket has a bucket which fills up which is 512kb, let's say. 无论如何,比喻:套接字有一个可以填充的桶,可以说是512kb。 You have a bucket that is 2048kb. 您有一个2048kb的存储桶。 You have to keep pouring the socket bucket into your own bucket. 您必须继续将套接字桶倒入自己的桶中。

Also, don't do stuff with Swing unless its on the Event Dispatch Thread. 另外,不要在事件调度线程上使用Swing进行处理。 See How do you use the Event Dispatch Thread? 请参阅如何使用事件调度线程? for more details. 更多细节。

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

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