- Check out the full code report
- Try out the demo
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.
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 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.
- 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
For details, see the complete code on Github.
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.
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 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.
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.
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.
Here are the steps:
- Click the gear icon at the top of the Cloudinary console for the settings.
- Click Upload below the top navigation.
- Under Upload presets, click Enable unsigned uploads and then click Add upload preset.
- Under Upload preset name, type a name for the preset, e.g.,
- Under Signing Mode, choose Unsigned from the drop-down menu.
- Click Save at the top.
Cloudinary then displays the preset you just created under Upload presets in the next screen.
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.
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.
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.
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.
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.
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.
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.
If you’ve entered a nonexistent cloud name or preset, a fail error-message is displayed.
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.
public/index.html file, which is not part of the webpacked code.
The Product Gallery widget then becomes responsive, as shown in the screenshot below the desktop view.
The image below shows the mobile view along with a carousel for maintaining a responsive layout.
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.
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.