简体   繁体   English

从TCP套接字读取JPEG字节(Android)

[英]Read JPEG bytes from TCP socket (Android)

I have an application that show CCTV feed from mobile. 我有一个应用程序可以显示来自移动设备的CCTV供稿。 I successfully develop iOS application and now my client wants me to port to Android. 我成功开发了iOS应用程序,现在我的客户希望我移植到Android。 The porting was fine until I'm stuck at this part of code. 移植很好,直到我被这部分代码所困扰。

This part of code, I have to connect to TCP socket server. 这部分代码,我必须连接到TCP套接字服务器。 When connected, I don't have to send server any thing, instead, server will send me a JPEG image. 连接后,我不必向服务器发送任何东西,而是服务器将向我发送JPEG图像。 So, after connected, I'll have to keep reading until I received JPEG end marker (0xFF, 0xD9) and then close the connection. 因此,连接后,我必须继续阅读直到收到JPEG结束标记(0xFF,0xD9),然后关闭连接。

What I plan to do is 我打算做的是

Socket s = new Socket("Server IP Addreess", SERVER_PORT);
DataInputStream dis = new DataInputStream(socket.getInputStream());

ArrayList<Byte> bytes = new ArrayList<Byte>();
boolean err = false;

while (true) {
    byte b = dis.readByte();

    if (b == -1) {
        err = true;
        break;
    }        

    bytes.add(b);

    if ((bytes.get(bytes.size() - 1) == (byte) 0xFF) &&
        (bytes.get(bytes.size() - 2) == (byte) 0xD9)) {
        break;
    }
}

socket.close();

if (!err) {
    // create bitmap from byte array and show in ImageView
}

But I'm not sure that this is correct or not. 但是我不确定这是正确还是错误。 The other solution I'm thinking about is 我正在考虑的另一个解决方案是

Socket s = new Socket("Server IP Addreess", SERVER_PORT);
BitmapFactory.decodeStream(s.getInputSteam());

But, also, I don't know how to close socket when server send 0xFF, 0xD9. 但是,而且,当服务器发送0xFF,0xD9时,我也不知道如何关闭套接字。 Or will the BitmapFactory will detect that marker and close socket connection for me? 还是BitmapFactory将为我检测到该标记并关闭套接字连接?

Any suggestion are welcome. 任何建议都欢迎。 Thank you! 谢谢!

PS I don't have test environment as they took it back when I delivered iOS app. PS:我没有测试环境,因为当我交付iOS应用时,他们将其收回。 So, I have to develop and then deliver to them to test (by playing with the app). 因此,我必须进行开发,然后交付给他们进行测试(通过使用该应用程序进行测试)。 Then if it's not working, they will tell me to correct it but I won't be able to access LogCat or other useful information. 然后,如果它不起作用,他们将告诉我更正它,但我将无法访问LogCat或其他有用的信息。 This is just like trying to sew a button in dark room, and I can't turn on the light. 这就像试图在黑暗的房间里缝一个按钮,而我却无法打开灯。

: | :|

EDIT 编辑

More information about this server 有关此服务器的更多信息

  • Server won't send length of file, so, number of bytes for each file is unknown. 服务器不会发送文件长度,因此每个文件的字节数未知。
  • I have to detect 0xFF, 0xD9 that indicate end of file. 我必须检测指示文件结尾的0xFF,0xD9。
  • Problem is, I have to terminate socket connection from my side, server won't do it. 问题是,我必须从我端终止套接字连接,服务器不会这样做。
  • I can't change the way server works, it's hardware that my client purchase. 我无法更改服务器的工作方式,这是客户购买的硬件。

Also, I'm getting one image for each TCP connection , I'm not getting multiple images for single TCP connection. 另外, 我为每个TCP连接获取一个图像 ,而对于单个TCP连接却没有获取多个图像。

This is a bad idea to just look for some magic bytes to determine the end of the data. 仅寻找一些魔术字节来确定数据的结尾是一个坏主意。 While these magic bytes should be at the end of the file they can also happen inside the data. 尽管这些不可思议的字节应该位于文件的末尾,但它们也可能出现在数据内部。

Better would be to either prefix the data with the length or use a separate TCP connection for each jpeg file and just read until the end of connection (that is until you don't get any more data). 最好是给数据加上前缀长度,或者为每个jpeg文件使用单独的TCP连接,并一直读取直到连接结束(即直到您没有更多数据为止)为止。 If this is not possible you have to do more advanced parsing, see Detect Eof for JPG images . 如果无法执行此操作,则必须执行更多高级分析,请参阅检测JPG图像的Eof

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

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