Skip to content

Commit 67b1fb3

Browse files
committed
Merge remote-tracking branch 'origin/master'
# Conflicts: # lib/index.js # src/index.js
2 parents d9ee5ea + bb4eee4 commit 67b1fb3

File tree

5 files changed

+33
-23
lines changed

5 files changed

+33
-23
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ Type: `string`, Optional
123123

124124
ClassName for the container element.
125125

126+
**chooseDeviceId**
127+
128+
Type: `function`, Optional, Arguments: (1) video devices matching `facingMode`, (2) all video devices
129+
130+
Called when choosing which device to use for scanning. By default chooses the first video device matching `facingMode`, if no devices match the first video device found is choosen.
131+
126132
## Dev
127133

128134
### Install dependencies

lib/getDeviceId.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33
var _require = require('./errors'),
44
NoVideoInputDevicesError = _require.NoVideoInputDevicesError;
55

6+
function defaultDeviceIdChooser(filteredDevices, videoDevices) {
7+
return filteredDevices.length > 0 ? filteredDevices[0].deviceId
8+
// No device found with the pattern thus use another video device
9+
: videoDevices[0].deviceId;
10+
}
11+
612
module.exports = function getDeviceId(facingMode) {
13+
var chooseDeviceId = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultDeviceIdChooser;
14+
715
// Get manual deviceId from available devices.
816
return new Promise(function (resolve, reject) {
917
var enumerateDevices = void 0;
@@ -35,12 +43,7 @@ module.exports = function getDeviceId(facingMode) {
3543
return pattern.test(label);
3644
});
3745

38-
if (filteredDevices.length > 0) {
39-
resolve(filteredDevices[0].deviceId);
40-
} else {
41-
// No device found with the pattern thus use another video device
42-
resolve(videoDevices[0].deviceId);
43-
}
46+
resolve(chooseDeviceId(filteredDevices, videoDevices));
4447
});
4548
});
4649
};

lib/index.js

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/getDeviceId.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
const { NoVideoInputDevicesError } = require('./errors')
22

3-
module.exports = function getDeviceId(facingMode) {
3+
function defaultDeviceIdChooser(filteredDevices, videoDevices) {
4+
return (filteredDevices.length > 0)
5+
? filteredDevices[0].deviceId
6+
// No device found with the pattern thus use another video device
7+
: videoDevices[0].deviceId
8+
}
9+
10+
module.exports = function getDeviceId(facingMode, chooseDeviceId = defaultDeviceIdChooser) {
411
// Get manual deviceId from available devices.
512
return new Promise((resolve, reject) => {
613
let enumerateDevices
@@ -32,12 +39,7 @@ module.exports = function getDeviceId(facingMode) {
3239
const filteredDevices = videoDevices.filter(({ label }) =>
3340
pattern.test(label))
3441

35-
if (filteredDevices.length > 0) {
36-
resolve(filteredDevices[0].deviceId)
37-
} else {
38-
// No device found with the pattern thus use another video device
39-
resolve(videoDevices[0].deviceId)
40-
}
42+
resolve(chooseDeviceId(filteredDevices, videoDevices))
4143
})
4244
})
4345
}

src/index.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ module.exports = class Reader extends Component {
2727
maxImageSize: PropTypes.number,
2828
style: PropTypes.any,
2929
className: PropTypes.string,
30+
chooseDeviceId: PropTypes.func,
3031
};
3132
static defaultProps = {
3233
delay: 500,
3334
maxImageSize: 1000,
34-
facingMode: 'rear'
35+
facingMode: 'rear',
3536
};
3637

3738
els = {};
@@ -121,11 +122,9 @@ module.exports = class Reader extends Component {
121122
}
122123
}
123124
initiate(props = this.props) {
124-
const { onError, facingMode } = props
125+
const { onError, facingMode, chooseDeviceId } = props
125126

126-
console.log(facingMode)
127-
128-
getDeviceId(facingMode)
127+
getDeviceId(facingMode, chooseDeviceId)
129128
.then(deviceId => {
130129
return navigator.mediaDevices.getUserMedia({
131130
video: {

0 commit comments

Comments
 (0)