showing results for - "read image metadata javascript"
Ilias
02 Nov 2016
1function getOrientation(file, callback) {
2    var reader = new FileReader();
3    reader.onload = function(e) {
4
5        var view = new DataView(e.target.result);
6        if (view.getUint16(0, false) != 0xFFD8)
7        {
8            return callback(-2);
9        }
10        var length = view.byteLength, offset = 2;
11        while (offset < length) 
12        {
13            if (view.getUint16(offset+2, false) <= 8) return callback(-1);
14            var marker = view.getUint16(offset, false);
15            offset += 2;
16            if (marker == 0xFFE1) 
17            {
18                if (view.getUint32(offset += 2, false) != 0x45786966) 
19                {
20                    return callback(-1);
21                }
22
23                var little = view.getUint16(offset += 6, false) == 0x4949;
24                offset += view.getUint32(offset + 4, little);
25                var tags = view.getUint16(offset, little);
26                offset += 2;
27                for (var i = 0; i < tags; i++)
28                {
29                    if (view.getUint16(offset + (i * 12), little) == 0x0112)
30                    {
31                        return callback(view.getUint16(offset + (i * 12) + 8, little));
32                    }
33                }
34            }
35            else if ((marker & 0xFF00) != 0xFF00)
36            {
37                break;
38            }
39            else
40            { 
41                offset += view.getUint16(offset, false);
42            }
43        }
44        return callback(-1);
45    };
46    reader.readAsArrayBuffer(file);
47}
48
49// usage:
50var input = document.getElementById('input');
51input.onchange = function(e) {
52    getOrientation(input.files[0], function(orientation) {
53        alert('orientation: ' + orientation);
54    });
55}