使用 Angular 中的视频和 canvas 元素
Working with video and canvas element in Angular
我正在尝试获取一个流并按照某些 Google 开发人员教程中的建议使用 drawImage 渲染它。我不断收到错误消息:
TypeError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type '(CSSImageValue or HTMLImageElement or SVGImageElement or HTMLVideoElement or HTMLCanvasElement or ImageBitmap or 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>
您似乎没有开始直播...
我在用
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");
...
}
我正在尝试获取一个流并按照某些 Google 开发人员教程中的建议使用 drawImage 渲染它。我不断收到错误消息:
TypeError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type '(CSSImageValue or HTMLImageElement or SVGImageElement or HTMLVideoElement or HTMLCanvasElement or ImageBitmap or 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>
您似乎没有开始直播...
我在用
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");
...
}