Android:寻找一种方法来加速通过 wifi 从 Android 到 PC 的图像传输
我正在尝试通过 wifi 在 Android 手机和 PC 之间快速传输图像。我已经编写了代码来执行此操作,但传输一张 640x480px 图像可能需要 4-5 秒。我想知道我的方法是否有缺陷,是否有更快的方法来做到这一点?
这是服务器代码
void main(String[] args)
{
try {
ServerSocket serverSocket = new ServerSocket(5555);
Socket clientSocket = serverSocket.accept();
long startTime = System.currentTimeMillis();
InputStream clientInputStream = 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();
}
}
}
这是 Android 客户端代码
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();
}
}
感谢您提供的任何帮助。
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 clientInputStream = 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能想到的两件事:
在输出中使用缓冲输出流,例如
new BufferedOutputStream(socket.getOutputStream());
首先将映像写入磁盘。然后尝试并行打开套接字,每个套接字传输图像的不同偏移量(例如,将图像拆分为 3 个作业,将每个作业并行传输到其他作业)。这将解决一些 TCP 行为。
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.