DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Creating Scrolling Text With HTML, CSS, and JavaScript
  • Mastering SSR and CSR in Next.js: Building High-Performance Data Visualizations
  • How to Create a Pokémon Breeding Gaming Calculator Using HTML, CSS, and JavaScript
  • How to Convert HTML to DOCX in Java

Trending

  • DZone's Article Submission Guidelines
  • Using Python Libraries in Java
  • Vibe Coding With GitHub Copilot: Optimizing API Performance in Fintech Microservices
  • Intro to RAG: Foundations of Retrieval Augmented Generation, Part 1
  1. DZone
  2. Coding
  3. Languages
  4. You Can Access a User’s Camera With Just HTML

You Can Access a User’s Camera With Just HTML

The other day I came across a cool HTML attribute I had never heard of before; capture. So I decided to make a video and blog post about it.

By 
Austin Gil user avatar
Austin Gil
DZone Core CORE ·
Sep. 06, 22 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
9.4K Views

Join the DZone community and get the full member experience.

Join For Free

The other day I came across a cool HTML attribute I had never heard of before; capture. So I decided to make a video and blog post about it.

Here’s a video format if you prefer that:

You can put the capture attribute on inputs with the type of file, and you can give it a value of “user” or “environment“.

The interesting thing about the capture attribute is for users coming to your website on a mobile device. If they interact with that input, instead of opening up the default file picker, it will actually open up one of their cameras. It could be the front-facing camera or the back-facing camera, depending on the value.

If you set the value to “user“, it will use the user-facing or front-facing camera and or microphone. And if you set it to “environment“, it will use the outer facing or back facing camera and or microphone.

To test it out myself, I created an index.html file that looked like this:

HTML
 
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width,initial-scale=1" />
  <style>
    * {
      font-size: 1.5rem;
    }
  </style>
</head>

<body>
  <label for="environment">Capture environment:</label>
  <br>
  <input
    type="file"
    id="environment"
    capture="environment"
    accept="video/*"
  >
  <br><br>
  <label for="user">Capture user:</label>
  <br>
  <input
    type="file"
    id="user"
    capture="user"
    accept="image/*"
  >
</body>

</html>

The only relevant part of the HTML is the two inputs. They both have a capture attribute and they both have an accept attribute. One is set to capture the “environment” with any type of video. The other is set to capture the “user” with any type of image.

The capture attribute doesn’t really do anything interesting on a desktop. When I click the inputs, I see that it opens up the file picker.

But here’s the interesting part…

If I bring up that page on my phone and click the first input, my camera opens in the video recording mode. When I click the second input, my phone opens up the camera in image capture mode (for some reason, my phone doesn’t differentiate between front or back facing cameras though).

When you actually snap a photo or video, that file is sent to the HTML input.

How cool is that!?!?

Now, anytime we talk about new web APIs or features, we have to discuss the compatibility and if we look at the support on caniuse.com, it doesn’t look great…

Screenshot of caniuse.com/html-media-capture showing no support for desktop browsers.

But it actually makes sense because all of those unsupported browsers are actually desktop browsers where there isn’t a front-facing or back-facing camera. So it makes sense to only open up the file picker.

But all the supported browsers are mobile browsers. So that feature is good to go.

The other cool thing is even if a user comes to your application with an unsupported browser, it’s just going to fall back to the default file picker UI.

Progressive Enhancement FTW!!!

So that’s the HTML capture attribute. It’s a pretty cool way to add a nicer user experience if you know that your mobile users are going to be taking a picture or a video or a voice recording at the time of upload.

Thank you so much for reading. If you liked this article, please share it. It's one of the best ways to support me.

HTML

Published at DZone with permission of Austin Gil. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Creating Scrolling Text With HTML, CSS, and JavaScript
  • Mastering SSR and CSR in Next.js: Building High-Performance Data Visualizations
  • How to Create a Pokémon Breeding Gaming Calculator Using HTML, CSS, and JavaScript
  • How to Convert HTML to DOCX in Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!