Javascript,将相机 (canvas) 中的照片存储到 local/server

Javascript, store photo from camera (canvas) to local/server

问题

我正在使用此站点的 javascript。

https://developers.google.com/web/fundamentals/media/capturing-images/#acquire_access_to_the_camera

有关于 canvas 的信息,我可以:

例如,如何将 canvas 存储到本地或服务器?那么时髦的效果呢? :)

并且是否可以在没有查看相机的情况下将相机的输入存储到文件中video tagwindow?

谢谢。

系统

Linux local 5.0.0-29-lowlatency #31-Ubuntu SMP PREEMPT Thu Sep 12 14:13:01 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

更新

您可以使用 canvas.toDataURL("img/png"):

将图像 src 保存为 base64
captureButton.addEventListener('click', () => {
    // Draw the video frame to the canvas.
    context.drawImage(player, 0, 0, canvas.width, canvas.height);
    // Get the image src (base64)
    const imgSrc=canvas.toDataURL("img/png");

    // Apply the src to an image element
    const img = new Image();
    img.src = imgSrc

    // Add the newly created image to the DOM
    // A html element with the class .image-holder needs to exist on the page
    document.querySelector('.image-holder').appendChild(img);

    // Store the src in local storage
    localStorage.setItem('imgSrc', imgSrc)
  });

要将其保存到您的本地计算机,您可以使用类似 FileSaver (thanks to this SO answer):

captureButton.addEventListener('click', () => {
    // Draw the video frame to the canvas.
    context.drawImage(player, 0, 0, canvas.width, canvas.height);

    // save the file
    canvas.toBlob(function(blob) {
        saveAs(blob, "image-name.jpg");
    });
  });

编辑:这里的工作示例 https://codepen.io/relativemc/pen/QWLoOZO