seantis/react-native-camera

Name: react-native-camera

Owner: seantis gmbh

Description: A Camera component for React Native. Also supports barcode scanning!

Created: 2016-11-30 09:46:14.0

Updated: 2016-11-30 09:46:16.0

Pushed: 2016-11-30 10:22:29.0

Homepage:

Size: 561

Language: Java

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Looking for contributors

Hey there, I'm looking for active contributors to help move the development of this branch forward in a stable and timely fashion. I haven't had a need for this module in quite some time and so my person time is not being allocated to it. If you are interested in contributing more actively, please contact me (same username on Twitter, Facebook, etc.) Thanks!

react-native-camera npm version Gitter

A camera module for React Native.

Getting started
Requirements
  1. JDK >= 1.7 (if you run on 1.6 you will get an error on “_cameras = new HashMap<>();“)
  2. With iOS 10 and higher you need to add the “Privacy - Camera Usage Description” key to the info.plist of your project. This should be found in 'your_project/ios/your_project/Info.plist'. Add the following code:
    >NSCameraUsageDescription</key>
    ing>Your message to user when the camera is accesseded for the first time</string>
    

NSPhotoLibraryUsageDescription Your message to user when the photo library is accessed for the first time

NSMicrophoneUsageDescription Your message to user when the microsphone is accessed for the first time

Mostly automatic install with react-native
npm install react-native-camera@https://github.com/lwansbrough/react-native-camera.git --save`
react-native link react-native-camera`

Mostly automatic install with CocoaPods
npm install react-native-camera@https://github.com/lwansbrough/react-native-camera.git --save`
dd the plugin dependency to your Podfile, pointing at the path where NPM installed it:

pod 'react-native-camera', path: '../node_modules/react-native-camera'

un `pod install`

Manual install
 iOS
npm install react-native-camera@https://github.com/lwansbrough/react-native-camera.git --save`
n XCode, in the project navigator, right click `Libraries` ? `Add Files to [your project's name]`
o to `node_modules` ? `react-native-camera` and add `RCTCamera.xcodeproj`
n XCode, in the project navigator, select your project. Add `libRCTCamera.a` to your project's `Build Phases` ? `Link Binary With Libraries`
lick `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`.
un your project (`Cmd+R`)


 Android
npm install react-native-camera@https://github.com/lwansbrough/react-native-camera.git --save`
pen up `android/app/src/main/java/[...]/MainApplication.java
Add `import com.lwansbrough.RCTCamera.RCTCameraPackage;` to the imports at the top of the file
Add `new RCTCameraPackage()` to the list returned by the `getPackages()` method. Add a comma to the previous item if there's already something there.

ppend 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')
```

nsert the following lines inside the dependencies block in `android/app/build.gradle`:

```
compile project(':react-native-camera')
```


sage

you need is to `require` the `react-native-camera` module and then use the
mera/>` tag.

'use strict'; import React, { Component } from 'react'; import { AppRegistry, Dimensions, StyleSheet, Text, TouchableHighlight, View } from 'react-native'; import Camera from 'react-native-camera';

class BadInstagramCloneApp extends Component { render() {

return (
  <View style={styles.container}>
    <Camera
      ref={(cam) => {
        this.camera = cam;
      }}
      style={styles.preview}
      aspect={Camera.constants.Aspect.fill}>
      <Text style={styles.capture} onPress={this.takePicture.bind(this)}>[CAPTURE]</Text>
    </Camera>
  </View>
);

}

takePicture() {

this.camera.capture()
  .then((data) => console.log(data))
  .catch(err => console.error(err));

} }

const styles = StyleSheet.create({ container: {

flex: 1

}, preview: {

flex: 1,
justifyContent: 'flex-end',
alignItems: 'center',
height: Dimensions.get('window').height,
width: Dimensions.get('window').width

}, capture: {

flex: 0,
backgroundColor: '#fff',
borderRadius: 5,
color: '#000',
padding: 10,
margin: 40

} });

AppRegistry.registerComponent('BadInstagramCloneApp', () => BadInstagramCloneApp);

roperties

 `aspect`

es: `Camera.constants.Aspect.fit` or `"fit"`, `Camera.constants.Aspect.fill` or `"fill"` (default), `Camera.constants.Aspect.stretch` or `"stretch"`

`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.

 `iOS` `captureAudio`

es: `true` (Boolean), `false` (default)

lies to video capture mode only.* Specifies whether or not audio should be captured with the video.


 `captureMode`

es: `Camera.constants.CaptureMode.still` (default), `Camera.constants.CaptureMode.video`

type of capture that will be performed by the camera - either a still image or video.

 `captureTarget`

es: `Camera.constants.CaptureTarget.cameraRoll` (default), `Camera.constants.CaptureTarget.disk`, `Camera.constants.CaptureTarget.temp`, ~~`Camera.constants.CaptureTarget.memory`~~ (deprecated),

 property allows you to specify the target output of the captured image data. The disk output has been shown to improve capture response time, so that is the recommended value. When using the deprecated memory output, the image binary is sent back as a base64-encoded string.

 `captureQuality`

es: `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"`, `Camera.constants.CaptureQuality["1080p"]` or `"1080p"`, `Camera.constants.CaptureQuality["720p"]` or `"720p"`, `Camera.constants.CaptureQuality["480p"]` or `"480p"`.

 property allows you to specify the quality output of the captured image or video. By default the quality is set to high.

 choosing more-specific quality settings (1080p, 720p, 480p), note that each platform and device supports different valid picture/video sizes, and actual resolution within each of these quality settings might differ. There should not be too much variance (if any) for iOS; 1080p should give 1920x1080, 720p should give 1280x720, and 480p should give 640x480 (note that iOS 480p therefore is NOT the typical 16:9 HD aspect ratio, and the typically-HD camera preview screen may differ greatly in aspect from what you actually record!!). For Android, expect more variance: on most Androids, 1080p *should* give 1920x1080 and 720p *should* give 1280x720; however, 480p will at "best" be 853x480 (16:9 HD aspect ratio), but falls back/down to 800x480, 720x480, or "worse", depending on what is closest-but-less-than 853x480 and available on the actual device. If your application requires knowledge of the precise resolution of the output image/video, you might consider manually determine the actual resolution itself after capture has completed (particularly for 480p on Android).

oid also supports `Camera.constants.CaptureQuality.preview` or `"preview"` which matches the output image to the same one used in the preview

 `type`

es: `Camera.constants.Type.front` or `"front"`, `Camera.constants.Type.back` or `"back"` (default)

the `type` property to specify which camera to use.


 `orientation`

es:
era.constants.Orientation.auto` or `"auto"` (default),
era.constants.Orientation.landscapeLeft` or `"landscapeLeft"`, `Camera.constants.Orientation.landscapeRight` or `"landscapeRight"`, `Camera.constants.Orientation.portrait` or `"portrait"`, `Camera.constants.Orientation.portraitUpsideDown` or `"portraitUpsideDown"`

`orientation` property allows you to specify the current orientation of the phone to ensure the viewfinder is "the right way up."

 `Android` `playSoundOnCapture`

es: `true` (default) or `false`

 property allows you to specify whether a shutter 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.

 `iOS` `onBarCodeRead`

 call the specified method when a barcode is detected in the camera's view.

t contains `data` (the data in the barcode) and `bounds` (the rectangle which outlines the barcode.)

following barcode types can be recognised:

ztec`
ode138`
ode39`
ode39mod43`
ode93`
an13`
an8`
df417`
r`
pce`
nterleaved2of5` (when available)
tf14` (when available)
atamatrix` (when available)

barcode type is provided in the `data` object.

 `iOS` `barCodeTypes`

rray of barcode types to search for. Defaults to all types listed above. No effect if `onBarCodeRead` is undefined.

 `flashMode`

es:
era.constants.FlashMode.on`,
era.constants.FlashMode.off`,
era.constants.FlashMode.auto`

the `flashMode` property to specify the camera flash mode.

 `torchMode`

es:
era.constants.TorchMode.on`,
era.constants.TorchMode.off`,
era.constants.TorchMode.auto`

the `torchMode` property to specify the camera torch mode.

 `onFocusChanged: Event { nativeEvent: { touchPoint: { x, y } }`

ed when a touch focus gesture has been made.
efault, `onFocusChanged` is not defined and tap-to-focus is disabled.

 `defaultOnFocusComponent`

es:
e` (default)
se`

defaultOnFocusComponent` set to false, default internal implementation of visual feedback for tap-to-focus gesture will be disabled.

 `onZoomChanged: Event { nativeEvent: { velocity, zoomFactor } }`

ed when focus has changed.
efault, `onZoomChanged` is not defined and pinch-to-zoom is disabled.

 `iOS` `keepAwake`

et 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.

 `mirrorImage`

et to `true`, the image returned will be mirrored.

omponent instance methods

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.

 `capture([options]): Promise`

ures 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.

orted 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.

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)

 `iOS` `getFOV(): Promise`

rns the camera's current field of view.

 `hasFlash(): Promise`

rns whether or not the camera has flash capabilities.

 `stopCapture()`

 the current capture session for video captures. Only applies when the current `captureMode` is `video`.

omponent static methods

 `iOS` `Camera.checkDeviceAuthorizationStatus(): Promise`

ses 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.

ubviews
 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.

xample

ee more of the `react-native-camera` in action, you can check out the source in [Example](https://github.com/lwansbrough/react-native-camera/tree/master/Example) folder.

--------

ks to Brent Vatne (@brentvatne) for the `react-native-video` module which provided me with a great example of how to set up this module.

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.