Skip to content

Commit 927c92a

Browse files
committed
refactor(:camera): accept String instead of Object
The camera provides a way to turn the camera off while the component is still mounted. It also allows customization of the MediaTrackConstraints object passed to `getUserMedia`. This however makes this prop: * harder to understand * more error prone (Safari breaks for certain inputs) * needlessly complex (only switching facingMode is really required) To preserve the main functionality and make it simpler to use, camera now accepts one of the following string values: * "rear" : use rear camera * "front" : use front camera * "off" : turn camera off BREAKING CHANGE: `:camera` prop does not accept objects or boolean anymore.
1 parent 96f495a commit 927c92a

File tree

3 files changed

+52
-60
lines changed

3 files changed

+52
-60
lines changed

config/webpack.config.common.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
1-
var webpack = require('webpack')
2-
var merge = require('webpack-merge')
3-
var base = require('./webpack.config.base')
4-
var path = require('path')
1+
var webpack = require("webpack");
2+
var merge = require("webpack-merge");
3+
var base = require("./webpack.config.base");
4+
var path = require("path");
55

6-
var outputFile = 'vue-qrcode-reader'
7-
var globalName = 'VueQrcodeReader'
6+
var outputFile = "vue-qrcode-reader";
7+
var globalName = "VueQrcodeReader";
88

99
module.exports = merge(base, {
1010
output: {
11-
path: path.resolve(__dirname, '../dist'),
12-
filename: outputFile + '.common.js',
13-
libraryTarget: 'commonjs2',
11+
path: path.resolve(__dirname, "../dist"),
12+
filename: outputFile + ".common.js",
13+
libraryTarget: "commonjs2"
1414
},
15-
target: 'node',
15+
target: "node",
1616
externals: {
1717
// Put external libraries like lodash here
1818
// With their package name
1919
// Example: 'lodash': 'lodash'
20-
'jsqr': 'jsqr',
21-
'babel-runtime': 'babel-runtime',
22-
'webrtc-adapter': 'webrtc-adapter',
23-
'lodash': 'lodash',
20+
jsqr: "jsqr",
21+
"babel-runtime": "babel-runtime",
22+
"webrtc-adapter": "webrtc-adapter"
2423
},
2524
plugins: [
2625
new webpack.optimize.UglifyJsPlugin({
2726
compress: {
28-
warnings: true,
27+
warnings: true
2928
},
30-
mangle: true,
31-
}),
32-
],
33-
})
29+
mangle: true
30+
})
31+
]
32+
});

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@
7878
"dependencies": {
7979
"babel-runtime": "^6.26.0",
8080
"jsqr": "^1.1.0",
81-
"lodash": "^4.17.11",
8281
"webrtc-adapter": "^6.2.1"
8382
}
8483
}

src/components/QrcodeStream.vue

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ import { keepScanning } from "../misc/scanner.js";
3333
import { thinSquare } from "../misc/track-func.js";
3434
import Camera from "../misc/camera.js";
3535
import CommonAPI from "../mixins/CommonAPI.vue";
36-
import isEqual from "lodash/isEqual";
37-
import isBoolean from "lodash/isBoolean";
38-
import isObject from "lodash/isObject";
39-
import stubObject from "lodash/stubObject";
4036
4137
export default {
4238
mixins: [CommonAPI],
@@ -48,8 +44,12 @@ export default {
4844
},
4945
5046
camera: {
51-
type: [Object, Boolean],
52-
default: stubObject
47+
type: String,
48+
default: "rear",
49+
50+
validator(camera) {
51+
return ["rear", "front", "off"].includes(camera);
52+
}
5353
},
5454
5555
track: {
@@ -62,7 +62,6 @@ export default {
6262
return {
6363
cameraInstance: null,
6464
destroyed: false,
65-
constraints: {},
6665
stopScanning: () => {}
6766
};
6867
},
@@ -72,7 +71,7 @@ export default {
7271
return (
7372
this.paused === false &&
7473
this.destroyed === false &&
75-
this.constraints.video !== false
74+
this.camera !== "off"
7675
);
7776
},
7877
@@ -100,6 +99,32 @@ export default {
10099
} else {
101100
return this.track;
102101
}
102+
},
103+
104+
constraints() {
105+
const base = {
106+
audio: false,
107+
video: {
108+
width: { min: 360, ideal: 640, max: 1920 },
109+
height: { min: 240, ideal: 480, max: 1080 }
110+
}
111+
};
112+
113+
switch (this.camera) {
114+
case "rear":
115+
base.video.facingMode = { ideal: "environment" };
116+
117+
return base;
118+
case "front":
119+
base.video.facingMode = { exact: "user" };
120+
121+
return base;
122+
case "off":
123+
return undefined;
124+
125+
default:
126+
return undefined;
127+
}
103128
}
104129
},
105130
@@ -121,37 +146,6 @@ export default {
121146
}
122147
},
123148
124-
camera: {
125-
deep: true,
126-
immediate: true,
127-
128-
handler(camera, oldValue) {
129-
const deeplyEqual = isEqual(camera, oldValue);
130-
131-
if (deeplyEqual) {
132-
// object reference changed but constraints are actually the same
133-
return;
134-
} else if (isBoolean(camera)) {
135-
this.constraints = {
136-
audio: false,
137-
video: camera
138-
};
139-
} else if (isObject(camera)) {
140-
this.constraints = {
141-
audio: false,
142-
video: {
143-
facingMode: { ideal: "environment" },
144-
width: { min: 360, ideal: 640, max: 1920 },
145-
height: { min: 240, ideal: 480, max: 1080 },
146-
147-
// overrides properties above if given
148-
...camera
149-
}
150-
};
151-
}
152-
}
153-
},
154-
155149
constraints() {
156150
this.$emit("init", this.init());
157151
}
@@ -171,7 +165,7 @@ export default {
171165
async init() {
172166
this.beforeResetCamera();
173167
174-
if (this.constraints.video === false) {
168+
if (this.constraints === undefined) {
175169
this.cameraInstance = null;
176170
} else {
177171
this.cameraInstance = await Camera(this.constraints, this.$refs.video);

0 commit comments

Comments
 (0)