react-native-camera/README.md

293 lines
11 KiB
Markdown
Raw Normal View History

2016-01-12 23:01:05 +00:00
# react-native-camera [![npm version](https://badge.fury.io/js/react-native-camera.svg)](http://badge.fury.io/js/react-native-camera) [![Gitter](https://badges.gitter.im/lwansbrough/react-native-camera.svg)](https://gitter.im/lwansbrough/react-native-camera)
2015-04-01 01:04:18 +00:00
2016-02-24 18:59:59 +00:00
A camera module for React Native.
2016-04-28 03:37:58 +00:00
**BREAKING CHANGES:**
2016-04-28 03:37:48 +00:00
[*April 27*] capture now returns an object instead of a string
2016-02-24 18:59:59 +00:00
**NOTE** These docs are for the work in progress v1 release. If you want to use the latest and greatest and can deal with *significant* instability you can install with `npm install --save lwansbrough/react-native-camera`. If you are using older version of this module please refer to the [old readme](https://github.com/lwansbrough/react-native-camera/tree/8cc61edef2c018b81e1c52f13c7d261fe6a35a63).
2016-02-03 16:57:25 +00:00
2015-04-24 17:52:00 +00:00
![](https://i.imgur.com/5j2JdUk.gif)
2015-04-01 01:02:57 +00:00
## Getting started
### Mostly automatic install
1. `npm install rnpm --global`
2016-02-03 16:57:25 +00:00
2. `npm install react-native-camera@https://github.com/lwansbrough/react-native-camera.git --save`
3. `rnpm link react-native-camera`
2015-04-01 01:02:57 +00:00
### Manual install
#### iOS
2016-02-04 03:18:00 +00:00
1. `npm install react-native-camera@https://github.com/lwansbrough/react-native-camera.git --save`
2. In XCode, in the project navigator, right click `Libraries``Add Files to [your project's name]`
3. Go to `node_modules``react-native-camera` and add `RCTCamera.xcodeproj`
4. In XCode, in the project navigator, select your project. Add `libRCTCamera.a` to your project's `Build Phases``Link Binary With Libraries`
5. Click `RCTCamera.xcodeproj` in the project navigator and go the `Build Settings` tab. Make sure 'All' is toggled on (instead of 'Basic'). In the `Search Paths` section, look for `Header Search Paths` and make sure it contains both `$(SRCROOT)/../../react-native/React` and `$(SRCROOT)/../../../React` - mark both as `recursive`.
2015-04-01 17:39:51 +00:00
5. Run your project (`Cmd+R`)
2015-04-01 01:02:57 +00:00
#### Android
2016-02-04 03:18:00 +00:00
1. `npm install react-native-camera@https://github.com/lwansbrough/react-native-camera.git --save`
2016-02-03 02:04:05 +00:00
2. Open up `android/app/src/main/java/[...]/MainActivity.java
- Add `import com.lwansbrough.RCTCamera.*;` to the imports at the top of the file
2016-02-01 04:47:59 +00:00
- Add `new RCTCameraPackage()` to the list returned by the `getPackages()` method
3. Append the following lines to `android/settings.gradle`:
```
include ':react-native-camera'
project(':react-native-camera').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-camera/android')
```
4. Insert the following lines inside the dependencies block in `android/app/build.gradle`:
```
compile project(':react-native-camera')
```
2015-04-01 01:02:57 +00:00
## Usage
All you need is to `require` the `react-native-camera` module and then use the
`<Camera/>` tag.
2015-04-01 01:45:47 +00:00
```javascript
2016-02-01 04:47:59 +00:00
'use strict';
import React, { Component } from 'react';
import {
2015-04-01 01:02:57 +00:00
AppRegistry,
2016-02-01 04:47:59 +00:00
Dimensions,
2015-04-01 01:02:57 +00:00
StyleSheet,
Text,
2016-02-01 04:47:59 +00:00
TouchableHighlight,
View
} from 'react-native';
import Camera from 'react-native-camera';
2016-02-01 04:47:59 +00:00
class BadInstagramCloneApp extends Component {
render() {
2015-04-01 01:02:57 +00:00
return (
2016-02-01 04:47:59 +00:00
<View style={styles.container}>
<Camera
ref={(cam) => {
this.camera = cam;
}}
style={styles.preview}
2016-03-03 02:03:51 +00:00
aspect={Camera.constants.Aspect.fill}>
2016-02-01 04:47:59 +00:00
<Text style={styles.capture} onPress={this.takePicture.bind(this)}>[CAPTURE]</Text>
</Camera>
</View>
2015-04-01 01:02:57 +00:00
);
2016-02-01 04:47:59 +00:00
}
2016-02-24 18:59:59 +00:00
2016-02-01 04:47:59 +00:00
takePicture() {
this.camera.capture()
.then((data) => console.log(data))
.catch(err => console.error(err));
}
}
const styles = StyleSheet.create({
2015-05-05 06:11:10 +00:00
container: {
2016-02-01 04:47:59 +00:00
flex: 1
},
preview: {
2015-05-05 06:11:10 +00:00
flex: 1,
2016-02-01 04:47:59 +00:00
justifyContent: 'flex-end',
2015-05-05 06:11:10 +00:00
alignItems: 'center',
2016-02-01 04:47:59 +00:00
height: Dimensions.get('window').height,
width: Dimensions.get('window').width
2015-05-05 06:11:10 +00:00
},
2016-02-01 04:47:59 +00:00
capture: {
flex: 0,
backgroundColor: '#fff',
borderRadius: 5,
color: '#000',
padding: 10,
margin: 40
}
2015-05-05 06:11:10 +00:00
});
2016-02-01 04:47:59 +00:00
AppRegistry.registerComponent('BadInstagramCloneApp', () => BadInstagramCloneApp);
2015-04-01 01:02:57 +00:00
```
## Properties
2015-04-01 02:38:30 +00:00
#### `aspect`
Values: `Camera.constants.Aspect.fit` or `"fit"`, `Camera.constants.Aspect.fill` or `"fill"` (default), `Camera.constants.Aspect.stretch` or `"stretch"`
The `aspect` property allows you to define how your viewfinder renders the camera's view. For instance, if you have a square viewfinder and you want to fill the it entirely, you have two options: `"fill"`, where the aspect ratio of the camera's view is preserved by cropping the view or `"stretch"`, where the aspect ratio is skewed in order to fit the entire image inside the viewfinder. The other option is `"fit"`, which ensures the camera's entire view fits inside your viewfinder without altering the aspect ratio.
2016-02-01 04:15:00 +00:00
#### `iOS` `captureAudio`
Values: `true` (default), `false` (Boolean)
*Applies to video capture mode only.* Specifies whether or not audio should be captured with the video.
2016-02-01 04:15:00 +00:00
#### `iOS` `captureMode`
Values: `Camera.constants.CaptureMode.still` (default), `Camera.constants.CaptureMode.video`
The type of capture that will be performed by the camera - either a still image or video.
#### `captureTarget`
Values: `Camera.constants.CaptureTarget.cameraRoll` (default), `Camera.constants.CaptureTarget.disk`, `Camera.constants.CaptureTarget.temp`, ~~`Camera.constants.CaptureTarget.memory`~~ (deprecated),
This property allows you to specify the target output of the captured image data. By default the image binary is sent back as a base 64 encoded string. The disk output has been shown to improve capture response time, so that is the recommended value.
2015-04-01 02:38:30 +00:00
2016-03-20 19:31:23 +00:00
#### `iOS` `captureQuality`
2016-04-23 19:59:17 +00:00
Values: `Camera.constants.CaptureQuality.high` or `"high"` (default), `Camera.constants.CaptureQuality.medium` or `"medium"`, `Camera.constants.CaptureQuality.low` or `"low"`, `Camera.constants.CaptureQuality.photo` or `"photo"`.
2016-03-20 19:31:23 +00:00
This property allows you to specify the quality output of the captured image or video. By default the quality is set to high.
2015-04-01 02:38:30 +00:00
#### `type`
Values: `Camera.constants.Type.front` or `"front"`, `Camera.constants.Type.back` or `"back"` (default)
Use the `type` property to specify which camera to use.
2015-04-01 02:38:30 +00:00
#### `orientation`
Values:
`Camera.constants.Orientation.auto` or `"auto"` (default),
`Camera.constants.Orientation.landscapeLeft` or `"landscapeLeft"`, `Camera.constants.Orientation.landscapeRight` or `"landscapeRight"`, `Camera.constants.Orientation.portrait` or `"portrait"`, `Camera.constants.Orientation.portraitUpsideDown` or `"portraitUpsideDown"`
2015-04-01 02:38:30 +00:00
The `orientation` property allows you to specify the current orientation of the phone to ensure the viewfinder is "the right way up."
2015-04-01 02:38:30 +00:00
#### `Android` `playSoundOnCapture`
Values: `true` (default) or `false`
This property allows you to specify whether a sound is played on capture. It is currently android only, pending [a reasonable mute implementation](http://stackoverflow.com/questions/4401232/avfoundation-how-to-turn-off-the-shutter-sound-when-capturestillimageasynchrono) in iOS.
2016-02-01 04:15:00 +00:00
#### `iOS` `onBarCodeRead`
2015-04-19 22:53:30 +00:00
Will call the specified method when a barcode is detected in the camera's view.
Event contains `data` (the data in the barcode) and `bounds` (the rectangle which outlines the barcode.)
2015-07-26 19:32:42 +00:00
The following barcode types can be recognised:
- `aztec`
- `code138`
- `code39`
- `code39mod43`
- `code93`
- `ean13`
- `ean8`
- `pdf417`
- `qr`
- `upce`
2016-02-13 05:59:29 +00:00
- `interleaved2of5` (when available)
- `itf14` (when available)
2015-11-05 19:25:27 +00:00
- `datamatrix` (when available)
2015-07-26 19:32:42 +00:00
The barcode type is provided in the `data` object.
#### `iOS` `barCodeTypes`
An array of barcode types to search for. Defaults to all types listed above. No effect if `onBarCodeRead` is undefined.
2015-05-30 16:36:55 +00:00
#### `flashMode`
Values:
`Camera.constants.FlashMode.on`,
`Camera.constants.FlashMode.off`,
`Camera.constants.FlashMode.auto`
Use the `flashMode` property to specify the camera flash mode.
2015-06-15 18:58:36 +00:00
#### `torchMode`
Values:
`Camera.constants.TorchMode.on`,
`Camera.constants.TorchMode.off`,
`Camera.constants.TorchMode.auto`
Use the `torchMode` property to specify the camera torch mode.
2016-02-01 03:46:33 +00:00
#### `onFocusChanged: Event { nativeEvent: { touchPoint: { x, y } }`
2016-02-01 03:46:33 +00:00
Called when a touch focus gesture has been made.
2015-08-29 08:29:10 +00:00
By default, `onFocusChanged` is not defined and tap-to-focus is disabled.
#### `defaultOnFocusComponent`
Values:
`true` (default)
`false`
2015-08-29 08:29:10 +00:00
If `defaultOnFocusComponent` set to false, default internal implementation of visual feedback for tap-to-focus gesture will be disabled.
2016-02-01 03:46:33 +00:00
#### `onZoomChanged: Event { nativeEvent: { velocity, zoomFactor } }`
2016-02-01 03:46:33 +00:00
Called when focus has changed.
2015-08-29 08:29:10 +00:00
By default, `onZoomChanged` is not defined and pinch-to-zoom is disabled.
2016-02-08 12:08:10 +00:00
#### `iOS` `keepAwake`
If set to `true`, the device will not sleep while the camera preview is visible. This mimics the behavior of the default camera app, which keeps the device awake while open.
#### `iOS` `mirrorImage`
If set to `true`, the image returned will be mirrored..
2016-02-01 03:46:33 +00:00
## Component instance methods
You can access component methods by adding a `ref` (ie. `ref="camera"`) prop to your `<Camera>` element, then you can use `this.refs.camera.capture(cb)`, etc. inside your component.
2016-01-28 11:14:00 +00:00
#### `capture([options]): Promise`
2016-03-31 19:59:26 +00:00
Captures data from the camera. What is captured is based on the `captureMode` and `captureTarget` props. `captureMode` tells the camera whether you want a still image or video. `captureTarget` allows you to specify how you want the data to be captured and sent back to you. See `captureTarget` under Properties to see the available values.
Supported options:
- `audio` (See `captureAudio` under Properties)
- `mode` (See `captureMode` under Properties)
- `target` (See `captureTarget` under Properties)
- `metadata` This is metadata to be added to the captured image.
- `location` This is the object returned from `navigator.geolocation.getCurrentPosition()` (React Native's geolocation polyfill). It will add GPS metadata to the image.
- `rotation` This will rotate the image by the number of degrees specified.
2016-03-31 19:59:26 +00:00
The promise will be fulfilled with an object with some of the following properties:
- `data`: Returns a base64-encoded string with the capture data (only returned with the deprecated `Camera.constants.CaptureTarget.memory`)
- `path`: Returns the path of the captured image or video file on disk
- `width`: (currently iOS video only) returns the video file's frame width
- `height`: (currently iOS video only) returns the video file's frame height
- `duration`: (currently iOS video only) video file duration
- `size`: (currently iOS video only) video file size (in bytes)
2016-02-01 03:46:33 +00:00
#### `iOS` `getFOV(): Promise`
Returns the camera's current field of view.
2016-03-27 00:27:40 +00:00
#### `hasFlash(): Promise`
2016-02-01 03:46:33 +00:00
Returns whether or not the camera has flash capabilities.
2015-07-11 00:07:44 +00:00
#### `stopCapture()`
Ends the current capture session for video captures. Only applies when the current `captureMode` is `video`.
2016-02-01 03:46:33 +00:00
## Component static methods
#### `Camera.checkDeviceAuthorizationStatus(): Promise`
2016-01-28 11:14:00 +00:00
Exposes the native API for checking if the device has authorized access to the camera. Can be used to call before loading the Camera component to ensure proper UX. The promise will be fulfilled with `true` or `false` depending on whether the device is authorized.
## Subviews
This component supports subviews, so if you wish to use the camera view as a background or if you want to layout buttons/images/etc. inside the camera then you can do that.
2016-05-29 16:55:51 +00:00
## Example
To see more of the `react-native-camera` in action, you can check out the `Example` folder.
2015-04-01 01:02:57 +00:00
------------
Thanks to Brent Vatne (@brentvatne) for the `react-native-video` module which provided me with a great example of how to set up this module.