How To Capture Photo Using Ngx-webcam In Angular 12

In this article, we will learn how to capture a photo using an ngx-webcam in Angular 12.

What is ngx-webcam?

ngx-webcam library is used to capture images from our mobile or desktop browser. This library provides a simple angular webcam component and it is very simple to use. The one component gives you full control and lets you take snapshots via actions and event bindings. Plug-and-play This library contains a single module that can be imported into every standard Angular 13+ project.

ngx-webcam features

1. Webcam live view.
2. Photo capturing.
3. Access to front- and back cameras, if multiple cameras exist.
4. Smartphone compatibility for modern OS (OS must support WebRTC/UserMedia API).
5. Mirrored live-view for user-facing cameras – “selfie view” on phones.
6. Portrait & Landscape mode on smartphones.
7. Capturing lossless pixel image data for better post-processing.


  • Basic knowledge of Angular.
  • Code editor likes VS Code.

First, create a new project in Angular using this command.

ng new AngularCamera

Then open the project in visual code and installs the following package.

npm i ngx-webcam --save

Open the app.module.ts and paste the following code into it.

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

import { WebcamModule } from 'ngx-webcam';

  declarations: [
  imports: [
  providers: [],
  bootstrap: [AppComponent]
export class AppModule { }

Open the app.component.ts and paste the following code into it.

import { Component } from '@angular/core';
import { Subject } from "rxjs/Subject";
import { Observable } from "rxjs/Observable";
import { WebcamImage } from 'ngx-webcam';

  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
export class AppComponent {
  seconds: number | any;
  trigger: Subject<void> = new Subject<void>();

  webcamImage: WebcamImage | any;

  takePicture(): void {
    this.seconds = 3;
    setTimeout(() => {
      this.seconds = 2;
      setTimeout(() => {
        this.seconds = 1
        setTimeout(() => {
          this.seconds = null;
        }, 2000)
      }, 2000)
    }, 2000)

  handleImage(webcamImage: WebcamImage): void {
    this.webcamImage = webcamImage;

  get triggerObservable(): Observable<void> {
    return this.trigger.asObservable();

Open the app.component.html and paste the following code into it.

<div style="text-align:center">
    <webcam [trigger]="triggerObservable" (imageCapture)="handleImage($event)"></webcam>
    <br />
    <div class="takePicture" (click)="takePicture()" title="Take Picture"><i class="fa fa-camera"></i></div>
<div class="snapshot" *ngIf="webcamImage">
  <img [src]="webcamImage.imageAsDataUrl" style="padding-left: 620px;" />

Open the app.component.css and paste the following code into it.

p {
  font-size: 120px;
  font-weight: bold;
.takePicture {
  font-size: 100px;
  cursor: pointer;

Now add this CDN to your main index.html.

<link rel="stylesheet" href="">



In our article, we learn how to capture images from a camera in an Angular 12 application using ngx-webcam. You can customize the style of the camera component as per your needs. You can download the code from my Github repository.

Also, check How To Record Video In Angular 12

1 Comment

  1. Carlos

    Hi, thanks for tis example
    I have a question: How can I save the photo in root storage? For example /app/image.jpg
    I’m getting URL image as Base64, but I don’t find how to convert and save to file. Thank you in advance.


Submit a Comment

Your email address will not be published. Required fields are marked *


Select Categories