Skip to content

Instantly share code, notes, and snippets.

@movii
Last active April 30, 2017 08:31
Show Gist options
  • Save movii/50f5943c8635b9a08f11e05d99fb4848 to your computer and use it in GitHub Desktop.
Save movii/50f5943c8635b9a08f11e05d99fb4848 to your computer and use it in GitHub Desktop.
let offset = 0
, len = view.byteLength;
// SOI marker
if (view.getUint16(0, false) != 0xFFD8) reject('不是 JPEG 文件');
// APP1 marker
while (offset < len) {
if (view.getUint16(offset, false) == 0xFFE1) break;
else offset += 2;
}
if (offset >= len) reject('没找到 APP1 标识');
// now offset point to APP1 marker 0xFFD8
let APP1_offset = offset;
// offset + 4 point offset to EXIF Header
let EXIF_offset = APP1_offset + 4;
// check if have 'Exif' ascii string: 0x45786966
if (view.getUint32(EXIF_offset, false) != 0x45786966) reject('无 EXIF');
let TIFF_offset = EXIF_offset + 6;
// 0x4d4d: big endian, 0x4949: little endian
let little = view.getUint16(TIFF_offset, false) == 0x4949 ? true : false
let IFD0_offset = TIFF_offset + view.getUint32(TIFF_offset + 4);
let entries_count = view.getUint16(IFD0_offset, little);
let entries_offset = IFD0_offset + 2;
for (let i = 0; i < entries_count; i++ ) {
let tag_offset = entries_offset + (i * 12);
if (view.getUint16(tag_offset, little) == 0x0112) {
let resolve_value = view.getUint16(tag_offset + 8, little);
resolve(resolve_value);
}
}
reject('没有 orientation 信息');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment