【问题标题】:Working with video and canvas element in Angular在 Angular 中使用视频和画布元素
【发布时间】:2019-08-24 07:12:48
【问题描述】:

我正在尝试按照某些 Google 开发人员教程中的建议使用 drawImage 获取流并对其进行渲染。我不断收到错误:

TypeError:无法在“CanvasRenderingContext2D”上执行“drawImage”:提供的值不是类型“(CSSImageValue 或 HTMLImageElement 或 SVGImageElement 或 HTMLVideoElement 或 HTMLCanvasElement 或 ImageBitmap 或 OffscreenCanvas)”

几个小时后,我仍然不知道为什么,所以我想我会寻求帮助。附上我的代码...

import {
  Component,
  OnInit,
  ViewChild,
  ElementRef
} from '@angular/core';


@Component({
  selector: 'app-scanner',
  templateUrl: './scanner.component.html',
  styleUrls: ['./scanner.component.css']
})
export class ScannerComponent implements OnInit {
  constructor() {}

  @ViewChild('mycanvas') mycanvas: ElementRef;
  @ViewChild('myvideo') myvideo: ElementRef;
  ctx;

  ngOnInit() {
    this.drawOnCanvas();
  }

  drawOnCanvas() {
    this.ctx = this.mycanvas.nativeElement.getContext('2d');

    navigator.mediaDevices
      .getUserMedia({
        audio: false,
        video: true
      })
      .then(stream => {
        this.myvideo.nativeElement.srcObject = stream;
        this.drawCanvas();
        //this.myvideo.nativeElement.play();
      });
  }

  drawCanvas() {
    this.mycanvas.nativeElement.width = this.myvideo.nativeElement.clientWidth;
    this.mycanvas.nativeElement.height = this.myvideo.nativeElement.clientHeight;
    this.ctx.drawImage(
      this.myvideo.nativeElement.srcObject,
      0,
      0,
      this.mycanvas.nativeElement.width,
      this.mycanvas.nativeElement.height
    );
  }
}
<div>
  <canvas #mycanvas id="mycanvasid"></canvas>
  <video #myvideo></video>
</div>

【问题讨论】:

  • 不要在ngOnInit 中运行drawOnCanvas,而是在ngAfterViewInit 中尝试。当ngOnInit 触发时,视图和元素还没有准备好。
  • 对不起,我刚刚更新了代码(在我粘贴之前有一个旧副本)我实际上已经尝试过 ngAfterViewInit 并且(现在)仍然遇到同样的错误。

标签: angular html5-canvas html5-video


【解决方案1】:

您似乎没有开始直播...

我正在使用它

html

  <div id="inner_video" >
      <video #video id="video" autoplay></video>
  </div>

  <canvas #canvas id="canvas" width="1024" height="768">canvas</canvas>      

ts

 @ViewChild("video")
  public video: ElementRef;

  @ViewChild("canvas")
  public canvas: ElementRef;

  ngOnInit() {
    if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
      navigator.mediaDevices.getUserMedia({ video: true }).then(stream => {
          this.video.nativeElement.srcObject = stream;
          this.video.nativeElement.play();
      });
    }
  } 

捕获代码

 capture(event) {
    event.preventDefault();
    var context = this.canvas.nativeElement.getContext("2d").drawImage(this.video.nativeElement, 0, 0, 1024, 768);
    var picture: any = this.canvas.nativeElement.toDataURL("image/png");
    ...
  }

【讨论】:

  • 感谢您的反馈。我之前一直在启动流,但将其注释掉。我只是把它放回去(实际上将代码重构为您上面的代码)但是当我尝试运行 drawCanvas 方法时出现以下错误... Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value不是类型...我尝试将该方法的调用拉到一个“按钮”中,只是为了看看它是否是一些元素没有时间加载的问题,但我仍然得到同样的错误。
  • 不应该在您播放语句之后绘制画布吗?此外,getContext 必须相同,之后(无论如何,我猜你是按照你所说的“重构”来做的)
  • 当我回复时,我只看到了您回复的一小部分(我想我可能在您仍在编辑代码 sn-p 时做出了回复!我现在正在使用您的完整代码回复,非常感谢您抽出宝贵时间回复。很快就会得到结果。
  • 还要注意它应该是this.myvideo.nativeElement 而不是this.myvideo.nativeElement.srcObject
猜你喜欢
  • 2017-05-06
  • 2021-01-13
  • 1970-01-01
  • 2014-09-18
  • 1970-01-01
  • 2021-08-19
  • 2013-05-10
  • 2016-09-24
相关资源
最近更新 更多