Accessing Front and Rear camera on a Mobile browser in Emberjs App

Search for a command to run...

No comments yet. Be the first to comment.
Motivation Recently, I tried to learn some low-level system programming stuff. I am a Mac user, and I thought that everything that works on Linux should also work on Mac. After all, Mac is a Unix-based system 😊. I guess we all heard this. Oh boy! I ...

Problem Statement While working on a Nestjs project, I encountered a weird problem related to the database column. I was trying to insert a record into a MySQL table using TypeORM. The error I was experiencing stated that a specific column “cannot be...

Recently, I was exploring design patterns courses on my LinkedIn Learning subscription. I came across a course, Node.js: Design Patterns by Alex Banks. It is a wonderful, easy-to-understand course. I started with the Builder Pattern, and the explanat...

Suppose you are working on a table in a LiveView project. This table has limited static data of not more than one page (you can avoid questions about pagination in the comment section 😊). From a user's point of view, it becomes hard to look into the...

Recently, while working on one of my personal Elixir projects. I came across a scenario where I needed to make some changes to the database schema.The changes mainly revolve around adding and removing indexes due to changes in the business requiremen...

Suppose, you are having a web application that has the feature to open a camera. For a Laptop or desktop machine, it is fine to access the one camera you are having. What if you are accessing the same web app on mobile or tablet, wouldn't it be cool to access both front and rear cameras. In this blog, we are going to create an Ember component using which will have features to access both Front and Rear cameras.
ember new my-camera-access.cd my-camera-access.ember-modifier by running ember install ember-modifier in your terminal.app/templates/application.hbs and remove ember generated boilerplate HTML code. Just leave {{outlet}}{{outlet}}
ember server and your application will run at localhost:4200. You can see a blank page.dual-camera component.ember g component dual-camera command in your terminal.app/components/dual-camera.hbs. In the latest Ember version, it may skip the component js class. You can add one by running ember generate component-class dual-camera. This will add app/components/dual-camera.js.<h1> Camera Test</h1>
application.hbs. <DualCamera />
{{outlet}}
Camera Test. mediaDevices.getUserMedia method to access the video feeds from the camera and play it in the browser.getUserMedia method prompts the user for permission to access the camera. You can see Allow/Block options.navigator.mediaDevices.getUserMedia({video: true}). //we will add other options later.
video tag in our component view i.e app/components/dual-camera.hbs.<video autoplay id="video-player"></video>
autoplay property. It still does not have video streams.did-insert hook of the glimmer component which will be called from did-insert modifier in the video.<video autoplay id="video-player" {{did-insert this.onRender}}></video>
did-insert will call onRender action with video tag as the parameter.@action
onRender(videoPlayer) {
videoPlayer.play();
this.video = videoPlayer; // we are maintaining `videoPlayer` instance in the class to use it later.
this.initializeCamera();
}
onRender method will be called, which will have access to the video tag. We can use it to play the stream.initializeCamera method.async initializeCamera() {
......
......
const videoStream= await navigator.mediaDevices.getUserMedia(this.videoOpts);
this.videoStream = videoStream;
this.video.srcObject = videoStream;
}
getUserMedia returns stream which is assigned to video.srcObject. video.srcObject is nothing but video player instance.getUserMedia need to pass facingMode options with a value of environment by default its value is user.<button {{action this.changeCamera}}>Change</button>
changeCamera action to change the camera view.@action
changeCamera() {
this.useFrontCamera = !this.useFrontCamera;
this.initializeCamera();
}
useFrontCamera to initialize the camera with facingMode options of user and environment. Something like below,async initializeCamera() {
const cameraType = this.useFrontCamera ? "user" : "environment";
this.videoOpts.video.facingMode = cameraType;
......
......
......
}
Final Implementation of the javascript code is below
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';
export default class DualCameraComponent extends Component {
@tracked videoStream = "";
@tracked useFrontCamera = true;
@tracked videoOpts = {
video: {
width: {
min: 1280,
ideal: 1920,
max: 2560
},
height: {
min: 720,
ideal: 1080,
max: 1440
}
}
};
async initializeCamera() {
const cameraType = this.useFrontCamera ? "user" : "environment";
this.videoOpts.video.facingMode = cameraType;
const videoStream = await navigator.mediaDevices.getUserMedia(this.videoOpts);
this.videoStream = videoStream;
this.video.srcObject = videoStream;
}
@action
onRender(videoPlayer) {
videoPlayer.play();
this.video = videoPlayer;
this.initializeCamera();
}
@action
changeCamera() {
this.useFrontCamera = !this.useFrontCamera;
this.initializeCamera();
}
}
I hope you like this easy implementation of accessing the front and rear cameras in the Ember application. If you have any questions then please comment below.