Detect Eof for JPG images

后端 未结 3 996
我寻月下人不归
我寻月下人不归 2020-11-30 04:42

I am sending many images from my server to client in sequence continuously through TCP.Now at client,how should i detect efficiently that this is end of my one image so writ

3条回答
  •  一个人的身影
    2020-11-30 05:17

    If you're sending the images via a byte array then you can simply add the file size of the image as a pair of bytes before the start of the file.
    Client grabs the first two bytes to find specified number of bytes (we'll call that x) and discards them, then pumps the next x number of bytes into a buffer which it can write to file.
    Rinse and repeat for all following jpegs.

    An alternative is just looking for the FFD9 marker - if I'm not mistaken any compressed value FF will be encoded as FF00 (the 00 byte is discarded and the FF byte is kept).
    The problem with this is that you get things like thumbnails with their own FFD9 headers, but those are contained within a segment in the headers. Those segments have a length value in the two bytes after their marker so you can just skip to the end of any segment you encounter to avoid premature eoi detection.

提交回复
热议问题