A Camera component for React Native. Also supports barcode scanning!
Go to file
Roman Volosovskyi c63caed29e
skip permissions request
2018-04-04 14:11:43 +01:00
.github Update ISSUE_TEMPLATE.md 2018-03-21 08:34:47 -03:00
android Crops Preview on Android 2018-04-03 21:34:40 +02:00
docs Add entry to docs 2018-04-03 15:30:18 +02:00
ios add bounds to barcodeReadEvent 2018-03-26 23:15:59 +02:00
postinstall_project fix(ios-project): fix path to parent's ios project 2018-03-13 11:37:56 -03:00
src skip permissions request 2018-04-04 14:11:43 +01:00
types feat(types): add types for #1428 2018-04-03 10:39:51 -03:00
.eslintrc chore(eslint): add eslint and prettier 2017-12-31 13:30:33 -02:00
.gitattributes disable git large files 2018-02-01 11:26:07 -02:00
.gitignore [git] add package-lock.json 2017-06-15 13:32:46 -07:00
.npmignore remove RNCameraExample from npm release 2018-02-01 11:24:57 -02:00
CHANGELOG.md Removed changes in 1.0.3 due to (later reverted) merge 2018-03-24 16:45:58 +01:00
LICENSE Initial commit 2015-03-31 17:54:41 -07:00
README.md Update readme to reflect changes made in #1418 (#1423) 2018-03-31 08:41:46 +02:00
THIRD-PARTY-LICENSES rename expo to simple RN, add js files, and export them inside the same camera package 2018-01-01 19:16:02 -02:00
changelog.js feat(changelog): add changelog script 2018-02-14 17:35:20 -02:00
commitlint.config.js feat(docs): add docs to v1.0.0 2018-02-12 07:51:54 -02:00
package-lock.json 1.0.3 2018-03-24 12:30:30 -03:00
package.json 1.0.3 2018-03-24 12:30:30 -03:00
react-native-camera.podspec Also include RCT as the JS code requires it. 2018-03-06 15:22:26 +01:00
yarn.lock feat(changelog): add changelog script 2018-02-14 17:35:20 -02:00

README.md

React Native Camera Backers on Open Collective Sponsors on Open Collective npm version npm downloads

The comprehensive camera module for React Native. Including photographs, videos, face detection, barcode scanning and text recognition (Android only)!

import { RNCamera, FaceDetector } from 'react-native-camera';

How to use master branch?

Inside your package.json, use this "react-native-camera": "git+https://git@github.com/react-native-community/react-native-camera" instead of "react-native-camera": "^1.0.0".

Contributing

  • Pull Requests are welcome, if you open a pull request we will do our best to get to it in a timely manner
  • Pull Request Reviews and even more welcome! we need help testing, reviewing, and updating open PRs
  • If you are interested in contributing more actively, please contact me (same username on Twitter, Facebook, etc.) Thanks!
  • We are now on Open Collective! Contributions are appreciated and will be used to fund core contributors. more details
  • If you want to help us coding, join Expo slack https://slack.expo.io/, so we can chat over there. (#react-native-camera)
Permissions

To use the camera on Android you must ask for camera permission:

  <uses-permission android:name="android.permission.CAMERA" />

To enable video recording feature you have to add the following code to the AndroidManifest.xml:

  <uses-permission android:name="android.permission.RECORD_AUDIO"/>
  <uses-permission android:name="android.permission.RECORD_VIDEO"/>
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

5j2jduk

Migrating from RCTCamera to RNCamera

See this doc

RNCamera Docs

RNCamera

Docs old RCTCamera

RCTCamera

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:
<key>NSCameraUsageDescription</key>
<string>Your message to user when the camera is accessed for the first time</string>

<!-- Include this only if you are planning to use the camera roll -->
<key>NSPhotoLibraryUsageDescription</key>
<string>Your message to user when the photo library is accessed for the first time</string>

<!-- Include this only if you are planning to use the microphone for video recording -->
<key>NSMicrophoneUsageDescription</key>
<string>Your message to user when the microphone is accessed for the first time</string>
  1. On Android, you require buildToolsVersion of 25.0.2+. This should easily and automatically be downloaded by Android Studio's SDK Manager.

  2. On iOS 11 and later you need to add NSPhotoLibraryAddUsageDescription key to the Info.plist. This key lets you describe the reason your app seeks write-only access to the users photo library. Info.plist can be found in 'your_project/ios/your_project/Info.plist'. Add the following code:

<!-- Include this only if you are planning to use the camera roll -->
<key>NSPhotoLibraryAddUsageDescription</key>
<string>Your message to user when the photo library is accessed for the first time</string>

Mostly automatic install with react-native

  1. npm install react-native-camera --save
  2. react-native link react-native-camera

Mostly automatic install with CocoaPods

  1. npm install react-native-camera --save
  2. Add the plugin dependency to your Podfile, pointing at the path where NPM installed it:
pod 'react-native-camera', path: '../node_modules/react-native-camera'
  1. Run pod install

Manual install

iOS

  1. npm install react-native-camera --save
  2. In XCode, in the project navigator, right click LibrariesAdd Files to [your project's name]
  3. Go to node_modulesreact-native-camera and add RNCamera.xcodeproj
  4. Expand the RNCamera.xcodeprojProducts folder
  5. In XCode, in the project navigator, select your project. Add libRNCamera.a to your project's Build PhasesLink Binary With Libraries
  6. Click RNCamera.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.

Face Detection or Text Recognition Steps

Face Detection is optional on iOS. If you want it, you are going to need to install Google Mobile Vision frameworks in your project, as mentioned in the next section.

No Face Detection steps

If you do not need it and do not want to install the GMV frameworks, open your app xcode project, on the Project Navigator, expand the RNCamera project, right click on the FaceDetector folder and delete it (move to trash, if you want). If you keep that folder and do not follow the GMV installation steps, your project will not compile.

If you want to make this automatic, you can add a postinstall script to your app package.json. Inside the postinstall_project there is a xcode project ready with the folder removed (we opened xcode, removed the folder from the project and copied the resulting project file). The post install script is:

#!/bin/bash
echo "Creating project without FaceDetector"
if [ -e node_modules/react-native-camera/ios/FaceDetector ] ; then
  rm -rf node_modules/react-native-camera/ios/FaceDetector
fi
cp node_modules/react-native-camera/postinstall_project/projectWithoutFaceDetection.pbxproj node_modules/react-native-camera/ios/RNCamera.xcodeproj/project.pbxproj

And add something like this to the scripts section in your package.json:

Note: The face detection code is excluded by default for the CocoaPods installation.

"postinstall": "./scripts/post.sh",
Installing GMV frameworks

GMV (Google Mobile Vision) is used for Face detection by the iOS RNCamera. You have to link the google frameworks to your project to successfully compile the RNCamera project.

  1. If using CocoaPods modify the dependency towards react-native-camera in your Podfile, from
pod 'react-native-camera', path: '../node_modules/react-native-camera'

to

pod 'react-native-camera', subspecs: ['RCT', 'RN', 'FaceDetector'], path: '../node_modules/react-native-camera'
  1. Download: Google Symbol Utilities: https://www.gstatic.com/cpdc/dbffca986f6337f8-GoogleSymbolUtilities-1.1.1.tar.gz

    Google Utilities: https://dl.google.com/dl/cpdc/978f81964b50a7c0/GoogleUtilities-1.3.2.tar.gz

    Google Mobile Vision: https://dl.google.com/dl/cpdc/df83c97cbca53eaf/GoogleMobileVision-1.1.0.tar.gz

    Google network Utilities: https://dl.google.com/dl/cpdc/54fd7b7ef8fd3edc/GoogleNetworkingUtilities-1.2.2.tar.gz

    Google Interchange Utilities: https://dl.google.com/dl/cpdc/1a7f7ba905b2c029/GoogleInterchangeUtilities-1.2.2.tar.gz

  2. Extract everything to one folder. Delete "BarcodeDetector" and "copy" folders from Google Mobile Vision.

  3. Open XCode, right click on your project and choose "New Group". Rename the new folder to "Frameworks". Right click on "Frameworks" and select "add files to 'YOUR_PROJECT'". Select all content from the folder of step 2, click on Options. Select "Copy items if needed", leave "Create groups" selected and choose all your targets on the "Add to targets" section. Then, click on "Add".

  4. On your target -> Build Phases -> Link Binary with Libraries -> add AddressBook.framework

  5. On your target -> Build Settings -> Other Linker Flags -> add -lz, -ObjC and -lc++

  6. To force indexing and prevent errors, restart xcode and reopen your project again before compiling.

Android

  1. npm install react-native-camera --save
  2. Open up android/app/src/main/java/[...]/MainApplication.java
  • Add import org.reactnative.camera.RNCameraPackage; to the imports at the top of the file
  • Add new RNCameraPackage() to the list returned by the getPackages() method. Add a comma to the previous item if there's already something there.
  1. 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')
    
  2. Insert the following lines inside the dependencies block in android/app/build.gradle:

    compile (project(':react-native-camera')) {
        exclude group: "com.google.android.gms"
        compile 'com.android.support:exifinterface:25.+'
        compile ('com.google.android.gms:play-services-vision:12.0.1') {
            force = true
        }
    }
    

You may need to use different exifinterface versions, e.g. 27.+ instead of 25.+.

  1. Declare the permissions in your Android Manifest (required for video recording feature)
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  1. Add jitpack to android/build.gradle
allprojects {
    repositories {
        maven { url "https://jitpack.io" }
        maven { url "https://maven.google.com" }
    }
}

The current Android library defaults to the below values for the Google SDK and Libraries,

def DEFAULT_COMPILE_SDK_VERSION             = 26
def DEFAULT_BUILD_TOOLS_VERSION             = "26.0.2"
def DEFAULT_TARGET_SDK_VERSION              = 26
def DEFAULT_GOOGLE_PLAY_SERVICES_VERSION    = "12.0.1"
def DEFAULT_SUPPORT_LIBRARY_VERSION         = "27.1.0"

You can override this settings by adding a Project-wide gradle configuration properties for use by all modules in your ReactNative project by adding the below to android/build.gradle file,

buildscript {...}

allprojects {...}

/**
* Project-wide gradle configuration properties for use by all modules
*/
ext {
    compileSdkVersion           = 26
    targetSdkVersion            = 26
    buildToolsVersion           = "26.0.2"
    googlePlayServicesVersion   = "12.0.1"
    supportLibVersion           = "27.1.0"
}

The above settings in the ReactNative project over-rides the values present in the react-native-camera module. For your reference below is the android/build.gradle file of the module.

buildscript {
...

def DEFAULT_COMPILE_SDK_VERSION             = 26
def DEFAULT_BUILD_TOOLS_VERSION             = "26.0.2"
def DEFAULT_TARGET_SDK_VERSION              = 26
def DEFAULT_GOOGLE_PLAY_SERVICES_VERSION    = "12.0.1"
def DEFAULT_SUPPORT_LIBRARY_VERSION         = "27.1.0"

android {
  compileSdkVersion rootProject.hasProperty('compileSdkVersion') ? rootProject.compileSdkVersion : DEFAULT_COMPILE_SDK_VERSION
  buildToolsVersion rootProject.hasProperty('buildToolsVersion') ? rootProject.buildToolsVersion : DEFAULT_BUILD_TOOLS_VERSION

  defaultConfig {
    minSdkVersion 16
    targetSdkVersion rootProject.hasProperty('targetSdkVersion') ? rootProject.targetSdkVersion : DEFAULT_TARGET_SDK_VERSION

    versionCode 1
    versionName "1.0.0"
  }
  lintOptions {
    abortOnError false
    warning 'InvalidPackage'
  }
}

...

dependencies {
  def googlePlayServicesVersion = rootProject.hasProperty('googlePlayServicesVersion')  ? rootProject.googlePlayServicesVersion : DEFAULT_GOOGLE_PLAY_SERVICES_VERSION
  def supportLibVersion = rootProject.hasProperty('supportLibVersion')  ? rootProject.supportLibVersion : DEFAULT_SUPPORT_LIBRARY_VERSION

  compile 'com.facebook.react:react-native:+'
  compile "com.google.zxing:core:3.2.1"
  compile "com.drewnoakes:metadata-extractor:2.9.1"
  compile 'com.google.android.gms:play-services-vision:$googlePlayServicesVersion'
  compile 'com.android.support:exifinterface:$supportLibVersion'

  compile 'com.github.react-native-community:cameraview:cc47bb28ed2fc54a8c56a4ce9ce53edd1f0af3a5'
}

Follow the Q & A section if you are having compilation issues.

Usage

RNCamera

Take a look into this documentation.

RCTCamera

Since 1.0.0, RCTCamera is deprecated, but if you want to use it, you can see its documentation.

Open Collective

We are just beginning a funding campaign for react-native-camera. Contributions are greatly appreciated. When we gain more than $250 we will begin distributing funds to core maintainers in a fully transparent manner. Feedback for this process is welcomed, we will continue to evolve the strategy as we grow and learn more.

Backers

Support us with a monthly donation and help us continue our activities. [Become a backer]

Sponsors

Become a sponsor and get your logo on our README on Github with a link to your site. [Become a sponsor]


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.