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

Feat/iphone stuff #5

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ node_modules
# Optional REPL history
.node_repl_history


lib/worker.js
yarn.lock
lib/worker.js
25 changes: 16 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
[![npm version](https://badge.fury.io/js/react-qr-reader2.svg)](https://badge.fury.io/js/react-qr-reader2)
<!-- [![npm version](https://badge.fury.io/js/react-qr-reader2.svg)](https://badge.fury.io/js/react-qr-reader2) -->

## Introduction

This is fork from [react-qr-reader](https://github.com/JodusNodus/react-qr-reader). Some changes are being made. E.g. onLoad gives streamTrack object, not just streamLabel string. legacyMode is also been removed. I'll add it back later if it is needed for some reason.
This is fork from [react-qr-reader2](https://github.com/tahv0/react-qr-reader2), which in turn is a fork of another package - [react-qr-reader](https://github.com/JodusNodus/react-qr-reader).

This fork of the package will focus more on supporting modern phones using PWAs, updating the outdated code from the original fork and focus on only using the environment facing cameras to scan QR codes. This is because it is rare for camera scan usage to involve front facing cameras. For now all support is being focused on Android Chrome and Safari iOS. Support for Firefox, and desktop environments may work but is not the focus of this package for now.

PRs welcome and encouraged :)

*Original package description below*
A [React](https://facebook.github.io/react/) component for reading QR codes from the webcam. It uses the WebRTC standards for reading webcam data and [jsQR](https://github.com/cozmo/jsQR) is used for detecting QR codes in that data. To optimise the speed and experience, a web-worker is used to offload the heavy QR code algorithm on a separate process. The web worker is inlined and loaded on creation of the component.

## Known Issues

* Currently the Storybook example does not seem to work.
* Server side rendering won't work so only require the componont when rendering in a browser environment.
* Due to browser implementations the camera can only be accessed over https or localhost.
* In Firefox a prompt will be shown to the user asking which camera to use, so `facingMode` will not affect it.
Expand All @@ -16,13 +22,13 @@ A [React](https://facebook.github.io/react/) component for reading QR codes from

## Install

`npm install --save react-qr-reader2`
`yarn install --save react-camera-qr`

## Example

```js
import React, { Component } from 'react'
import QrReader from 'react-qr-reader2'
import QrReader from 'react-camera-qr'

class Test extends Component {
state = {
Expand Down Expand Up @@ -77,26 +83,27 @@ class Test extends Component {
| className | string | none | ClassName for the container element. |
| showViewFinder | boolean | `true` | Show or hide the build in view finder. See demo |
| constraints | object | `null` | Use custom camera constraints that the override default behavior. [MediaTrackConstraints](https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackConstraints)|
| chosenCamera | string | `''` | By using the `enumerateDevices` function you can pass a device ID directly to the camera instead of using the default. |

## Dev

### Install dependencies

`npm install`
`yarn install`

### Build

`npm run build`
`yarn run build`

### Demo

`npm run storybook`
`yarn run storybook`

### Linting

`npm run lint`
`yarn run lint`

## Tested platforms

* Chrome Android
* Firefox Android
* Safari iOS
47 changes: 38 additions & 9 deletions lib/getDeviceId.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,43 @@ function defaultDeviceIdChooser(filteredDevices, videoDevices, facingMode) {
}

var getFacingModePattern = function getFacingModePattern(facingMode) {
return facingMode === 'environment' ? /rear|back|environment/gi : /front|user|face/gi;
return facingMode === 'environment' ? /rear|back|bak|environment/gi : /front|foran|user|face/gi;
};

function getDeviceId(facingMode) {
var chooseDeviceId = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultDeviceIdChooser;
function getIdDirectly(facingMode, chosenCamera) {
return new Promise(function (resolve, reject) {
var enumerateDevices = void 0;
try {
enumerateDevices = navigator.mediaDevices.enumerateDevices();
} catch (err) {
reject(new NoVideoInputDevicesError());
}
enumerateDevices.then(function (devices) {
var videoDevices = devices.filter(function (device) {
return device.kind === 'videoinput';
});
if (videoDevices.length < 1) {
reject(new NoVideoInputDevicesError());
return;
}
var pattern = getFacingModePattern(facingMode);
// Filter out video devices without the pattern
var filteredDevices = videoDevices.filter(function (_ref) {
var label = _ref.label;
return pattern.test(label);
});
for (var device in videoDevices) {
if (videoDevices[device].deviceId === chosenCamera) {
resolve(chosenCamera);
return;
}
}
resolve(defaultDeviceIdChooser(filteredDevices, getFacingModePattern, facingMode));
});
});
}

function getDefaultDeviceId(facingMode) {
// Get manual deviceId from available devices.
return new Promise(function (resolve, reject) {
var enumerateDevices = void 0;
Expand All @@ -40,16 +71,14 @@ function getDeviceId(facingMode) {
}

var pattern = getFacingModePattern(facingMode);

// Filter out video devices without the pattern
var filteredDevices = videoDevices.filter(function (_ref) {
var label = _ref.label;
var filteredDevices = videoDevices.filter(function (_ref2) {
var label = _ref2.label;
return pattern.test(label);
});

resolve(chooseDeviceId(filteredDevices, videoDevices, facingMode));
resolve(defaultDeviceIdChooser(filteredDevices, videoDevices, facingMode));
});
});
}

module.exports = { getDeviceId: getDeviceId, getFacingModePattern: getFacingModePattern };
module.exports = { getDefaultDeviceId: getDefaultDeviceId, getFacingModePattern: getFacingModePattern, getIdDirectly: getIdDirectly };
43 changes: 31 additions & 12 deletions lib/index.js

Large diffs are not rendered by default.

Loading