Skip to content

Commit 9df15d7

Browse files
committed
Only mirror video when device is detected as user facing #46
1 parent 7e2460f commit 9df15d7

File tree

6 files changed

+161
-22
lines changed

6 files changed

+161
-22
lines changed

lib/getDeviceId.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ function defaultDeviceIdChooser(filteredDevices, videoDevices, facingMode) {
77
if (filteredDevices.length > 0) {
88
return filteredDevices[0].deviceId;
99
}
10-
if (videoDevices.length == 1 || facingMode == 'front') {
10+
if (videoDevices.length == 1 || facingMode == 'user') {
1111
return videoDevices[0].deviceId;
1212
}
1313
return videoDevices[1].deviceId;
1414
}
1515

16-
module.exports = function getDeviceId(facingMode) {
16+
var getFacingModePattern = function getFacingModePattern(facingMode) {
17+
return facingMode == 'environment' ? /rear|back|environment/ig : /front|user|face/ig;
18+
};
19+
20+
function getDeviceId(facingMode) {
1721
var chooseDeviceId = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultDeviceIdChooser;
1822

1923
// Get manual deviceId from available devices.
@@ -35,7 +39,7 @@ module.exports = function getDeviceId(facingMode) {
3539
return;
3640
}
3741

38-
var pattern = facingMode == 'rear' ? /rear|back|environment/ig : /front|user|face/ig;
42+
var pattern = getFacingModePattern(facingMode);
3943

4044
// Filter out video devices without the pattern
4145
var filteredDevices = videoDevices.filter(function (_ref) {
@@ -46,4 +50,6 @@ module.exports = function getDeviceId(facingMode) {
4650
resolve(chooseDeviceId(filteredDevices, videoDevices, facingMode));
4751
});
4852
});
49-
};
53+
}
54+
55+
module.exports = { getDeviceId: getDeviceId, getFacingModePattern: getFacingModePattern };

lib/index.js

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

package-lock.json

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

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "A react component for reading QR codes from the webcam.",
55
"main": "./lib/index.js",
66
"scripts": {
7+
"start": "concurrently --kill-others \"npm run build:watch\" \"npm run storybook\"",
78
"storybook": "start-storybook -p 9001",
89
"build": "gulp",
910
"build:watch": "gulp watch",
@@ -32,6 +33,7 @@
3233
"babel-preset-es2015": "^6.24.1",
3334
"babel-preset-react": "^6.24.1",
3435
"babel-preset-stage-0": "^6.5.0",
36+
"concurrently": "^3.5.0",
3537
"del": "^2.2.2",
3638
"eslint": "^2.13.1",
3739
"eslint-plugin-react": "^5.2.2",

src/getDeviceId.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ function defaultDeviceIdChooser(filteredDevices, videoDevices, facingMode) {
44
if(filteredDevices.length > 0){
55
return filteredDevices[0].deviceId
66
}
7-
if(videoDevices.length == 1 || facingMode == 'front'){
7+
if(videoDevices.length == 1 || facingMode == 'user'){
88
return videoDevices[0].deviceId
99
}
1010
return videoDevices[1].deviceId
1111
}
1212

13-
module.exports = function getDeviceId(facingMode, chooseDeviceId = defaultDeviceIdChooser) {
13+
const getFacingModePattern = (facingMode) => facingMode == 'environment'
14+
? /rear|back|environment/ig
15+
: /front|user|face/ig
16+
17+
function getDeviceId(facingMode, chooseDeviceId = defaultDeviceIdChooser) {
1418
// Get manual deviceId from available devices.
1519
return new Promise((resolve, reject) => {
1620
let enumerateDevices
@@ -30,9 +34,7 @@ module.exports = function getDeviceId(facingMode, chooseDeviceId = defaultDevice
3034
return
3135
}
3236

33-
const pattern = facingMode == 'rear'
34-
? /rear|back|environment/ig
35-
: /front|user|face/ig
37+
const pattern = getFacingModePattern(facingMode)
3638

3739
// Filter out video devices without the pattern
3840
const filteredDevices = videoDevices.filter(({ label }) =>
@@ -42,3 +44,5 @@ module.exports = function getDeviceId(facingMode, chooseDeviceId = defaultDevice
4244
})
4345
})
4446
}
47+
48+
module.exports = { getDeviceId, getFacingModePattern }

src/index.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const React = require('react')
22
const { Component } = React
33
const PropTypes = require('prop-types')
4-
const getDeviceId = require('./getDeviceId')
4+
const { getDeviceId, getFacingModePattern } = require('./getDeviceId')
55
const havePropsChanged = require('./havePropsChanged')
66

77
// Require adapter to support older browser implementations
@@ -31,20 +31,24 @@ module.exports = class Reader extends Component {
3131
resolution: PropTypes.number,
3232
showViewFinder: PropTypes.bool,
3333
style: PropTypes.any,
34-
className: PropTypes.string
34+
className: PropTypes.string,
3535
};
3636
static defaultProps = {
3737
delay: 500,
3838
resolution: 600,
3939
facingMode: 'environment',
40-
showViewFinder: true
40+
showViewFinder: true,
4141
};
4242

4343
els = {};
4444

4545
constructor(props) {
4646
super(props)
4747

48+
this.state = {
49+
mirrorVideo: false,
50+
}
51+
4852
// Bind function to the class
4953
this.initiate = this.initiate.bind(this)
5054
this.initiateLegacyMode = this.initiateLegacyMode.bind(this)
@@ -97,7 +101,11 @@ module.exports = class Reader extends Component {
97101
}
98102
}
99103
}
100-
shouldComponentUpdate(nextProps) {
104+
shouldComponentUpdate(nextProps, nextState) {
105+
if(nextState !== this.state){
106+
return true
107+
}
108+
101109
// Only render when the `propsKeys` have changed.
102110
const changedProps = havePropsChanged(this.props, nextProps, propsKeys)
103111
return changedProps.length > 0
@@ -127,7 +135,7 @@ module.exports = class Reader extends Component {
127135
}
128136
}
129137
initiate(props = this.props) {
130-
const { onError, facingMode, resolution } = props
138+
const { onError, facingMode } = props
131139

132140
// Check browser facingMode constraint support
133141
// Firefox ignores facingMode or deviceId constraints
@@ -178,6 +186,10 @@ module.exports = class Reader extends Component {
178186
this.stopCamera = streamTrack.stop.bind(streamTrack)
179187

180188
preview.addEventListener('loadstart', this.handleLoadStart)
189+
190+
const facingUserPattern = getFacingModePattern('user')
191+
const isUserFacing = facingUserPattern.test(streamTrack.label)
192+
this.setState({ mirrorVideo: isUserFacing })
181193
}
182194
handleLoadStart() {
183195
const { delay, onLoad } = this.props
@@ -317,7 +329,7 @@ module.exports = class Reader extends Component {
317329
const videoPreviewStyle = {
318330
...previewStyle,
319331
objectFit: 'cover',
320-
transform: facingMode == 'user' ? 'scaleX(-1)' : undefined,
332+
transform: this.state.mirrorVideo ? 'scaleX(-1)' : undefined,
321333
}
322334
const imgPreviewStyle = {
323335
...previewStyle,

0 commit comments

Comments
 (0)