开发者

Android: Looking for a way to speed up Image Transfer from Android to PC over wifi

开发者 https://www.devze.com 2023-02-05 19:12 出处:网络
I am trying to transfer images quickly between my Android phone and my PC over wifi. I have written code to do this but it can 4-5 seconds to transfer one 640x480px image across. I am wondering is my

I am trying to transfer images quickly between my Android phone and my PC over wifi. I have written code to do this but it can 4-5 seconds to transfer one 640x480px image across. I am wondering is my method flawed and is there a faster way to do this?

Here is the Server code

void main(String[] args)
{
        try {
             ServerSocket serverSocket = new ServerSocket(5555);
             Socket clientSocket = serverSocket.accept();

             long startTime = System.currentTimeMillis();
             InputStream clientInputStrea开发者_C百科m = clientSocket.getInputStream();
             BufferedImage BI = ImageIO.read(clientInputStream);
             long endTime = System.currentTimeMillis();
             ImageIO.write(BI,"png",new File("test.png"));
             System.out.println((endTime - startTime) + " ms.");

             } catch (IOException e) 
             {
                e.printStackTrace();
             }     
}
}

Here is the Android client code

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Bitmap imageToSend = BitmapFactory.decodeResource(this.getResources(),R.drawable.img);

    try 
    {
        Socket socket = new Socket("192.168.1.1",5555);         
        imageToSend.compress(CompressFormat.PNG,0 , socket.getOutputStream());          
    }        
    catch (Exception e) {
        e.printStackTrace();
    }
}

Thank you for any help you can give.


Two things I can think of:

  • Use a buffered output stream in your output e.g.

    new BufferedOutputStream(socket.getOutputStream());

  • Write the image to disk first. Then try to open sockets in parallel each transferring a different offset of the image (e.g. split the image to 3 jobs, transfer each in parallel to the others). This will workaround some TCP behaviors.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号