2012-03-23 76 views
0

使用蓝牙将Android camera.callback中的图像传输到PC。在Windows端我使用DataInputStream来读取传入的数据。问题在于PC端无法检测到第一张图像和第二张图像的结束。因此我无法重建图像。检测通过蓝牙传输的两张图像之间的EOF

DataOutputStream类的错误是在下面的代码

public void create_file() { 
    synchronized (frames) { 
     if (frames.size() > 0) { 
      Log.i("dhiraj", "" + frames.size()); 
      YuvImage image = new YuvImage(frames.remove(0), 
        ImageFormat.NV21, size.width, size.height, null); 

      try { 
       out = new DataOutputStream(client.getOutputStream()); 
      } catch (IOException e1) { 
       Log.i("dhiraj", "new outstream error"); 
       e1.printStackTrace(); 
      } 
      Log.i("dhiraj", "new outstream"); 
      image.compressToJpeg(rectangle, 90, baos); 
      Log.i("dhiraj", "compressed"); 
      try {     
       out.write(baos.toByteArray()); 
       Log.i("dhiraj", "" + baos.size()); 
       Log.i("dhiraj", "output"); 
      } catch (IOException e) { 
       Log.i("dhiraj", "IO"); 
       e.printStackTrace(); 
      } 

      try { 
       out.close(); 
      } catch (IOException e) { 
       Log.i("dhiraj", "new outstream close error"); 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

回答

0

有没有可能每个图像之前发送的大小?所以你可以只读n个字节。 也许将大小写入文件。

伪:

for (each image) 
    write (size of image) to size.txt on Android 
    transfer size.txt to PC 
    read the (size of image) from size.txt 
    read for (size of image) from bluetooth 
end for 

另一种选择是字符的一个非常独特的序列附加到每个图像的端部,和在接收之后剥离它关闭。 如果这不是唯一的,你可以在图像的某个地方找到这个序列,并且将图像分成几部分。

+0

我可能会工作,但会大大减慢整个操作。我想发送的图像速度非常快,以便他们可以投影,我们得到一个视图,就好像一个视频播放...否则我想通过蓝牙将数码流预览传输到电脑 – 2012-03-23 15:59:46

+0

“显着减慢整个操作”?发送一个包含几个字符的文件......或者甚至更好,只是一个整数作为一个字节流......只有几个字节。 实际上只有两种选择:1)终止字符(或序列)。 2)附上一个大小的标题,以知道要阅读多少。 (而不是将图像头添加到图像上,您可以将图像大小作为单独的xfer发送,以简化操作)。 – braden 2012-03-23 16:16:33

+0

你是否检查过你发送的图像格式还没有终止序列/字符?在十六进制编辑器中打开几张图像,看看是否可以识别它们最后共有的任何图案。 – braden 2012-03-23 16:22:10