Image capture in LWC

In most cases, we may need to use media-level features such as a camera and microphone. Salesforce Supports user media capabilities to do that. We can utilize these features in Lightning Web Components.

We can utilize the Javascript Navigator feature in the Lightning Web Component to access the media features.

Step 1: Before going to start, please make sure that Lightning Web Security is enabled in the Org.

Go to Setup → Session Setting → Enable Lightning Web Security.

Step 2: Create a Lightning Web Component and give its name to accessCamera.
Step 3 : To Use the navigator. mediaDevices methods to access the camera.

navigator.mediaDevices.getUserMedia({video: true, audio: false})

Step 4: Use the below HTML code to embed the camera frame.

       <div class="camera-wrapper">
           <lightning-button label="Start Video" 
              class="slds-m-left_xx-small" onclick={initCamera}>

        <lightning-button label="Stop Video" class="slds-m-left_xx-small" 
        <lightning-button label="Capture Image"   
             class="slds-m-left_xx-small" onclick={captureImage}>
    <div class="slds-grid slds-gutters">
        <div class="slds-col">
            <video class="videoElement" autoplay></video>
            <canvas class="slds-hide canvas"></canvas>
        <div class="slds-col">
            <img src="" class="slds-hide imageElement" alt="captured  

Step 5: In the JS file, create more methods to initiate the camera and capture the image.

 import { LightningElement } from 'lwc';

   export default class AccessCamera extends LightningElement {

    renderedCallback() {
        this.videoElement = this.template.querySelector('.videoElement');
        console.log('video element>>',this.videoElement);
        this.canvasElement = this.template.querySelector('.canvas');

    async initCamera() {
        if (navigator.mediaDevices && navigator.mediaDevices) {
            try {
                this.videoElement.srcObject = await 
      navigator.mediaDevices.getUserMedia({video: true, audio: false});
            } catch (error) {
                console.error('Error accessing camera: ',  
        } else {
            console.error('getUserMedia is not supported in this  

    async captureImage() {

        if(this.videoElement && this.videoElement.srcObject !== null) {
            this.canvasElement.height = this.videoElement.videoHeight;
            this.canvasElement.width = this.videoElement.videoWidth;
            const context = this.canvasElement.getContext('2d');
            context.drawImage(this.videoElement, 0, 0,  
            this.canvasElement.width, this.canvasElement.height);
            const imageData = this.canvasElement.toDataURL('image/png');
            const imageElement =
            imageElement.setAttribute('src', imageData);

    async stopCamera(){
        const video = this.template.querySelector(".videoElement");
        video.srcObject.getTracks().forEach((track) => track.stop());
        video.srcObject = null;

        const imageElement = this.template.querySelector('.imageElement');
        imageElement.setAttribute('src', "");

Step 6: Give access to the Lightning tab on meta data file.

   <?xml version="1.0" encoding="UTF-8"?>

Step 7: Go to setup → Tabs and create a custom tab for the Lightning web component we created.

Step 8: Once you create the tab, open the tab and test the camera access from the component.