Cloudinary Blog

Building a Camera App With WebRTC, Vue.js, and Cloudinary

By
Create a Camera App With WebRTC, Vue.js, and Cloudinary

Since 1999, WebRTC has served as an open-source and free solution for real-time audio and video communications within webpages, eliminating the need to use plugins, native apps, or other third-party software. I first became aware of WebRTC when Google and other browser vendors started supporting it. Many products, including Amazon Chime and Slack, have since jumped on the bandwagon.

webrtc

One notable WebRTC capability is that its API enables you to take pictures with the camera on the device that’s rendering your webpage. In my new role as a developer instructional designer for Cloudinary, I recently built an instructional app, called Vue Camera, that integrates with our upload and delivery services and that can take pictures for upload to my Cloudinary account. This article describes the related components and process.

Vue Camera’s Components

Vue Camera contains two routes:

  • The home route, called Camera, has a component that takes pictures.
  • The Gallery route displays pictures. This route and the Upload feature are disabled until after you’ve specified a cloud name and an unsigned upload preset.

Vue Camera is responsive and works well on desktop or mobile. Even though it can take pictures that face away from the device, its ability to do so depends on the device on which it’s running. For example, I can only take selfies on my MacBook Pro but can take both selfies and outward pictures on my Google Pixel. On its home page, WebRTC states that it does not support all browsers.

The App Framework

The name Vue Camera clues you in on the JavaScript framework for building the app: Vue.js. Additionally, I used many APIs:

  • WebRTC for streaming image data from a device camera
  • Canvas toDataURL for representing formatted images as data
  • Vue.js plugins:
    • Vue-bootstrap for responsive CSS
    • Vuex, an implementation of the Flux container architecture
    • Vue-ls for HTML5 local storage
    • Vue-router for creating routable views
  • Cloudinary’s Upload API for uploading images to a Cloudinary account
  • Cloudinary’s Product Gallery JavaScript function for rendering a responsive display of images in a Cloudinary account

For details, see the complete code on Github.

The Part Played by the APIs

Let’s take a high-level look at how the APIs work together in Vue.js to create Vue Camera. Like many similar apps, Vue Camera performs these tasks:

  • Collects data.
  • Transmits the data to a database or static storage.
  • Displays the data to users in another format.

WebRTC: Capture Data

The WebRTC APIs are asynchronous, promise-based browser calls that discover the source of the data and then capture it from the device they run on. Those APIs are wrapped in a component in Vue Camera.

Cloudinary Upload API: Save Data

Cloudinary offers an upload endpoint that can be requested by many HTTP clients, including Axios, XMLHttpRequest (XHR), jQuery, and Angular’s HttpClient. Depending on the HTTP client, the code handles a callback or a promise. Axios posts data to Cloudinary in the Camera component.

Cloudinary Product Gallery: Display Data

A JavaScript function in Cloudinary renders a responsive set of images. Even though most frameworks work with plugins or npm libraries, this code wraps a JavaScript function call that binds to a Document Object Model (DOM) element from within a framework lifecycle. That way, you can take advantage of code that might not otherwise be readily available to the framework.

You can build apps in the React and Angular frameworks with the same technique. As you move between app frameworks like Vue.js, Angular, and React, you organize, configure, and deliver captured data and render HTML, as appropriate. The frameworks have in common similar integrations with the outside world, following a component-based structure for modular development and ensuring that components are bound to the DOM. The idea is that you as the developer are creating HTML tags with your components, which is why you can integrate API functionalities in other frameworks just as you do with this Vue.js app.

Setup on Cloudinary

Now let’s look at how Vue Camera works. To run the app from the demo site, sign up for a free Cloudinary account and then create an unsigned upload preset to upload your pictures to Cloudinary and view them in the Gallery. Click the info icon (see the screenshot below) for details.

navigation

Here are the steps:

  1. Click the gear icon at the top of the Cloudinary console for the settings.
  2. Click Upload below the top navigation.
  3. Under Upload presets, click Enable unsigned uploads and then click Add upload preset.
  4. Under Upload preset name, type a name for the preset, e.g., my-unsigned-preset.
  5. Under Signing Mode, choose Unsigned from the drop-down menu.
  6. Click Save at the top.

Cloudinary then displays the preset you just created under Upload presets in the next screen. unsigned upload preset

Now open Vue Camera and register your cloud name and the preset. Click the gear icon for the Cloudinary Upload Info form and fill in the two text fields with your cloud name and preset name. Click OK.

preset

Afterwards, Cloudinary displays the Upload button with the Gallery link enabled.

Important: The first time you run Vue Camera on a device, you must grant permission for the app to use the camera.

You can now click Snapshot to take pictures and Upload to post them to your Cloudinary account.

What do your cloud name and preset do behind the scenes? Cloudinary loads them into the Vuex store, making them available for use by all components. Cloudinary also stores them in the HTML5 local storage for one hour, saving you having to reregister them if you leave your browser and come back within that time.

local storage

In essence, the cloud name and preset act as credentials in Vue Camera, providing the information Cloudinary needs for its Upload API to post data. Note that you don’t need credentials to download images to your local drive with Vue Camera. However, to save them to the cloud, you must register your cloud name and preset.

Implementation on WebRTC

The WebRTC code resides in the camera component in a model that includes media, devices, and constraints. The media is in the form of a data stream. The devices are detected and then opened with constraints, thus enabling the flow of media. See the snippet below.

Copy to clipboard
getDevices: async function() {
     if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) {
       return false;
     }
     try {
       let allDevices = await navigator.mediaDevices.enumerateDevices();
       for (let mediaDevice of allDevices) {
         if (mediaDevice.kind === "videoinput") {
           let option = {};
           option.text = mediaDevice.label;
           option.value = mediaDevice.deviceId;
           this.options.push(option);
           this.devices.push(mediaDevice);
         }
       }
       return true;
     } catch (err) {
       throw err;
     }
   }
 },

The HTML code loads with an HTML5 video tag and a hidden canvas tag. WebRTC directs the video stream to the video tag so that when the user clicks Snapshot, WebRTC reads the video frame as a JPEG data URI, ready for handoff to the Cloudinary Upload API. See the snippet below.

Copy to clipboard
 snapshot: function() {
     this.canvas.width = this.video.videoWidth;
     this.canvas.height = this.video.videoHeight;
     this.canvas
       .getContext("2d")
       .drawImage(this.video, 0, 0, this.canvas.width, this.canvas.height);
     this.fileData = this.canvas.toDataURL("image/jpeg");
     this.isPhoto = true;
     this.cameraState = false;
     //remove any hidden links used for download
     let hiddenLinks = document.querySelectorAll(".hidden_links");
     for (let hiddenLink of hiddenLinks) {
       document.querySelector("body").remove(hiddenLink);
     }
   }

Cloudinary Upload API

The uploadToCloudinary function packages the registered cloud name and preset with the file data obtained in the snapshot described above as a FormData object. I posted the data to Cloudinary with the Axios library so the cloud name becomes part of the upload endpoint.

You can organize media on Cloudinary in many ways. One of them is through tags, which, like metadata, can help you identify your assets. To take advantage of that capability, I added a tag named browser_upload, with which the Gallery component pulls only the pictures taken by Vue Camera. See the snippet below.

Copy to clipboard
async function uploadToCloudinary(cloudName, preset, fileData) {
 try {
   let fd = new FormData();
   let url = `https://api.cloudinary.com/v1_1/${cloudName}/image/upload`;
   fd.append("upload_preset", preset);
   fd.append("tags", "browser_upload");
   fd.append("file", fileData);
   let res = await axios({
     method: "post",
     url: url,
     data: fd
   });
   return await res.data;
 } catch (err) {
   throw err;
 }
}

If the upload succeeds, the function that calls uploadToCloudinary notifies the user with a “toast” message. The code below shows such a message that’s five seconds long.

Copy to clipboard
this.$bvToast.toast(
         `Upload to Cloudinary unsuccessful: use settings to provide cloudname and preset`,
         {
           title: "Cloudinary Upload",
           autoHideDelay: 5000,
           appendToast: false
         }
       )

If you’ve entered a nonexistent cloud name or preset, a fail error-message is displayed.

Gallery

Now comes the step to show off the images. Enter Cloudinary’s Product Gallery widget, available as a JavaScript function that binds output to a DOM element, much like what you would do in jQuery or Vanilla JavaScript. So, how to include that function in a framework?

The Gallery component code shows how I “wrapped” the Gallery function in a component's script with a Vue.js lifecycle hook. First, when the component elements are added to the virtual DOM, Vue.js calls the mounted function, gets an instance of the Product Gallery widget, and renders the Product Gallery output to the DOM. Finally, to prevent memory leaks, the function called by the beforeDestroy lifecycle hook destroys the gallery element when exiting the component view. See the snippet below.

Copy to clipboard
mounted() {
   //get cloudname and preset from local storage
   if (this.$ls.get("cloudname")) {
     this.cloudname = this.$ls.get("cloudname");
   }
   if (this.$ls.get("preset")) {
     this.preset = this.$ls.get("preset");
   }
   //if these aren't set don't allow browse
   /*global cloudinary*/
   /*eslint no-undef: "error"*/
   this.myGallery = cloudinary.galleryWidget({
     container: "#images",
     cloudName: this.cloudname,
     mediaAssets: [{ tag: "browser_upload" }]
   });
   this.myGallery.render();
 },

Be sure to add links for the widget CSS and JavaScript to your public/index.html file, which is not part of the webpacked code.

Copy to clipboard
<head>
 <meta charset="utf-8">
 <meta http-equiv="X-UA-Compatible" content="IE=edge">
 <meta name="viewport" content="width=device-width,initial-scale=1.0">
 <link rel="icon" href="<%= BASE_URL %>favicon.ico">
 <title>vue-camera</title>
 <link href="//fonts.googleapis.com/css?family=Roboto:300,400,500,700" rel="stylesheet" type="text/css">
 <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.11.2/css/all.css" integrity="sha384-KA6wR/X5RY4zFAHpv/CnoG2UW1uogYfdnP67Uv7eULvTveboZJg0qUpmJZb5VqzN" crossorigin="anonymous">
 <link rel="stylesheet" href="./css/main.css">

</head>

<body>
 <noscript>
   <strong>We're sorry but vue-camera doesn't work properly without JavaScript enabled. Please enable it to
     continue.</strong>
 </noscript>
 <div id="app"></div>
 <script src="https://product-gallery.cloudinary.com/all.js"></script>
 <!-- built files will be auto injected -->
</body>

The Product Gallery widget then becomes responsive, as shown in the screenshot below the desktop view.

gallery

The image below shows the mobile view along with a carousel for maintaining a responsive layout. mobile gallery

Conclusion

The Vue Camera app serves as instructional code only and is not intended to replace Google Photos or Mac’s Photo Booth. You can add more capabilities, e.g., device constraints that change the size of the video-stream frame and a feature that shares the Cloudinary link. You can also use the Cloudinary Vue.js SDK plugin to transform images.

If you are running Vue Camera on both desktop and mobile, feel free to use it as a tool for self-reflection. The image below shows me taking a selfie in the desktop version with the mobile version.

laptop As a side note, I presented the material from this article at the API Meetup in Seattle, Washington in November 2019. Gratifyingly, it elicited a lot of interest from the audience.

Recent Blog Posts

Our $2B Valuation

By
Blackstone Growth Invests in Cloudinary

When we started our journey in 2012, we were looking to improve our lives as developers by making it easier for us to handle the arduous tasks of handling images and videos in our code. That initial line of developer code has evolved into a full suite of media experience solutions driven by a mission that gradually revealed itself over the course of the past 10 years: help companies unleash the full potential of their media to create the most engaging visual experiences.

Read more
Direct-to-Consumer E-Commerce Requires Compelling Visual Experiences

When brands like you adopt a direct–to-consumer (DTC) e-commerce approach with no involvement of retailers or marketplaces, you gain direct and timely insight into evolving shopping behaviors. Accordingly, you can accommodate shoppers’ preferences by continually adjusting your product offering and interspersing the shopping journey with moments of excitement and intrigue. Opportunities abound for you to cultivate engaging customer relationships.

Read more
Automatically Translating Videos for an International Audience

No matter your business focus—public service, B2B integration, recruitment—multimedia, in particular video, is remarkably effective in communicating with the audience. Before, making video accessible to diverse viewers involved tasks galore, such as eliciting the service of production studios to manually dub, transcribe, and add subtitles. Those operations were costly and slow, especially for globally destined content.

Read more
Cloudinary Helps Minted Manage Its Image-Generation Pipeline at Scale

Shoppers return time and again to Minted’s global online community of independent artists and designers because they know they can count on unique, statement-making products of the highest quality there. Concurrently, the visual imagery on Minted.com must do justice to the designs into which the creators have poured their hearts and souls. For Minted’s VP of Engineering David Lien, “Because we are a premium brand, we need to ensure that every single one of our product images matches the selected configuration exactly. For example, if you pick an 18x24 art print on blue canvas, we will show that exact combination on the hero images in the PDF.”

Read more
Highlights on ImageCon 2021 and a Preview of ImageCon 2022

New year, same trend! Visual media will continue to play a monumental role in driving online conversions. To keep up with visual-experience trends and best practices, Cloudinary holds an annual conference called ImageCon, a one-of-a-kind event that helps attendees create the most engaging visual experiences possible.

Read more