Camera App
Building a Camera App
Create a new application
Run the following commands from your terminal
$ npx react-native init ReactNativeCamera
$ cd ReactNativeCamera
$ npx react-native start
$ npx react-native run-iosInstall and link dependencies
$ 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
Coding time
Let's start by creating our directory structure. Once you are done creating the directories your project should look like this.
Now create a camera container component and a root container component
Create a camera component. React Native allows to write really modular code with components as the driving force allowing you to reuse code easily.
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.
Install prop-types
The prop-types 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.
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
Build the camera layout
Install @nartc/react-native-barcode-mask and it'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.
Here is the updated code. Also don't forget to recompile your app after linking native modules.
Add shutter, flash and zoom controls
Create a new component named CameraControls.js
Edit CameraControls.js and it should look something similar to this
Display a camera in our app
Start by editing RootContainer.js and import CameraScreen.js
Now let's update App.js to display the camera. Start by removing everything already there and replace it with the following.
Build nicer auth error and auth pending views for the camera
Install react-native-vector-icons
Edit Info.plist and add a property called Fonts provided by application (or UIAppFonts) to XCode.
Use IconFinder 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. IconFinder allows you to download any of their icons in three sizes. Save them as:
Create two new components in src/components/Camera
Edit the not authorized view component and style it. We are using PropTypes and variables to make our view as reusable as possible.

Similarly here is the pending auth view.

Last updated
Was this helpful?