forked from SouICry/fetch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
171 lines (124 loc) · 5.16 KB
/
test.html
File metadata and controls
171 lines (124 loc) · 5.16 KB
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
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script> /*
https://davidwalsh.name/browser-camera
https://stackoverflow.com/questions/18483160/which-camera-will-open-getusermedia-api-in-mobile-device-front-or-rear
https://codeforgeek.com/2014/11/file-uploads-using-node-js/
*/
'use strict';
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
function gotSources(sourceInfos) {
for (var i = 0; i !== sourceInfos.length; ++i) {
var sourceInfo = sourceInfos[i];
var option = document.createElement('option');
option.value = sourceInfo.id;
if (sourceInfo.kind === 'audio') {
} else if (sourceInfo.kind === 'video') {
option.text = sourceInfo.label || 'camera ' + (videoSelect.length + 1);
videoSelect.appendChild(option);
} else {
console.log('Some other kind of source: ', sourceInfo);
}
}
}
function enableCamera(vid, canvas, takeButton, redoButton, source) {
if (typeof MediaStreamTrack === 'undefined' ||
typeof MediaStreamTrack.getSources === 'undefined') {
// alert('Your browser doesnt support using the camera :( Try uploading or use Chrome instead');
} else {
MediaStreamTrack.getSources(gotSources);
}
var videoSelect = document.getElementById('videoSource');
if (source >= videoSelect.length){
source = videoSelect.length - 1;
}
videoSelect.selectedIndex = source;
var videoSource = videoSelect.value;
var constraints = {
video: {
optional: [{
sourceId: videoSource
}]
}
};
function successCallback(stream) {
window.stream = stream; // make stream available to console
vid.src = window.URL.createObjectURL(stream);
vid.play();
}
function errorCallback(error) {
console.log('navigator.getUserMedia error: ', error);
// alert('Something went wrong with the camera :( Try uploading or use Chrome instead');
}
function disableCamera() {
if (window.stream) {
vid.src = null;
window.stream.getTracks().forEach(function (track) {
track.stop();
});
window.stream = null;
}
}
navigator.getUserMedia(constraints, successCallback, errorCallback);
takeButton.addEventListener("click", function () {
var context = canvas.getContext("2d");
context.drawImage(vid, 0, 0, canvasWidth, canvasHeight);
disableCamera();
});
redoButton.addEventListener("click", function(){
navigator.getUserMedia(constraints, successCallback, errorCallback);
});
}
</script>
<select class="hidden" style="display: none" id="videoSource"></select>
<video id="video1" width="800" height="600" muted autoplay></video>
<canvas id="canvas1" width="800" height="600"></canvas>
<a id="button" download="file.png">Download</a>
<button id="takeButton1">Take photo</button>
<button id="redoButton1">Retake Photo</button>
<button id="uploadButton1">Upload to server</button>
<input type="file" id="uploadImage1" name="imageLoader"/>
<script>
var canvasWidth = 800;
var canvasHeight = 600;
enableCamera(document.getElementById("video1"),
document.getElementById("canvas1"),
document.getElementById("takeButton1"),
document.getElementById("redoButton1"),
0);
var imageLoader = document.getElementById('uploadImage1');
imageLoader.addEventListener('change', handleImageUpload, false);
function handleImageUpload() {
var reader = new FileReader();
reader.onload = function (event) {
var img = new Image();
img.onload = function () {
var widthScaleFactor = canvas.width / img.width;
var heightScaleFactor = canvas.height / img.height;
var scaleFactor = widthScaleFactor > heightScaleFactor ? widthScaleFactor : heightScaleFactor;
var imgWidth = img.width * scaleFactor;
var imgHeight = img.height * scaleFactor;
var widthOffset = (imgWidth - canvas.width) / 2;
var heightOffset = (imgHeight - canvas.height) / 2;
context.drawImage(img, -widthOffset, -heightOffset, imgWidth, imgHeight);
};
img.src = event.target.result;
};
reader.readAsDataURL(e.target.files[0]);
}
var canvas = document.getElementById("canvas1");
$(document).ready(function () {
$('#uploadButton1').click(function () {
$.ajax({
type: "POST",
contentType: "application/json",
dataType: "json",
url: "/savePhoto",
data: JSON.stringify({image: canvas.toDataURL("image/png")}),
success: function (resp) {
// alert(resp);
}
});
})
})
</script>