face-check-index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. $(function () {
  2. // let lodingMsg = this.$Message.loading('视频加载中....', 6000);
  3. // alert(1);
  4. startVideo();
  5. });
  6. function startVideo(){
  7. // let lodingMsg = that.$Message.loading('视频加载中....', 6000);
  8. $('#startBtn').attr('disabled', true);
  9. $('#stopBtn').attr('disabled', true);
  10. $('#ppBtn').attr('disabled', true);
  11. $("#outVideo-div").html('');
  12. //判断浏览器
  13. navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
  14. var constraints = { audio: true, video: { width: 480, height: 320 } };
  15. var aa = '' ; //防止两次上传
  16. //调用摄像头 成功后获取视频流:mediaStream
  17. navigator.mediaDevices.getUserMedia(constraints)
  18. .then(function(mediaStream) {
  19. var video = document.querySelector('video');
  20. // 赋值 video 并开始播放
  21. video.srcObject = mediaStream;
  22. video.onloadedmetadata = function(e) {
  23. video.play();
  24. $('#startBtn').attr('disabled', false);
  25. alert('视频已加载成功');
  26. };
  27. //录像api的调用
  28. var mediaRecorder = new MediaStreamRecorder(mediaStream);
  29. mediaRecorder.mimeType = 'video/webm';
  30. // 停止录像以后的回调函数
  31. mediaRecorder.ondataavailable = function (blob) {
  32. // 停止以后调用上传
  33. if(aa == ""){
  34. uploadToPHPServer(blob);
  35. aa = blob;
  36. }
  37. };
  38. $("#startBtn").click(function(){
  39. showTime(4);
  40. $('#commentDiv').html('活体人脸识别音频开始录制...录制3秒后自动上传转为base64,录制倒计时:');
  41. $('#startBtn').attr('disabled',true);
  42. $('#ppBtn').attr('disabled',true);
  43. $('#stopBtn').attr('disabled',true);
  44. //开始录像
  45. mediaRecorder.start();
  46. setTimeout(function(){
  47. //停止录像
  48. mediaRecorder.stop();
  49. },4000);
  50. });
  51. //停止录像
  52. $("#stopBtn").click(function(){
  53. $('#commentDiv').html('');
  54. mediaRecorder.stop();
  55. });
  56. //重新开始录像
  57. $("#ppBtn").click(function(){
  58. if(aa !== ""){
  59. showTime(4);
  60. $('#commentDiv').html('活体人脸识别音频开始录制...录制3秒后自动上传转为base64,录制倒计时:');
  61. // $('#startBtn').attr('disabled',false);
  62. $('#stopBtn').attr('disabled',true);
  63. $('#ppBtn').attr('disabled',true);
  64. aa = "";
  65. mediaRecorder.start();
  66. setTimeout(function(){
  67. // 4秒后停止录像
  68. mediaRecorder.stop();
  69. },4000);
  70. }
  71. });
  72. // 上传
  73. function uploadToPHPServer(blob) {
  74. var file = new File([blob], 'msr-' + (new Date).toISOString().replace(/:|\./g, '-') + '.webm', {
  75. type: 'video/webm'
  76. });
  77. var formData = new FormData();
  78. formData.append('videoFilename', file.name);
  79. formData.append('file', file);
  80. console.log(formData.get("file"));
  81. $('#ppBtn').attr('disabled', false);
  82. var src = getObjectURL(formData.get("file"));//这里的objURL就是file的真实路径
  83. $("#outVideo-div").html('');
  84. var video = '<video id="outVideo" controls="" autoplay="" name="media" style="width: 480px !important;height: 320px !important;">\n' +
  85. '<source src=\"' + src +
  86. '\"></video>';
  87. $("#outVideo-div").html(video);
  88. $('#commentDiv').html('');
  89. alert("录像结束,临时路径:"+src,function () {
  90. // $("#outVideo-div").html('');
  91. /*$.ajax({
  92. url:'../sys/oss/uploadAndBase64',
  93. dataType:'json',
  94. type:'POST',
  95. async: false,
  96. data: formData,
  97. processData : false, // 使数据不做处理
  98. contentType : false, // 不要设置Content-Type请求头
  99. success: function(data){
  100. console.log(data);
  101. if(data.code==0){
  102. var src = getObjectURL(formData.get("file"));
  103. var video = '<video id="outVideo" controls="" autoplay="" name="media" style="width: 480px !important;height: 320px !important;">\n' +
  104. '<source src=\"' + src +
  105. '\"></video>';
  106. $("#outVideo-div").html(video);
  107. $('#ppBtn').attr('disabled', false);
  108. $('#commentDiv').html('');
  109. vm.faceLivenessRecognition.videoBase64 = data.base64EncoderImg;
  110. }else{
  111. alert(data.msg);
  112. $('#ppBtn').attr('disabled', false);
  113. }
  114. },
  115. error:function(response){
  116. console.log(response);
  117. }
  118. });*/
  119. });
  120. }
  121. // 上传结束
  122. }).catch(function(err) {
  123. alert(err.name + ": " + err.message);
  124. });
  125. }
  126. function getObjectURL(file) {
  127. var url = null;
  128. if (window.createObjcectURL != undefined) {
  129. url = window.createOjcectURL(file);
  130. } else if (window.URL != undefined) {
  131. url = window.URL.createObjectURL(file);
  132. } else if (window.webkitURL != undefined) {
  133. url = window.webkitURL.createObjectURL(file);
  134. }
  135. return url;
  136. }
  137. // 计时开始
  138. function startTimer(){
  139. let self = this;
  140. this.si = setInterval(function () {
  141. self.timer++;
  142. }, 1000);
  143. }
  144. // 比赛结束,停止计时
  145. function stopTimer(){
  146. let self = this;
  147. clearInterval(self.si);
  148. }
  149. //显示倒数秒数
  150. function showTime(countdown){
  151. let self = this;
  152. self.isShareCount = true;
  153. self.isShowBtn = false;
  154. self.isShowFalseBtn = true;
  155. document.getElementById('showtimes').innerHTML = countdown;
  156. if (countdown == 0) {
  157. self.isShareCount = false;
  158. self.isShowTimer = true;
  159. self.isShowFalseBtn = false;
  160. document.getElementById('showtimes').innerHTML = "";
  161. // 计时器开始计时
  162. self.startTimer();
  163. } else {
  164. countdown -= 1;
  165. setTimeout(function () {
  166. self.showTime(countdown);
  167. }, 1000);
  168. }
  169. }