forked from don/html-cam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
44 lines (34 loc) · 1.36 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
var fileChooser = document.getElementsByTagName('input')[0];
var content = document.getElementById('content');
if (typeof window.FileReader === 'undefined') {
content.className = 'fail';
content.innerHTML = 'File API & FileReader API are not supported in your browser. Try on a new-ish Android phone.';
}
fileChooser.onchange = function (e) {
//e.preventDefault();
var file = fileChooser.files[0],
reader = new FileReader();
reader.onerror = function (event) {
content.innerHTML = "Error reading file";
}
reader.onload = function (event) {
var img = new Image();
// files from the Gallery need the URL adjusted
if (event.target.result && event.target.result.match(/^data:base64/)) {
img.src = event.target.result.replace(/^data:base64/, 'data:image/jpeg;base64');
} else {
img.src = event.target.result;
}
// Guess photo orientation based on device orientation, works when taking picture, fails when loading from gallery
if (navigator.userAgent.match(/mobile/i) && window.orientation === 0) {
img.height = 250;
img.className = 'rotate';
} else {
img.width = 400;
}
content.innerHTML = '';
content.appendChild(img);
};
reader.readAsDataURL(file);
return false;
}