【发布时间】:2019-01-21 11:02:33
【问题描述】:
我正在使用 ng2-image 裁剪器,因为它不采用“src”属性它采用“image”属性。
因此,如果我提供 dataUrl,则图像不会显示在裁剪器中。
我正在使用相机拍摄图像,并从中获取 base64 图像。
想把base64 dataUrl转换成图片,这样就可以在
//component.html
<div *ngSwitchCase="'camera'">
<mat-dialog-actions>
<button mat-raised-button class="capture" (click)="capture()">Take Photo</button>
<button mat-raised-button mat-dialog-close class="cancel" (click)="closeCamera()" (click)="openDialog()">Cancel</button>
</mat-dialog-actions>
<canvas #canvas id="canvas" width="400" height="400"></canvas>
</div>
<img-cropper #cropper [image]="data" [settings]="cropperSettings"></img-cropper>
<span class="result rounded" *ngIf="data">
<img src="{{data}}" [width]="cropperSettings.croppedWidth"
[height]="cropperSettings.croppedHeight">
</span>
从相机中获取图像并在画布中绘制并将其推送到 dataUrl
// component.ts
public async capture() {
var context = this.canvas.nativeElement.getContext("2d").drawImage(this.video.nativeElement, 0, 0, 400, 400);
await this.captures.push(this.canvas.nativeElement.toDataURL("image/png"));
this.state = 'photo';
this.data = this.canvas.nativeElement.toDataURL("image/png");
localStorage.setItem('webcam', this.data);
}
【问题讨论】:
-
如果您有一个数据 url,并且希望它显示在
img元素中,只需将 src 设置为数据 url。您无需将其转换为图像。 -
我需要在
中设置图像。它不是在获取数据 url,而是在获取图像 -
即便如此,只需将图像用作
的提要。只需创建一个图像,将 src 设置为数据 URL,然后将该图像用于您的 。无论如何,请检查我的答案,如果您绝对将数据 URL 转换为有用的东西(例如 blob),应该会让您朝着正确的方向前进。
标签: angular typescript html5-canvas webcam-capture