├── .eslintignore ├── .eslintrc.js ├── .gitignore ├── .npmrc ├── .prettierrc.js ├── .travis.yml ├── LICENSE ├── README.md ├── dist ├── components │ └── Camera │ │ ├── Camera.d.ts │ │ ├── styles.d.ts │ │ └── types.d.ts ├── index.cjs.js ├── index.d.ts └── index.esm.js ├── example ├── .eslintrc.js ├── .gitignore ├── .prettierrc.js ├── README.md ├── package-lock.json ├── package.json ├── public │ ├── favicon.ico │ ├── index.html │ └── manifest.json ├── src │ ├── App.tsx │ ├── Camera │ │ ├── components │ │ │ └── Camera │ │ │ │ ├── Camera.d.ts │ │ │ │ ├── styles.d.ts │ │ │ │ └── types.d.ts │ │ ├── index.d.ts │ │ └── index.js │ ├── index.css │ ├── index.tsx │ ├── react-app-env.d.ts │ └── serviceWorker.js └── tsconfig.json ├── package-lock.json ├── package.json ├── rollup.config.js ├── src ├── components │ └── Camera │ │ ├── Camera.tsx │ │ ├── styles.ts │ │ └── types.ts └── index.ts ├── test └── test.ts └── tsconfig.json /.eslintignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist 3 | reactComponentLib 4 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | parser: '@typescript-eslint/parser', 4 | extends: [ 5 | 'plugin:react/recommended', 6 | 'plugin:@typescript-eslint/recommended', 7 | 'prettier', 8 | 'plugin:prettier/recommended', 9 | ], 10 | parserOptions: { 11 | ecmaVersion: 2018, 12 | sourceType: 'module', 13 | ecmaFeatures: { 14 | jsx: true, 15 | }, 16 | }, 17 | rules: { 18 | '@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_|^req|^next' }], 19 | '@typescript-eslint/no-explicit-any': 0, 20 | '@typescript-eslint/explicit-function-return-type': 0, 21 | '@typescript-eslint/no-use-before-define': 0, 22 | 'react/prop-types': 0, 23 | }, 24 | settings: { 25 | react: { 26 | version: 'detect', 27 | }, 28 | }, 29 | }; 30 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | .rpt2_cache 4 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | registry = "https://registry.npmjs.com/" 2 | -------------------------------------------------------------------------------- /.prettierrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | semi: true, 3 | trailingComma: 'all', 4 | singleQuote: true, 5 | printWidth: 120, 6 | tabWidth: 2, 7 | }; 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '11' 4 | - '10' 5 | - '8' 6 | - '6' 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Purple Technology 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![npm][npm-badge] 2 | ![downloads][downloads-badge] 3 | 4 | # react-camera-pro 5 | 6 | Universal Camera component for React. 7 | 8 | Designed with focus on Android and iOS cameras. 9 | Works with standard webcams as well. 10 | 11 | See [this](http://caniuse.com/#feat=stream) for browser compatibility. 12 | 13 | Note: WebRTC is only supported on secure connections. So you need to serve it from https. You can test and debug in Chrome from localhost though (this doesn't work in Safari). 14 | 15 | ## Features 16 | 17 | - mobile friendly camera solution (tested on iOS and Android) 18 | - video element is fully responsive 19 | - you can setup parameter to cover your container 20 | - you can define aspectRatio of view: 16/9, 4/3, 1/1, ... 21 | - taking photo to base64 jpeg file - with same aspect Ratio as view, with FullHD resolution (or maximum supported by camera). 22 | - working with standard webcams or other video input devices 23 | - supports autofocus 24 | - switching facing/environment cameras (with your own button) 25 | - detect number of cameras 26 | - facing camera is mirrored, environment is not 27 | - controlled by react [Ref](https://reactjs.org/docs/refs-and-the-dom.html) 28 | - public functions to take photo, to switch camera and to get number of cameras 29 | - typescript library 30 | 31 | ## Installation 32 | 33 | ``` 34 | npm install --save react-camera-pro 35 | ``` 36 | 37 | ## Demo 38 | 39 | https://purple-technology.github.io/react-camera-pro/ 40 | 41 | ## Example 42 | 43 | https://github.com/purple-technology/react-camera-pro/blob/master/example/src/App.tsx 44 | 45 | ## Usage 46 | 47 | ```javascript 48 | import React, { useState, useRef } from "react"; 49 | import {Camera} from "react-camera-pro"; 50 | 51 | const Component = () => { 52 | const camera = useRef(null); 53 | const [image, setImage] = useState(null); 54 | 55 | return ( 56 |
57 | 58 | 59 | Taken photo 60 |
61 | ); 62 | } 63 | 64 | export Component; 65 | ``` 66 | 67 | ### Props 68 | 69 | | prop | type | default | notes | 70 | | ----------------------- | -------------------------------- | ------------ | ---------------------------------------------- | 71 | | facingMode | `'user'\|'environment'` | `'user'` | default camera - 'user' or 'environment' | 72 | | aspectRatio | `'cover'\|number` | `'cover'` | aspect ratio of video (16/9, 4/3); | 73 | | numberOfCamerasCallback | `(numberOfCameras: number):void` | `() => null` | callback is called if number of cameras change | 74 | | errorMessages | `object?` see below | see below | Error messages object (optional) | 75 | 76 | #### Error messages (prop errorMessages) 77 | 78 | Type: 79 | 80 | ``` 81 | errorMessages: { 82 | noCameraAccessible?: string; 83 | permissionDenied?: string; 84 | switchCamera?: string; 85 | canvas?: string; 86 | }; 87 | ``` 88 | 89 | Default: 90 | ``` 91 | { 92 | noCameraAccessible: 'No camera device accessible. Please connect your camera or try a different browser.', 93 | permissionDenied: 'Permission denied. Please refresh and give camera permission.', 94 | switchCamera: 95 | 'It is not possible to switch camera to different one because there is only one video device accessible.', 96 | canvas: 'Canvas is not supported.' 97 | } 98 | ``` 99 | 100 | ### Methods 101 | 102 | - `takePhoto(): string` - Returns a base64 encoded string of the taken image. 103 | - `switchCamera(): 'user'|'environment'` - Switches the camera - user to environment or environment to user. Returns the new value 'user' or 'environment'. 104 | - `getNumberOfCameras(): number` - Returns number of available cameras. 105 | 106 | [See demo](https://purple-technology.github.io/react-camera-pro/) 107 | 108 | [See example code](https://github.com/purple-technology/react-camera-pro/blob/8290b1319d7436c77403784fe845060f6c4ed3bd/example/src/App.tsx#L120) 109 | 110 | ```javascript 111 | 112 | const Component = () => { 113 | const camera = useRef(null); 114 | const [numberOfCameras, setNumberOfCameras] = useState(0); 115 | const [image, setImage] = useState(null); 116 | 117 | //... 118 | 119 | return ( 120 | 121 | Image preview 122 |