JPEG image in base64 format

2020-04-08 21:18发布

I have a camera that send me an xml file with some tags , including a tag that represents the image in base64 format. The image is in JPEG format.

For some reason the tag that represents the image it doesn't always have the same name (for a problem of the camera)

So to detect with certainty the tag that contains the image is correct i have to now one thing :

A JPEG image in base64 format ALWAYS start with /9j/ ?

Example :

data:image/jpeg;base64,-->/9j/<--4RYQRXhpZgAATU0AKgAAAAgABwESAAMAAAABAAEAAAEaAAUAAAABAAAAYgEbAAUAAAABAAAAagEoAAMAAAABAAIAAAExAAIAAAAgAAAAcgEyAAIAAAAUAAAAkodpAAQAAAABAAAAqAAAANQACvyAAAAnEAAK/IAAACcQQWRvYmUgUGhvdG9zaG9wIENTNiAoTWFjaW50b3NoKQAyMDE2OjA4OjA1IDA5OjQ1OjA4AAAAAAOgAQADAAAAAf//AACgAgAEAAAAAQAAAPGgAwAEAAAAAQAAAFkAAAAAAAAABgEDAAMAAAABAAYAAAEaAAUAAAABAAABIgEbAA....

1条回答
贼婆χ
2楼-- · 2020-04-08 21:22

Jpeg file format (as many others) can be identified by magic number. For JPEG the magic number is ff d8 ff at offset 0. If you encode this to Base64, you'll always get /9j/.

Additional note: since Base64 converts group of 3 data bytes to 4 bytes (ASCII), this will work for any magic number with length and offset multiple of 3 bytes. If the length of magic number is not multiple of 3, the last group of 4 Base-64 encoded characters will change depending on the data immediately following the magic number.

https://en.wikipedia.org/wiki/JPEG

https://en.wikipedia.org/wiki/List_of_file_signatures

查看更多
登录 后发表回答