1 | # expo-camera
|
2 |
|
3 | ## Installation
|
4 |
|
5 | ### iOS (Cocoapods)
|
6 |
|
7 | If you're using Cocoapods, add the dependency to your `Podfile`:
|
8 |
|
9 | `pod 'EXCamera'`
|
10 |
|
11 | and run `pod install`.
|
12 |
|
13 | ### iOS (no Cocoapods)
|
14 |
|
15 | 1. In XCode, in the project navigator, right click `Libraries` ➜ `Add Files to [your project's name]`
|
16 | 2. Go to `node_modules` ➜ `expo-camera` and add `EXCamera.xcodeproj`
|
17 | 3. In XCode, in the project navigator, select your project. Add `libEXCamera.a` to your project's `Build Phases` ➜ `Link Binary With Libraries`
|
18 | 4. Run your project (`Cmd+R`).
|
19 |
|
20 | ### Android
|
21 |
|
22 | 1. Append the following lines to `android/settings.gradle`:
|
23 | ```gradle
|
24 | include ':expo-camera'
|
25 | project(':expo-camera').projectDir = new File(rootProject.projectDir, '../node_modules/expo-camera/android')
|
26 | ```
|
27 | and if not already included
|
28 | ```gradle
|
29 | include ':expo-permissions-interface'
|
30 | project(':expo-permissions-interface').projectDir = new File(rootProject.projectDir, '../node_modules/expo-permissions-interface/android')
|
31 | ```
|
32 | 2. Insert the following lines inside the dependencies block in `android/app/build.gradle`:
|
33 | ```gradle
|
34 | compile project(':expo-camera')
|
35 | ```
|
36 | and if not already included
|
37 | ```gradle
|
38 | compile project(':expo-permissions-interface')
|
39 | ```
|
40 |
|
41 | ## Introduction
|
42 |
|
43 | A React component that renders a preview for the device's either front or back camera. Camera's parameters like zoom, auto focus, white balance and flash mode are adjustable. With use of `Camera` one can also take photos and record videos that are saved to the app's cache. Morever, the component is also capable of detecting faces and bar codes appearing on the preview.
|
44 |
|
45 | > **Note**: Only one Camera preview is supported by this library right now. When using navigation, the best practice is to unmount previously rendered `Camera` component so next screens can use camera without issues.
|
46 |
|
47 | > **Note**: Android devices can use one of two available Camera apis underneath. This was previously chosen automatically, based on the device's Android system version and camera hardware capabilities. As we experienced some issues with Android's Camera2 API, we decided to choose the older API as a default. However, using the newer one is still possible through setting `useCamera2Api` prop to true. The change we made should be barely visible - the only thing that is not supported using the old Android's API is setting focus depth.
|
48 |
|
49 | Requires `Permissions.CAMERA`. Video recording requires `Permissions.AUDIO_RECORDING`. (See `expo-permissions` package).
|
50 |
|
51 | ### Basic Example
|
52 |
|
53 | ```javascript
|
54 | import React from 'react';
|
55 | import { Text, View, TouchableOpacity } from 'react-native';
|
56 | import { Permissions } from 'expo-permissions';
|
57 | import { Camera } from 'expo-camera';
|
58 |
|
59 | export default class CameraExample extends React.Component {
|
60 | state = {
|
61 | hasCameraPermission: null,
|
62 | type: Camera.Constants.Type.back,
|
63 | };
|
64 |
|
65 | async componentWillMount() {
|
66 | const { status } = await Permissions.askAsync(Permissions.CAMERA);
|
67 | this.setState({ hasCameraPermission: status === 'granted' });
|
68 | }
|
69 |
|
70 | render() {
|
71 | const { hasCameraPermission } = this.state;
|
72 | if (hasCameraPermission === null) {
|
73 | return <View />;
|
74 | } else if (hasCameraPermission === false) {
|
75 | return <Text>No access to camera</Text>;
|
76 | } else {
|
77 | return (
|
78 | <View style={{ flex: 1 }}>
|
79 | <Camera style={{ flex: 1 }} type={this.state.type}>
|
80 | <View
|
81 | style={{
|
82 | flex: 1,
|
83 | backgroundColor: 'transparent',
|
84 | flexDirection: 'row',
|
85 | }}>
|
86 | <TouchableOpacity
|
87 | style={{
|
88 | flex: 0.1,
|
89 | alignSelf: 'flex-end',
|
90 | alignItems: 'center',
|
91 | }}
|
92 | onPress={() => {
|
93 | this.setState({
|
94 | type:
|
95 | this.state.type === Camera.Constants.Type.back
|
96 | ? Camera.Constants.Type.front
|
97 | : Camera.Constants.Type.back,
|
98 | });
|
99 | }}>
|
100 | <Text style={{ fontSize: 18, marginBottom: 10, color: 'white' }}> Flip </Text>
|
101 | </TouchableOpacity>
|
102 | </View>
|
103 | </Camera>
|
104 | </View>
|
105 | );
|
106 | }
|
107 | }
|
108 | }
|
109 | ```
|
110 |
|
111 | ### props
|
112 |
|
113 | * **type**
|
114 |
|
115 | Camera facing. Use one of `Camera.Constants.Type`. When `Type.front`, use the front-facing camera. When `Type.back`, use the back-facing camera. Default: `Type.back`.
|
116 |
|
117 | * **flashMode**
|
118 |
|
119 | Camera flash mode. Use one of `Camera.Constants.FlashMode`. When `on`, the flash on your device will turn on when taking a picture, when `off`, it won't. Setting to `auto` will fire flash if required, `torch` turns on flash during the preview. Default: `off`.
|
120 |
|
121 | * **autoFocus**
|
122 |
|
123 | State of camera auto focus. Use one of `Camera.Constants.AutoFocus`. When `on`, auto focus will be enabled, when `off`, it wont't and focus will lock as it was in the moment of change but it can be adjusted on some devices via `focusDepth` prop.
|
124 |
|
125 | * **zoom** (_float_)
|
126 |
|
127 | A value between 0 and 1 being a percentage of device's max zoom. 0 - not zoomed, 1 - maximum zoom. Default: 0.
|
128 |
|
129 | * **whiteBalance**
|
130 |
|
131 | Camera white balance. Use one of `Camera.Constants.WhiteBalance`: `auto`, `sunny`, `cloudy`, `shadow`, `fluorescent`, `incandescent`. If a device does not support any of these values previous one is used.
|
132 |
|
133 | * **focusDepth** (_float_)
|
134 |
|
135 | Distance to plane of sharpest focus. A value between 0 and 1: 0 - infinity focus, 1 - focus as close as possible. Default: 0. For Android this is available only for some devices and when `useCamera2Api` is set to true.
|
136 |
|
137 | * **ratio** (_string_)
|
138 |
|
139 | Android only. A string representing aspect ratio of the preview, eg. `4:3`, `16:9`, `1:1`. To check if a ratio is supported by the device use `getSupportedRatiosAsync`. Default: `4:3`.
|
140 |
|
141 | * **onCameraReady** (_function_)
|
142 |
|
143 | Callback invoked when camera preview has been set.
|
144 |
|
145 | * **onFacesDetected** (_function_)
|
146 |
|
147 | Callback invoked with results of face detection on the preview. It will receive an object containing:
|
148 |
|
149 | * **faces** (_array_) - array of faces objects:
|
150 | * **faceID (_number_)** -- a face identifier (used for tracking, if the same face appears on consecutive frames it will have the same `faceID`).
|
151 | * **bounds (_object_)** -- an object containing:
|
152 | * **origin (`{ x: number, y: number }`)** -- position of the top left corner of a square containing the face in view coordinates,
|
153 | * **size (`{ width: number, height: number }`)** -- size of the square containing the face in view coordinates,
|
154 | * **rollAngle (_number_)** -- roll angle of the face (bank),
|
155 | * **yawAngle (_number_)** -- yaw angle of the face (heading, turning head left or right),
|
156 | * **smilingProbability (_number_)** -- probability that the face is smiling,
|
157 | * **leftEarPosition (`{ x: number, y: number}`)** -- position of the left ear in view coordinates,
|
158 | * **rightEarPosition (`{ x: number, y: number}`)** -- position of the right ear in view coordinates,
|
159 | * **leftEyePosition (`{ x: number, y: number}`)** -- position of the left eye in view coordinates,
|
160 | * **leftEyeOpenProbability (_number_)** -- probability that the left eye is open,
|
161 | * **rightEyePosition (`{ x: number, y: number}`)** -- position of the right eye in view coordinates,
|
162 | * **rightEyeOpenProbability (_number_)** -- probability that the right eye is open,
|
163 | * **leftCheekPosition (`{ x: number, y: number}`)** -- position of the left cheek in view coordinates,
|
164 | * **rightCheekPosition (`{ x: number, y: number}`)** -- position of the right cheek in view coordinates,
|
165 | * **mouthPosition (`{ x: number, y: number}`)** -- position of the center of the mouth in view coordinates,
|
166 | * **leftMouthPosition (`{ x: number, y: number}`)** -- position of the left edge of the mouth in view coordinates,
|
167 | * **rightMouthPosition (`{ x: number, y: number}`)** -- position of the right edge of the mouth in view coordinates,
|
168 | * **noseBasePosition (`{ x: number, y: number}`)** -- position of the nose base in view coordinates.
|
169 |
|
170 | `smilingProbability`, `leftEyeOpenProbability` and `rightEyeOpenProbability` are returned only if `faceDetectionClassifications` property is set to `.all`.
|
171 |
|
172 | Positions of face landmarks are returned only if `faceDetectionLandmarks` property is set to `.all`.
|
173 |
|
174 | See also `FaceDetector` component.
|
175 |
|
176 | * **faceDetectionMode** (_Camera.Constants.FaceDetection.Mode_)
|
177 |
|
178 | Mode of the face detection. Use one of `Camera.Constants.FaceDetection.Mode.{fast, accurate}`.
|
179 |
|
180 | * **faceDetectionLandmarks** (_Camera.Constants.FaceDetection.Landmarks_)
|
181 |
|
182 | Whether to detect landmarks on the faces. Use one of `Camera.Constants.FaceDetection.Landmarks.{all, none}`. See FaceDetector documentation for details.
|
183 |
|
184 | * **faceDetectionClassifications** (_Camera.Constants.FaceDetection.Classifications_)
|
185 |
|
186 | Whether to run additional classifications on the faces. Use one of `Camera.Constants.FaceDetection.Classifications.{all, none}`. See FaceDetector documentation for details.
|
187 |
|
188 | * **onMountError** (_function_)
|
189 |
|
190 | Callback invoked when camera preview could not been started. It is provided with an error object that contains a `message`.
|
191 |
|
192 | * **onBarCodeRead (_function_)**
|
193 |
|
194 | **Deprecated**. Use **onBarCodeScanned** instead.
|
195 |
|
196 | * **onBarCodeScanned (_function_)**
|
197 |
|
198 | Callback that is invoked when a bar code has been successfully scanned. The callback is provided with an Object of the shape `{ type: BarCodeScanner.Constants.BarCodeType, data: string }`, where the type refers to the bar code type that was scanned and the data is the information encoded in the bar code (in this case of QR codes, this is often a URL). See [`BarCodeScanner.Constants.BarCodeType`](https://docs.expo.io/versions/latest/sdk/bar-code-scanner#supported-formats) for supported values.
|
199 |
|
200 | * **barCodeTypes (_Array<string>_)**
|
201 |
|
202 | **Deprecated**. Use **barCodeScannerSettings** instead.
|
203 |
|
204 | * **barCodeScannerSettings (_object_)**
|
205 |
|
206 | Settings passed to [`BarCodeScanner`](https://docs.expo.io/versions/latest/sdk/bar-code-scanner) module (if present). Supported settings: [**barCodeTypes**].
|
207 |
|
208 | ```javascript
|
209 | <Camera
|
210 | barCodeScannerSettings={{
|
211 | barCodeTypes: [BarCodeScanner.Constants.BarCodeType.qr]
|
212 | }}
|
213 | />
|
214 | ```
|
215 |
|
216 | * **useCamera2Api** (_boolean_)
|
217 |
|
218 | Android only. Whether to use Android's Camera2 API. See `Note` at the top of this page.
|
219 |
|
220 | ## Methods
|
221 |
|
222 | To use methods that Camera exposes one has to create a components `ref` and invoke them using it.
|
223 |
|
224 | ```javascript
|
225 | // ...
|
226 | <Camera
|
227 | ref={ref => {
|
228 | this.camera = ref;
|
229 | }}
|
230 | />;
|
231 | // ...
|
232 | snap = async () => {
|
233 | if (this.camera) {
|
234 | let photo = await this.camera.takePictureAsync();
|
235 | }
|
236 | };
|
237 | ```
|
238 |
|
239 | ### `takePictureAsync`
|
240 |
|
241 | Takes a picture and saves it to app's cache directory. Photos are rotated to match device's orientation and scaled to match the preview. Anyway on Android it is essential to set `ratio` prop to get a picture with correct dimensions.
|
242 |
|
243 | #### Arguments
|
244 |
|
245 | * **options (_object_)** --
|
246 |
|
247 | A map of options:
|
248 |
|
249 | * **quality (_number_)** -- Specify the quality of compression, from 0 to 1. 0 means compress for small size, 1 means compress for maximum quality.
|
250 | * **base64 (_boolean_)** -- Whether to also include the image data in Base64 format.
|
251 | * **exif (_boolean_)** -- Whether to also include the EXIF data for the image.
|
252 |
|
253 | #### Returns
|
254 |
|
255 | Returns a Promise that resolves to an object: `{ uri, width, height, exif, base64 }` where `uri` is a URI to the local image file (useable as the source for an `Image` element) and `width, height` specify the dimensions of the image. `base64` is included if the `base64` option was truthy, and is a string containing the JPEG data of the image in Base64--prepend that with `'data:image/jpg;base64,'` to get a data URI, which you can use as the source for an `Image` element for example. `exif` is included if the `exif` option was truthy, and is an object containing EXIF data for the image--the names of its properties are EXIF tags and their values are the values for those tags.
|
256 |
|
257 | The local image URI is temporary. Use `Expo.FileSystem.copyAsync` to make a permanent copy of the image.
|
258 |
|
259 | ### `recordAsync`
|
260 |
|
261 | Starts recording a video that will be saved to cache directory. Videos are rotated to match device's orientation. Flipping camera during a recording results in stopping it.
|
262 |
|
263 | #### Arguments
|
264 |
|
265 | * **options (_object_)** --
|
266 |
|
267 | A map of options:
|
268 |
|
269 | * **quality (_VideoQuality_)** -- Specify the quality of recorded video. Usage: `Camera.Constants.VideoQuality['<value>']`, possible values: for 16:9 resolution `2160p`, `1080p`, `720p`, `480p` (_Android only_) and for 4:3 `4:3` (the size is 640x480). If the chosen quality is not available for a device, the highest available is chosen.
|
270 | * **maxDuration (_number_)** -- Maximum video duration in seconds.
|
271 | * **maxFileSize (_number_)** -- Maximum video file size in bytes.
|
272 | * **mute (_boolean_)** -- If present, video will be recorded with no sound.
|
273 |
|
274 | #### Returns
|
275 |
|
276 | Returns a Promise that resolves to an object containing video file `uri` property. The Promise is returned if `stopRecording` was invoked, one of `maxDuration` and `maxFileSize` is reached or camera preview is stopped.
|
277 |
|
278 | ### `stopRecording`
|
279 |
|
280 | Stops recording if any is in progress.
|
281 |
|
282 | ### `getSupportedRatiosAsync`
|
283 |
|
284 | Android only. Get aspect ratios that are supported by the device and can be passed via `ratio` prop.
|
285 |
|
286 | #### Returns
|
287 |
|
288 | Returns a Promise that resolves to an array of strings representing ratios, eg. `['4:3', '1:1']`.
|