Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    • Top 10 PMD Issues Salesforce Developers Should Focus on in Apex
    • How to Use Graph API for Outlook-Salesforce Connection
    • Enhancing Performance with File Compression in Apex
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Sunday, May 18
    • Home
    • Architecture
    • Salesforce
      • News
      • Apex
      • Integration
      • Books Testimonial
    • Questions
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Integration Posts
    • Downloads
    • About Us
      • Privacy Policy
    SalesforceCodex
    Home»Salesforce»Capture Image using WebCAM in Lightning Component

    Capture Image using WebCAM in Lightning Component

    Dhanik Lal SahniBy Dhanik Lal SahniSeptember 19, 2019Updated:April 8, 202137 Comments2 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Capture Image using WebCAM in Lightning Component
    Share
    Facebook Twitter LinkedIn Pinterest Email

    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.

    lightning WebRTC
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleIntegrate Salesforce with WhatsApp using Twilio API
    Next Article Integrate Salesforce Stackexchange using Lightning Web Component and Apex
    Dhanik Lal Sahni
    • Website

    Related Posts

    By Dhanik Lal Sahni6 Mins Read

    Top 10 Salesforce Flow Features of Salesforce Summer ’25

    May 11, 2025
    By Dhanik Lal Sahni6 Mins Read

    Unlock the Power of Vibe Coding in Salesforce

    April 30, 2025
    By Dhanik Lal Sahni5 Mins Read

    How to Implement Dynamic Queueable Chaining in Salesforce Apex

    April 21, 2025
    View 37 Comments

    37 Comments

    1. Utkarsh Maurya on 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 on 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
    2. Vineet Kaul on 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 on 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
    3. S on 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 on 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
    4. Son on May 26, 2020 7:47 pm

      Do we save the image in the org?

      Reply
      • Dhanik Lal Sahni on 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
    5. Sokka on May 30, 2020 1:44 pm

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

      Reply
      • Dhanik Lal Sahni on 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
    6. Merajul on June 16, 2020 2:35 pm

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

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

        Thank You Merajul.

        Reply
    7. suresh on 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 on August 11, 2020 6:51 pm

        Hey Suresh,

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

        Thank you,
        Dhanik

        Reply
    8. Vince on 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 on 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
    9. Stepan on 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 on 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
    10. U on 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 on 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 on 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
    11. Amit on 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
    12. Amit on 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
    13. Ramakrishna on February 8, 2021 3:11 pm

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

      Reply
      • Dhanik Lal Sahni on 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
    14. Dhanik Lal Sahni on February 14, 2021 9:38 pm

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

      Reply
    15. Mugdha on 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 on 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
    16. Prisila on April 7, 2021 4:37 pm

      please post code for saving image in attachment / contentDocument

      Reply
      • Dhanik Lal Sahni on 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 on April 12, 2021 12:19 am

          Thanks Dhanik. It was quite helpful. 🙂

          Reply
    17. Suzane on 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 on 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
    18. Vikas on 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 on 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
    19. Romil on 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 on 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 Reply Cancel Reply

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (110) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) einstein (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (29) Lightning web component (61) lwc (50) named credential (8) news (4) optimize apex (3) optimize apex code (4) Permission set (4) Queueable (9) rest api (23) S3 Server (4) salesforce (140) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (110) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) einstein (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (29) Lightning web component (61) lwc (50) named credential (8) news (4) optimize apex (3) optimize apex code (4) Permission set (4) Queueable (9) rest api (23) S3 Server (4) salesforce (140) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Facebook X (Twitter) Instagram Pinterest YouTube Tumblr LinkedIn Reddit Telegram
    © 2025 SalesforceCodex.com. Designed by Vagmine Cloud Solution.

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Our website is made possible by displaying online advertisements to our visitors. Please support us by disabling your Ad Blocker.