将画布数据设置为输入

时间:2019-02-01 15:26:03

标签: javascript jquery html

我想从用户那里拍摄一张照片并将其发送到不带ajax的服务器。 我搜索并找到此代码,该代码从视频中获取照片并将其绘制在画布上。现在我想将该图像设置为表单上的输入,但是我不知道如何。 这是HTML代码:

<video id="player" controls autoplay></video>
<button id="capture" onclick="$('#loader').hide()">Capture</button>
<canvas id="canvas" width=320 height=240></canvas>
<input id="image-input">

这是javascript代码:

const player = document.getElementById('player');
  const canvas = document.getElementById('canvas');
  const context = canvas.getContext('2d');
  const captureButton = document.getElementById('capture');

  const constraints = {
    video: true,
  };

  captureButton.addEventListener('click', () => {
    // Draw the video frame to the canvas.
    context.drawImage(player, 0, 0, canvas.width, canvas.height);
    player.srcObject.getVideoTracks().forEach(track => track.stop());
    var Pic = document.getElementById("canvas").toDataURL("image/png");
    Pic = Pic.replace(/^data:image\/(png|jpg);base64,/, "")
  });

  // Attach the video stream to the video element and autoplay.
  navigator.mediaDevices.getUserMedia(constraints)
    .then((stream) => {
      player.srcObject = stream;
    });

现在我想将此图像设置为输入图像。 我该怎么办?

0 个答案:

没有答案