Detect Eof for JPG images

后端 未结 3 981
我寻月下人不归
我寻月下人不归 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.

    0 讨论(0)
  • 2020-11-30 05:37

    Well, there's no guarantee that you won't find FFD9 within a jpeg image. The best way you can find the end of a jpeg image is to parse it. Every marker, except for FFD0 to FFD9 and FF01(reserved), is immediately followed by a length specifier that will give you the length of that marker segment, including the length specifier but not the marker. FF00 is not a marker, but for your purposes you can treat it as marker without a length specifier.

    The length specifier is two bytes long and it's big endian. So what you'll do is search for FF, and if the following byte is not one of 0x00, 0x01 or 0xD0-0xD8, you read the length specifier and skips forward in the stream as long as the length specifier says minus two bytes.

    Also, every marker can be padded in the beginning with any number of FF's.

    When you get to FFD9 you're at the end of the stream.

    Of course you could read the stream word by word, searching for FF if you want performance but that's left as an exercise for the reader. ;-)

    0 讨论(0)
  • 2020-11-30 05:40

    A quick look at Wikipedia's JPEG article would have given you the answer:

    • bytes 0xFF, 0xD8 indicate start of image
    • bytes 0xFF, 0xD9 indicate end of image
    0 讨论(0)
提交回复
热议问题