$ npm install react-native-camera --save
$ cd ios && pod install && cd ..
iOS required steps
Add permissions with usage descriptions to your app's Info.plist
ios/ReactNativeCameraBoilerplage/Info.plis
<!-- Required with iOS 10 and higher -->
<key>NSCameraUsageDescription</key>
<string>Your message to user when the camera is accessed for the first time</string>
<!-- Required with iOS 11 and higher: 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>
<!-- 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>
Android required steps
Add permissions to your AndroidManifest.xml file
android/app/src/main/AndroidManifest.xml
<!-- Required -->
<uses-permission android:name="android.permission.CAMERA" />
<!-- Include this only if you are planning to use the camera roll -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Include this only if you are planning to use the microphone for video recording -->
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
Make sure to shut down your app and run it again so the changes are applied
Coding time
Let's start by creating our directory structure. Once you are done creating the directories your project should look like this.
Edit the Camera.js component. The finished result should look similar to this, a functional component that can be imported into any screens that need it.
Once install update Camera.js to look like the following. Notice that we are also setting the default props which are the default unless the parent overrides them.
Edit the camera container and import our camera component
// src/containers/CameraScreen.js
import React, {Component} from 'react';
import {View, Text} from 'react-native';
import Camera from '../components/Camera/Camera';
export default class CameraScreen extends Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return (
<View>
<Camera />
</View>
);
}
}
Build the camera layout
Install @nartc/react-native-barcode-mask andit's dependency react-native-reanimated to get us a nice viewfinder so we don't have to build one from scratch. Also import SafeAreaView from React Native so we can handle iPhoneX notches.
$ npm install @nartc/react-native-barcode-mask
$ npm install react-native-reanimated
$ cd ios && pod install && cd ..
Here is the updated code. Also don't forget to recompile your app after linking native modules.
The library will help us reduce errors once we deploy this app to the app/play store. We get runtime type checking for React props. React will check props passed to our camera component against the prop type definitions set, and warn in development if they don't match.
Use to generate or find a nice icon for the not authorized camera view
Create a new directory to keep our images in src/images and download a png from icon finder. allows you to download any of their icons in three sizes. Save them as: