Skip to content

Commit

Permalink
Fix back camera not working on forked webcam directive ITO-70 #dev-ac…
Browse files Browse the repository at this point in the history
…cept (#2)

* add facingMode constraint

* Update app/scripts/webcam.js

* update version

* Create .npmrc

* package and min file updates

* remove dupe file, redo version

* more readme cleanup

* missed 2
  • Loading branch information
Derek McCrae authored Jun 17, 2022
1 parent b624942 commit 5779bea
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 87 deletions.
13 changes: 0 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ You can easily add it as a module to your own app.

A complete example can be found at [http://jonashartmann.github.io/webcam-directive](http://jonashartmann.github.io/webcam-directive) (*gh-pages*)

## Download
[![NPM](https://nodei.co/npm/webcam.png?compact=true)](https://nodei.co/npm/webcam/)

#### Using the command line
```shell
npm install @ubreakifix/webcam
Expand All @@ -20,16 +17,6 @@ npm install @ubreakifix/webcam
"@ubreakifix/webcam": "3.2.13"
```

#### There is a tag for each version, with the built javascript file inside the _dist_ directory
Ex.: dist/webcam.min.js

## Installation

#### Using script tag
```html
<script type="text/javascript" src="webcam.min.js"></script>
```

## Usage

#### Add module "webcam" as dependency
Expand Down
102 changes: 30 additions & 72 deletions app/scripts/webcam.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
*
* (c) Jonas Hartmann http://jonashartmann.github.io/webcam-directive
* License: MIT
*
* @version: 3.2.1
*/
'use strict';

Expand Down Expand Up @@ -146,79 +144,39 @@ angular.module('webcam', [])
return;
}

var mediaConstraint = { video: true, audio: false };
var videoOptions;

navigator.mediaDevices.enumerateDevices().then(function (devices) {
var sourceId = null;
var source = null;
var backCam = null;
$scope.config.sourceList = [];
$scope.config.sourceDevice = {};

// enumerate all devices
devices.forEach(function (device) {
// if there is still no video input, or if this is the rear camera
if (device.kind === 'videoinput') {
$scope.config.sourceList.push(device);
if (device.label.indexOf('back') !== -1) {
backCam = device;
}
sourceId = device.deviceId;
source = device;
}
});
// we didn't find any video input
if (!sourceId) {
throw 'no video input';
}
var mediaConstraint = { video: {facingMode: 'environment'}, audio: false };

$scope.config.sourceDevice = backCam ? backCam : source;
$scope.config.source = backCam ? backCam.deviceId : sourceId;
// if we got to choose a source
if ($scope.config.source !== undefined) {
videoOptions = {optional: [{sourceId: $scope.config.source}]};
} else {
videoOptions = true;
}
mediaConstraint =
{
video: videoOptions,
audio: false
};

if (window.hasModernUserMedia) {
// The spec has changed towards a Promise based interface
navigator.getMedia(mediaConstraint)
.then(onSuccess)
.catch(onFailure);
} else {
navigator.getMedia(mediaConstraint, onSuccess, onFailure);
}
if (window.hasModernUserMedia) {
// The spec has changed towards a Promise based interface
navigator.getMedia(mediaConstraint)
.then(onSuccess)
.catch(onFailure);
} else {
navigator.getMedia(mediaConstraint, onSuccess, onFailure);
}

/* Start streaming the webcam data when the video element can play
* It will do it only once
*/
videoElem.addEventListener('canplay', function () {
if (!isStreaming) {
var scale = width / videoElem.videoWidth;
height = (videoElem.videoHeight * scale) ||
$scope.config.videoHeight;
videoElem.setAttribute('width', width);
videoElem.setAttribute('height', height);
isStreaming = true;

$scope.config.video = videoElem;

_removeDOMElement(placeholder);

/* Call custom callback */
if ($scope.onStreaming) {
$scope.onStreaming();
}
/* Start streaming the webcam data when the video element can play
* It will do it only once
*/
videoElem.addEventListener('canplay', function() {
if (!isStreaming) {
var scale = width / videoElem.videoWidth;
height = (videoElem.videoHeight * scale) ||
$scope.config.videoHeight;
videoElem.setAttribute('width', width);
videoElem.setAttribute('height', height);
isStreaming = true;

$scope.config.video = videoElem;

_removeDOMElement(placeholder);

/* Call custom callback */
if ($scope.onStreaming) {
$scope.onStreaming();
}
}, false);
});
}
}, false);
};

var stopWebcam = function stopWebcam() {
Expand Down
1 change: 0 additions & 1 deletion dist/webcam.min.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@ubreakifix/webcam",
"author": "Jonas Hartmann",
"version": "3.3.0",
"version": "3.3.1",
"license": "MIT",
"main": "app/scripts/webcam.js",
"dependencies": {},
Expand Down

0 comments on commit 5779bea

Please sign in to comment.