为什么我的画布元素扭曲了我的图像?

发布于 2025-01-17 00:31:27 字数 1699 浏览 0 评论 0原文

画布元素扭曲了我的图像并放大到中间左侧。正如这里所见。

https://i.ibb.co/ZMCcM88/Screenshot-116.png

上面是它应该的样子,下面是它的样子。

我的 console.log() 显示视频和画布大小相等,并且不应出现任何扭曲或错误。

https://i.ibb.co/p0cHDXf/Screenshot-118.png

我的代码看起来像这样

模板

<div>
  <video ref="video" class="full-width" autoplay playsinline >
  <canvas ref="canvas" class="full-width" height="240" >
</div>

脚本

export default defineComponent({
  name: "CameraPage",
  setup() {

    const imageCaptured = ref(false);
    const video = ref(null);
    const canvas = ref(null);

    const initCamera = () => {
      navigator.mediaDevices
        .getUserMedia({
          video: true,
        })
        .then((stream) => {
          video.value.srcObject = stream;
        });
    };

    const captureImage = () => {
      canvas.width = video.value.getBoundingClientRect().width;
      canvas.height = video.value.getBoundingClientRect().height;
      let context = canvas.value.getContext("2d");
      context.drawImage(video.value, 0, 0, canvas.width, canvas.height);
      imageCaptured.value = "True";
    };

    onMounted(() => {
      initCamera();
    });

    return {
      initCamera,
      onMounted,
      captureImage,
      video,
      canvas,
      imageCaptured,
    };
  },
});

我在做什么导致图像扭曲?我在drawImage()方法中得到的东西是错误的吗?是canvas html属性中的“全角”和“高度”吗?

我只是想“截取流媒体视频的屏幕截图”并使其看起来基本上像一张照片。

我如此接近,却又如此遥远。

The canvas element is distorting my image and zooming in to the middle left. As seen here.

https://i.ibb.co/ZMCcM88/Screenshot-116.png

The above is what it should look like, the bottom is how its coming out.

My console.log() shows the video and canvas sizes are equal and nothing should be distorted or coming out wrong.

https://i.ibb.co/p0cHDXf/Screenshot-118.png

My code looks like this

Template

<div>
  <video ref="video" class="full-width" autoplay playsinline >
  <canvas ref="canvas" class="full-width" height="240" >
</div>

Script

export default defineComponent({
  name: "CameraPage",
  setup() {

    const imageCaptured = ref(false);
    const video = ref(null);
    const canvas = ref(null);

    const initCamera = () => {
      navigator.mediaDevices
        .getUserMedia({
          video: true,
        })
        .then((stream) => {
          video.value.srcObject = stream;
        });
    };

    const captureImage = () => {
      canvas.width = video.value.getBoundingClientRect().width;
      canvas.height = video.value.getBoundingClientRect().height;
      let context = canvas.value.getContext("2d");
      context.drawImage(video.value, 0, 0, canvas.width, canvas.height);
      imageCaptured.value = "True";
    };

    onMounted(() => {
      initCamera();
    });

    return {
      initCamera,
      onMounted,
      captureImage,
      video,
      canvas,
      imageCaptured,
    };
  },
});

What am I doing that's distorting the image? Am I getting something in the drawImage() method thats wrong? Is it the "full-width" and "height" in the canvas html attributes?

I just want to "take a screenshot" of the streaming video and make it look like a photo basically.

Im so close but so far away.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文