-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimagepicker.js
177 lines (109 loc) · 3.11 KB
/
imagepicker.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
$('document').ready(function() {
var photo = document.getElementById('photo');
var context = photo.getContext('2d');
showEnable();
$('#pickFile').click(function() {
stopStream();
removeSmile();
hideSave();
hideEnable();
context.clearRect ( 0 , 0 , photo.width , photo.height );
chrome.fileSystem.chooseEntry({
accepts: [{mimeTypes:['image/*']}]
}, function (fileEntry) {
fileEntry.file(function(fff) {
var reader = new FileReader();
reader.onloadend = function(e) {
console.log(this.result);
//image_element.src = this.result;
var img = new Image;
img.onload = function(){
context.clearRect ( 0 , 0 , photo.width , photo.height );
context.drawImage(img, 0, 0, photo.width, photo.height);
}
img.src = this.result;
showSave(false);
};
reader.readAsDataURL(fff);
});
});
});
function getCamera() {
hideSave();
context.clearRect ( 0 , 0 , photo.width , photo.height );
navigator.webkitGetUserMedia({audio: true, video: true}, function(stream) {
document.querySelector('video').src = webkitURL.createObjectURL(stream);
setupPhotoBooth();
}, function(e) {
console.error(e);
});
}
var saveButton;
function removeSmile() {
$('#smileButton').unbind();
$('#smileButton').css('display', 'none');
}
function setupPhotoBooth() {
console.log('setupPhotoBooth');
$('video').css('display','inline');
hideEnable();
$('#smileButton').css('display', 'inline');
$('#smileButton').click(function(e) {
takePhoto();
});
}
function takePhoto() {
var video = document.querySelector('video');
context.clearRect ( 0 , 0 , photo.width , photo.height );
context.drawImage(video, 0, 0, photo.width, photo.height);
showSave(true);
}
function showSave(isCamera) {
$('#saveButton').unbind();
$('#saveButton').css('display', 'inline');
$('#saveButton').click(function(e) {
savePhoto(isCamera);
});
}
function hideSave() {
$('#saveButton').unbind();
$('#saveButton').css('display', 'none');
}
function showEnable () {
/**
* Click handler to init the camera grab
*/
$('#buttonEnable').css('display', 'inline');
$('#buttonEnable').click(function(e) {
getCamera();
});
}
function hideEnable() {
$('#buttonEnable').unbind();
$('#buttonEnable').css('display', 'none');
}
function savePhoto(isCamera) {
console.log('savePhoto');
var data = photo.toDataURL("image/png");
data = data.replace("image/png","image/octet-stream");
console.log(data);
Entry.saveImage(data);
showEnable();
removeSmile();
hideSave();
context.clearRect ( 0 , 0 , 320 , photo.height );
if (isCamera) {
stopStream();
}
}
function stopStream() {
navigator.webkitGetUserMedia({audio: true, video: true},
function(stream)
{
var video = document.querySelector('video');
video.src = window.webkitURL.createObjectURL(null);
$('video').css('display','none');
}
);
}
})