简体   繁体   English

流无法读取整个图像

[英]Stream not reading entire image

I am sending an image from java pc application to android application through sockets. 我正在通过套接字将图像从java pc应用程序发送到android应用程序。 My image that I send does not full read into the android byte[] for some reason and it has varying results for how much of the image is decoded. 由于某些原因,我发送的图像未完全读入android byte [],并且对多少图像进行了解码,结果有所不同。 Here is my code 这是我的代码

Desktop: 桌面:

Socket socket = new Socket("127.0.0.1", 59900);
           // BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());

            OutputStream outputStream = socket.getOutputStream();

            Robot r = new Robot();
            Toolkit t = Toolkit.getDefaultToolkit();
            Rectangle rect = new Rectangle(t.getScreenSize());
            BufferedImage img = r.createScreenCapture(rect);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(img, "jpg", baos);

            byte[] size = ByteBuffer.allocate(4).putInt(baos.size()).array();
            outputStream.write(size);
            outputStream.write(baos.toByteArray());
            //outputStream.flush();

            System.out.println("Flushed: " + System.currentTimeMillis());

            //Thread.sleep(12000);
            System.out.println("Closing: " + System.currentTimeMillis());
            socket.close();

Android code Android代码

 ServerSocket server = new ServerSocket(59900);
        Socket socket = server.accept();

        InputStream inputStream = socket.getInputStream();

        System.out.println("Reading: " + System.currentTimeMillis());

        byte[] sizeAr = new byte[4];
        inputStream.read(sizeAr);
        int size = ByteBuffer.wrap(sizeAr).asIntBuffer().get();
        byte[] imageAr = new byte[size];
        inputStream.read(imageAr);
       // ByteArrayInputStream bis = new ByteArrayInputStream(imageAr);

        bp = BitmapFactory.decodeByteArray(imageAr, 0, size);

Here is a picture of what the result looks like 这是结果的图片 在此处输入图片说明

As you're closing the socket after sending the image, you don't need to send the count, and therefore you don't need the ByteBuffer , the ByteArrayOutputStream , or the ByteArrayInputStream . 发送图像后关闭套接字时,不需要发送计数,因此不需要ByteBufferByteArrayOutputStreamByteArrayInputStream Or the sleep. 还是睡觉。 Get rid of them all and write the image directly to the socket output stream, and read it directly from the socket input stream with ImageIO.read() . 摆脱它们,直接将图像写入套接字输出流,并使用ImageIO.read()从套接字输入流直接读取图像。

There is a common misconception that reading N bytes from a socket will return N bytes. 有一个常见的误解,即从套接字读取N个字节将返回N个字节。 A socket read call will return whenever it wants (usually when there is a time gap in the bytes received, which highly depends on the network). 套接字读取调用将需要时返回(通常是在收到的字节中存在时间间隔时,这在很大程度上取决于网络)。 N is just the maximum number of bytes. N只是最大字节数。

To solve this issue, you can either loop until you have the number of bytes required, or use specialized streams like DataOutputStream and DataInputStream , which offer the write() and readFully() method. 要解决此问题,您可以循环直到拥有所需的字节数,或者使用提供了write()readFully()方法的专用流(如DataOutputStreamDataInputStream readFully() They also provide the writeInt() and readInt() , that make sending the size easier. 它们还提供了writeInt()readInt() ,这使发送大小变得更加容易。

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

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