script.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. $uploadJs .= '
  2. var buttonCapture = document.getElementById("buttonCapture");
  3. var buttonSave = document.getElementById("buttonSave");
  4. var savedImages = document.getElementById("savedImages");
  5. var canvas = document.getElementById("webcamcanvas");
  6. var video = document.getElementById("webcamvideo");
  7. var context;
  8. var width = ' . $dest_w . '; //set width of the video and image
  9. var height;
  10. var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
  11. var isSafari = /Safari/.test(navigator.userAgent) && /Apple Computer, Inc/.test(navigator.vendor);
  12. video.width = width;
  13. var canvas = canvas;
  14. canvas.style.width = width + "px";
  15. canvas.width = width;
  16. context = canvas.getContext("2d");
  17. if((isChrome || isSafari) && window.location.protocol == "http:") {
  18. savedImages.innerHTML = "<h1>This browser only supports camera streams over https:</h1>";
  19. } else {
  20. startWebcam();
  21. }
  22. function startWebcam() {
  23. navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mediaDevices || navigator.mozGetUserMedia || navigator.msGetUserMedia || navigator.oGetUserMedia;
  24. if (navigator.mediaDevices){
  25. navigator.mediaDevices.getUserMedia({video: true}, handleVideo, videoError).then(function(stream){
  26. video.onloadedmetadata = setHeight;
  27. buttonCapture.disabled = false;
  28. return video.srcObject = stream;
  29. }).catch(function(e) {
  30. console.log(e.name + ": "+ e.message);
  31. buttonCapture.disabled = true;
  32. switch(e.name) {
  33. case "NotAllowedError":
  34. savedImages.innerHTML = "<h3>You cant use this app because you denied camera access. Refresh the page and allow the camera to be used by this app.</h3>";
  35. break;
  36. case "NotReadableError":
  37. savedImages.innerHTML = "<h3>Camera not available. Your camera may be used by another application.</h3>";
  38. break;
  39. case "NotFoundError":
  40. savedImages.innerHTML = "<h3>Camera not available. Please connect a camera to your device.</h3>";
  41. break;
  42. }
  43. });
  44. } else {
  45. savedImages.innerHTML = "<h3>Camera not supported.</h3>";
  46. }
  47. function handleVideo(stream) {
  48. video.src = window.URL.createObjectURL(stream);
  49. }
  50. function videoError(e) {
  51. savedImages.innerHTML = "<h3>" + e +"</h3>";
  52. }
  53. function setHeight() {
  54. var ratio = video.videoWidth / video.videoHeight;
  55. height = width/ratio;
  56. canvas.style.height = height + "px";
  57. canvas.height = height;
  58. }
  59. //add event listener and handle the capture button
  60. buttonCapture.addEventListener("mousedown", handleButtonCaptureClick);
  61. function handleButtonCaptureClick() {
  62. if(canvas.style.display == "none" || canvas.style.display == ""){
  63. canvas.style.display = "block";
  64. buttonCapture.innerHTML = "' . $labelReCaptureF . '";
  65. setHeight();
  66. context.drawImage(video, 0, 0, width, height);
  67. buttonSave.innerHTML = "' . $labelSaveF . '";
  68. buttonSave.disabled = false;
  69. } else {
  70. makeCaptureButton();
  71. }
  72. }
  73. function makeCaptureButton() {
  74. canvas.style.display = "none";
  75. buttonCapture.innerHTML = "' . $labelCaptureF . '";
  76. buttonSave.innerHTML = "' . $labelSaveF . '";
  77. buttonSave.disabled = true;
  78. }
  79. //add event listener and handle the save button
  80. buttonSave.addEventListener("mousedown", handleButtonSaveClick);
  81. function handleButtonSaveClick() {
  82. var dataURL = canvas.toDataURL("image/jpg");
  83. var xhr = new XMLHttpRequest();
  84. xhr.open("POST", "' . $uploadUrl . '");
  85. xhr.onload = function() {
  86. if (xhr.readyState == 4 ) {
  87. if(xhr.status == 200) {
  88. savedImages.innerHTML=xhr.responseText;
  89. buttonSave.innerHTML = "Saved";
  90. buttonSave.disabled = true;
  91. makeCaptureButton();
  92. }
  93. }
  94. };
  95. var form = new FormData();
  96. form.append("image", dataURL);
  97. xhr.send(form);
  98. }
  99. }';