使用WebRTC将从网络摄像头捕获的图像上传到服务器

时间:2020-02-12 04:49:26

标签: javascript html webrtc

我正在做一个演示项目,因为我实际上对编码还不熟悉,现在遇到了一些问题。基本上,我的项目是关于智能出勤的,学生需要将手机拍摄的自拍照提交到服务器。该项目使用HTML,CSS和Js。我正在使用表单通过POST方法提交图像。因此,我引用this使用WebRTC从网络摄像头捕获图像。从我在多个论坛帖子中看到的内容来看,WebRTC捕获图像,并且该图像不是图像文件,而是base64格式。基于多项研究,以下是使用JavaScript进行捕获过程的代码:

function takepicture() {
  var context = canvas.getContext('2d');
  if (width && height) {
    canvas.width = width;
    canvas.height = height;
    context.drawImage(video, 0, 0, width, height);

    var data = canvas.toDataURL('image/png');
    photo.setAttribute('src', data);
    $.ajax({
      url: "/",
      data: data,
      type: 'POST'
      success: function(dataR) {}
      error: function(xhr, status, error) {
        console.log('Error:' + error.message);
      }
    });
  } else {
    clearphoto();
  }
}
HTML Code:

<form method="POST" form class="login100-form validate-form" enctype="multipart/form-data">
  <div class="wrap-input100 validate-input" data-validate="Enter e-mail">
    <input class="input100" type="text" name="staff_email" placeholder="E-mail">
    <span class="focus-input100" data-placeholder="&#xf207;"></span>
  </div>
  <div class="camera">
    <video id="video">Video stream not available.</video>
    <button id="startbutton">Take photo</button>
  </div>
  <canvas id="canvas">
  		    </canvas>
  <div class="output">
    <img id="photo" alt="The screen capture will appear in this box.">
  </div>

我想将捕获的照片提交到服务器,但是根本没有提交。我可以知道我面临什么样的问题吗?我很乐意提供更多说明。在其他一些文章中,他们使用PHP,但我并不真正了解它……..在此先感谢=)

0 个答案:

没有答案