Home Salesforce Capture Image using WebCAM in Lightning Component

Capture Image using WebCAM in Lightning Component

by Dhanik Lal Sahni

In real time projects, some time we have requirement to capture customer/user image in application processing at real time.  In this article we will see, how to capture image using lightning component.

For capturing image, we can use browser’s inbuilt WebRTC functionality to access the camera on a laptop, computer or mobile phone with WebRTC support and take a photo with it.

WebRTC (Web Real-Time Communications) is a technology which enables Web applications and sites to capture and optionally stream audio and/or video media, as well as to exchange arbitrary data between browsers without requiring an intermediary.

We will use Media Capture and Streams API of WebRTC to get streaming audio and video data.  MediaDevices.getUserMedia() method will first ask permission to access your WebCam and audio device. If permission is granted to access video camera then it will return stream as promise otherwise it will throw NotAllowedError.

navigator.mediaDevices.getUserMedia({video: true, audio: false})
        .then(function(stream) {
            video.srcObject = stream;
            video.play();
        })
        .catch(function(err) {
            console.log("An error occurred: " + err);
});

Once stream is captured it will play in video element using video.play() function.

<video id="video">Video stream not available.</video>

canplay event of above video element alerts that the video is ready to start playing. We can capture video information using this element like video player’s height and width etc.

  video.addEventListener('canplay', function(ev){
            if (!streaming) {
                height = video.videoHeight / (video.videoWidth/width);
                
                // Firefox currently has a bug where the height can't be read from
                // the video, so make assumptions if this happens.
                
                if (isNaN(height)) {
                    height = width / (4/3);
                }
                canvas.setAttribute('width', width);
                canvas.setAttribute('height', height);
                streaming = true;
            }
        }, false);

We have used canplay event to set canvas properties. Image will be draw in that canvas element from video.

var context = canvas.getContext('2d');
if (width && height) {
    canvas.width = width;
    canvas.height = height;
    context.drawImage(video, 0, 0, width, height);
                
    var data = canvas.toDataURL('image/png');
    photo.setAttribute('src', data);
}

canvas.toDataURL method returns a data URI containing a representation of the image in the format which we specified in type parameter. This data URI we can save in any salesforce record for application processing.

Complete lightning component Code :

Complete Apex Code :

To save captured image in file or attachement object, we have to use apex. Both type of methods are written in ImageController apex class, to store in file or attachement object. Based on your requirement call appropriate method from lighting component method (savePhoto).

Demo:

Reference:

https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API/Taking_still_photos

Updated: On 8-Apr-2021 for Saving captured image.

You may also like

37 comments

Utkarsh Maurya April 16, 2020 - 7:19 pm

This is Working on mobile phones but I want to open the back camera of my phone not front

Reply
Dhanik Lal Sahni May 3, 2020 - 8:42 am

Hello Utkarsh,

You have to use setting { audio: true, video: { facingMode: { exact: “environment” } } } in function navigator.mediaDevices.getUserMedia({video: true, audio: false}).

Refer API link at https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia

Thank You,
Dhanik

Reply
Vineet Kaul May 5, 2020 - 10:40 am

Hi Dhanik,

This is an excellent post. I was looking at something similar for my Einstein project. I work for the Platforms & Einstein team at Salesforce.

I was trying to use your component but it gives a blank on the screen. Would you know how to debug it?

Regards,
Vineet
vkaul@salesforce.com

Reply
Dhanik Lal Sahni May 7, 2020 - 1:53 pm

Hello Vineet,

Have you checked lightning component version? It should be 39 or you can use VF page for this. Locker service is blocking navigator.mediaDevices so you have to change version to 39.

Thank You,
Dhanik
http://salesforcecodex.com/

Reply
S May 13, 2020 - 9:42 pm

I am facing the following error: cannot read property ‘getUserMedia’ of undefined.can u help?

Reply
Dhanik Lal Sahni May 14, 2020 - 5:50 pm

Hey S,

Have you changed Lightning component version to 39. As after this locker service create issue and you will get undefined or null.

Thank You,
Dhanik

Reply
Son May 26, 2020 - 7:47 pm

Do we save the image in the org?

Reply
Dhanik Lal Sahni May 30, 2020 - 9:37 pm

Hello Son,

We can save image in any Salesforce record. Check takepicture function, data is having image.You can store it in org as well.

Thank You,
Dhanik

Reply
Sokka May 30, 2020 - 1:44 pm

Sir,
DO we save the image in the org?
Thanks,

Reply
Dhanik Lal Sahni May 30, 2020 - 9:19 pm

Hello Sonakshi,

We can save image in any Salesforce record. Check takepicture function, data is having image.You can store it in org as well.

Thank You,
Dhanik

Reply
Merajul June 16, 2020 - 2:35 pm

Thnx Bro..
It helps a lot, I mean it.

Reply
Dhanik Lal Sahni June 23, 2020 - 12:17 am

Thank You Merajul.

Reply
suresh August 10, 2020 - 2:40 am

Hi Dhanik,
its very helpful to start on this usecase, but i am getting below error do you got any idea.

[Cannot read property ‘getUserMedia’ of undefined]

Reply
Dhanik Lal Sahni August 11, 2020 - 6:51 pm

Hey Suresh,

Please check API version. It is only supporting till 39.

Thank you,
Dhanik

Reply
Vince September 28, 2020 - 2:59 pm

Does it work on iPhones? I’m using the Salesforce App and it doesn’t work on there but it does work on desktop browser.

Reply
Dhanik Lal Sahni September 29, 2020 - 8:49 am

Yes, it is not working always in mobile. Some time it works for front camera but not with back camera. You can use mobile native code to get camera access.

Thank You,
Dhanik

Reply
Stepan October 5, 2020 - 2:20 pm

hi Dhanik. thank you a lot for this article. could you please say if it is possible to use this element (or another one) to record video from the web cam?

Reply
Dhanik Lal Sahni October 7, 2020 - 10:30 am

Hello Stepan,

You can use video element for this purpose. See documentation https://developers.google.com/web/fundamentals/media/recording-video for this. Only problem will be – you have to use that in lightning aura component and make component api version 39.

Thank You,
Dhanik

Reply
U December 25, 2020 - 2:40 pm

Can someone please share the code to save the captured image as attachment to a record?

Reply
Dhanik Lal Sahni December 26, 2020 - 10:38 am

Hey Urvi,

See post https://salesforce.stackexchange.com/questions/46486/create-attachment-in-apex-to-store-encoded-signature for saving record.

You can use data element (line#74) of captureImageWebCamController.js to store value in record.

Thank You,
Dhanik

Reply
U December 29, 2020 - 5:55 pm

Hi,
I tried using this-
Attachment a = new Attachment( ParentId = parentid,Name=name+’.png’, Body=EncodingUtil.base64Decode(data), ContentType=’image/png’);
insert a;
in apex function where data is from line 74, but it doesn’t insert record or show the attachment in notes and attachments related list either.

Reply
Amit December 28, 2020 - 5:24 pm

Hi Dhanik,

Thank you for your informative article. Can you please let me know if there is a way to give a user the choice whether to use the front or the back camera?
I need to support both the back camera and front camera so I support both on the mobile and on the PC.

Reply
Amit January 28, 2021 - 2:55 pm

Hi everybody,

Anyone get in trouble when they allow the iPhone to capture a picture, but see a black screen, but if they save the picture, the original picture is saved?
I’m talking about iPhones in the newer versions (such as 11 / SE)

Reply
Ramakrishna February 8, 2021 - 3:11 pm

If i wanted to use more than api 39 then what should i do?

Reply
Dhanik Lal Sahni February 11, 2021 - 12:16 am

Hey Ramakrishna,

Right now it is not possible. Locker service will block this api so you have to use this only.

Thank You,
Dhanik

Reply
Dhanik Lal Sahni February 14, 2021 - 9:38 pm

Thank you amit for solution for iPhone support. I have updated code for others.

Reply
Mugdha April 7, 2021 - 1:17 pm

Hello Dhanik,
Thanks a lot for this solution. Could you please provide js controller code for saving a document.
apex controller:
Attachment record = new Attachment(ParentId = parentID, Body = EncodingUtil.base64Decode(data), ContentType = ‘image/jpeg’);
insert record;

Reply
Dhanik Lal Sahni April 8, 2021 - 1:43 am

Hello Mugdha, I have updated code to save in attachement and file object. Use updated code to save it in record.

Thank You,
Dhanik

Reply
Prisila April 7, 2021 - 4:37 pm

please post code for saving image in attachment / contentDocument

Reply
Dhanik Lal Sahni April 8, 2021 - 1:43 am

Hello Prisila, I have updated code to save in attachement and file object. Use updated code to save it in record.

Thank You,
Dhanik

Reply
Prisila April 12, 2021 - 12:19 am

Thanks Dhanik. It was quite helpful. 🙂

Reply
Suzane June 22, 2021 - 5:48 pm

Hello Dhanik,

Could you please tell what will be the size of captured image? Can we have control over it? Can we control and set size in KB?

Reply
Dhanik Lal Sahni July 3, 2021 - 1:43 pm

Hello Suzane, Size is dependent on area covered by CAM. You can control but you have to use some JS code for this.

Checkout some useful link here https://stackoverflow.com/questions/2303690/resizing-an-image-in-an-html5-canvas.

Thank You,
Dhanik

Reply
Vikas March 3, 2023 - 2:52 pm

Hi I am getting this error event after using api 39. Action failed: c:captureImageWebCam$controller$doInit [Cannot read properties of undefined (reading ‘getUserMedia’)]

Reply
Dhanik Lal Sahni March 12, 2023 - 10:42 am

Hello Vikas,

This code will only work till the Aura version is 39. After that, it will not support. Please check the API version or implement in the VF page.

Thank You,
Dhanik

Reply
Romil June 23, 2023 - 8:07 pm

Hi, this code is working fine for admin user but for other users it is not saving the image, I have used the above code snippet only, please can you help me with this

Reply
Dhanik Lal Sahni June 28, 2023 - 10:36 pm

Hello Romil,

This looks CRUD permission issue. Please check permission of the user once. If still issue, ping me on my linked in profile. We will connect and resolve your issue.

Thank You,
Dhanik

Reply

Leave a Comment