【问题标题】:How to send a bitmap to a java socket server?如何将位图发送到 Java 套接字服务器?
【发布时间】:2015-06-03 10:04:16
【问题描述】:

我正在将图像从 android 客户端发送到 java 服务器套接字。目前我正在使用以下代码 -

客户代码

try
            {
                if (bmp != null & socket!=null)
                {
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bmp.compress(CompressFormat.JPEG, 100,stream);
                    byte[] byteArray = stream.toByteArray();
                    InputStream inn = new ByteArrayInputStream(byteArray);
                    dos.writeInt(byteArray.length);
                    int len = 0 ;
                    byte [] b = new byte [1024];
                    while ((len = inn.read(b)) != -1)
                    {
                        dos.write(b,0,len);
                    }
                    dos.flush();
                    stream.close();
                    inn.close();
                    result = in.readLine();
                }
            }
            catch (IOException ioe)
            {
                Log.d("Exception Caught", ioe.getMessage());
            }

服务器代码

try
                {
                    int length = in.readInt();
                    System.out.println("Got the Size");
                    int bytesRead ;
                    int len = 0;
                    byte[] buffer = new byte[8192];
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    while (len<length)
                    {
                        bytesRead = in.read(buffer,0,(int)Math.min(buffer.length, length-len));
                        len = len + bytesRead;
                        baos.write(buffer, 0, bytesRead);
                    }
                    byte [] byteArray = new byte [length];
                    byteArray = baos.toByteArray();
                    File file = new File (fileName+(int)(Math.random()*500)+".jpg");
                    if (!file.exists())
                    {
                        file.createNewFile();
                    }
                    FileOutputStream fos = new FileOutputStream (file);
                    fos.write(byteArray);
                    fos.close();
                    out.println("Image Received");
                }
                catch (IOException ioe)
                {
                    ioe.printStackTrace();
                }

但是,我在客户端创建了两个额外的流,只是为了确定并发送压缩为 JPEG 格式的图像的大小,而不是直接在套接字输出流上使用 bmp.compress()。所以我想知道是否可以防止客户端代码中两个额外流的开销,或者是否有其他方法可以减少开销?

【问题讨论】:

  • 你不需要在new FileOutputStream(...)之前调用createNewFile()。只是浪费时间和空间。

标签: java android sockets


【解决方案1】:

如果您不需要保持连接打开,请不要发送长度,只需读取到流的末尾。如果您确实需要保持打开状态,则说明您的复制循环不正确:它可能会在图像末尾溢出。应该是:

while (len<length)
{
    bytesRead = in.read(buffer, 0, (int)Math.min(buffer.length, length-len));
    len = len + bytesRead;
    baos.write(buffer, 0, bytesRead);
}

【讨论】:

  • 我需要保持连接打开,所以修改了复制循环。是否有任何其他优化可以加快整个过程?
  • 要做的事情是先让它工作,但无论如何你不能将它加速到超过网络的速度。这是最好的。
猜你喜欢
  • 2015-06-21
  • 1970-01-01
  • 2020-11-24
  • 2019-08-27
  • 1970-01-01
  • 1970-01-01
  • 2011-07-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多