Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix not working and swapping cameras for iOS11 and above #207

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified LICENSE
100644 → 100755
Empty file.
2 changes: 2 additions & 0 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Real-time webcam-driven HTML5 QR code scanner. [Try the live demo](https://schmi

*Note:* Chrome requires HTTPS when using the WebRTC API. Any pages using this library should be served over HTTPS.

*Note:* Some browsers (like Edge) require [WebRTC Adapter shim](https://github.com/webrtc/adapter).

### NPM

`npm install --save instascan`
Expand Down
Empty file modified RELEASE.md
100644 → 100755
Empty file.
Empty file modified assets/qr.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified assets/setup.jpg
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified deploy/deploy.sh
100644 → 100755
Empty file.
Empty file modified deploy/id_rsa.enc
100644 → 100755
Empty file.
Empty file modified deploy/id_rsa.pub
100644 → 100755
Empty file.
Empty file modified docs/app.js
100644 → 100755
Empty file.
Empty file modified docs/favicon.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/index.html
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<title>Instascan &ndash; Demo</title>
<link rel="icon" type="image/png" href="favicon.png">
<link rel="stylesheet" href="style.css">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/webrtc-adapter/3.3.3/adapter.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/webrtc-adapter/5.0.4/adapter.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.10/vue.min.js"></script>
<script type="text/javascript" src="https://rawgit.com/schmich/instascan-builds/master/instascan.min.js"></script>
</head>
Expand Down
Empty file modified docs/style.css
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion export.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1 +1 @@
window.Instascan = require('./index');
window.Instascan = require('./src/index');
51 changes: 34 additions & 17 deletions gulpfile.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,29 @@ var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var uglify = require('gulp-uglify');
var babelify = require('babelify');
var babel = require('gulp-babel');
var transform = require('gulp-transform');

var babelOptions = {
ignore: /zxing\.js$/i,
presets: ['env'],
plugins: ['transform-runtime']
};

var build = function (file) {
return browserify(file, { noParse: [ require.resolve('./src/vendor/zxing') ] })
.transform(babelify, babelOptions)
.bundle()
.pipe(source('instascan.js'));
}

var mockImportsInZXing = function (content, file) {
if (/zxing\.js$/i.test(file.relative)) {
return content.replace(/require\([^)]+\)/g, '{}');
} else {
return content;
}
};

gulp.task('default', ['build', 'watch']);

Expand All @@ -13,28 +36,22 @@ gulp.task('watch', function () {
gulp.watch('./*.js', ['build']);
});

function build(file) {
return browserify(file, {
noParse: [require.resolve('./src/zxing')]
})
.transform(babelify, {
ignore: /zxing\.js$/i,
presets: ['es2015'],
plugins: ['syntax-async-functions', 'transform-regenerator']
})
.bundle()
.pipe(source('instascan.js'));
}
gulp.task('build-package', function () {
return gulp.src('./src/**/*.js')
.pipe(transform('utf-8', mockImportsInZXing))
.pipe(babel(babelOptions))
.pipe(gulp.dest('./lib/'));
});

gulp.task('release', function () {
gulp.task('build', ['build-package'], function () {
return build('./export.js')
.pipe(buffer())
.pipe(uglify())
.pipe(rename({ suffix: '.min' }))
.pipe(gulp.dest('./dist/'));
});

gulp.task('build', function () {
gulp.task('release', ['build-package'], function () {
return build('./export.js')
.pipe(buffer())
.pipe(uglify())
.pipe(rename({ suffix: '.min' }))
.pipe(gulp.dest('./dist/'));
});
Empty file modified index.js
100644 → 100755
Empty file.
273 changes: 273 additions & 0 deletions lib/camera.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
'use strict';

var _getIterator2 = require('babel-runtime/core-js/get-iterator');

var _getIterator3 = _interopRequireDefault(_getIterator2);

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function cameraName(label) {
var clean = label.replace(/\s*\([0-9a-f]+(:[0-9a-f]+)?\)\s*$/, '');
return clean || label || null;
}

var MediaError = function (_Error) {
(0, _inherits3.default)(MediaError, _Error);

function MediaError(type) {
(0, _classCallCheck3.default)(this, MediaError);

var _this = (0, _possibleConstructorReturn3.default)(this, (MediaError.__proto__ || (0, _getPrototypeOf2.default)(MediaError)).call(this, 'Cannot access video stream (' + type + ').'));

_this.type = type;
return _this;
}

return MediaError;
}(Error);

var Camera = function () {
function Camera(id, name) {
(0, _classCallCheck3.default)(this, Camera);

this.id = id;
this.name = name;
this._stream = null;
}

(0, _createClass3.default)(Camera, [{
key: 'start',
value: function () {
var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2() {
var _this2 = this;

return _regenerator2.default.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.next = 2;
return Camera._wrapErrors((0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee() {
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
return _context.abrupt('return', navigator.mediaDevices.getUserMedia({
audio: false,
video: {
deviceId: {
exact: _this2.id
},
facingMode: "environment"
}
}));

case 1:
case 'end':
return _context.stop();
}
}
}, _callee, _this2);
})));

case 2:
this._stream = _context2.sent;
return _context2.abrupt('return', this._stream);

case 4:
case 'end':
return _context2.stop();
}
}
}, _callee2, this);
}));

function start() {
return _ref.apply(this, arguments);
}

return start;
}()
}, {
key: 'stop',
value: function stop() {
if (!this._stream) {
return;
}

var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;

try {
for (var _iterator = (0, _getIterator3.default)(this._stream.getVideoTracks()), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var stream = _step.value;

stream.stop();
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}

this._stream = null;
}
}], [{
key: 'getCameras',
value: function () {
var _ref3 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3() {
var devices;
return _regenerator2.default.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
_context3.next = 2;
return this._ensureAccess();

case 2:
_context3.next = 4;
return navigator.mediaDevices.enumerateDevices();

case 4:
devices = _context3.sent;
return _context3.abrupt('return', devices.filter(function (d) {
return d.kind === 'videoinput';
}).map(function (d) {
return new Camera(d.deviceId, cameraName(d.label));
}));

case 6:
case 'end':
return _context3.stop();
}
}
}, _callee3, this);
}));

function getCameras() {
return _ref3.apply(this, arguments);
}

return getCameras;
}()
}, {
key: '_ensureAccess',
value: function () {
var _ref4 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee5() {
var _this3 = this;

return _regenerator2.default.wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
return _context5.abrupt('return', this._wrapErrors((0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee4() {
return _regenerator2.default.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
_context4.next = 2;
return navigator.mediaDevices.getUserMedia({ video: true });

case 2:
case 'end':
return _context4.stop();
}
}
}, _callee4, _this3);
}))));

case 1:
case 'end':
return _context5.stop();
}
}
}, _callee5, this);
}));

function _ensureAccess() {
return _ref4.apply(this, arguments);
}

return _ensureAccess;
}()
}, {
key: '_wrapErrors',
value: function () {
var _ref6 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee6(fn) {
return _regenerator2.default.wrap(function _callee6$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
_context6.prev = 0;
return _context6.abrupt('return', fn());

case 4:
_context6.prev = 4;
_context6.t0 = _context6['catch'](0);

if (!_context6.t0.name) {
_context6.next = 10;
break;
}

throw new MediaError(_context6.t0.name);

case 10:
throw _context6.t0;

case 11:
case 'end':
return _context6.stop();
}
}
}, _callee6, this, [[0, 4]]);
}));

function _wrapErrors(_x) {
return _ref6.apply(this, arguments);
}

return _wrapErrors;
}()
}]);
return Camera;
}();

module.exports = Camera;
8 changes: 8 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

var Instascan = {
Scanner: require('./scanner'),
Camera: require('./camera')
};

module.exports = Instascan;
Loading