React Native Camera

We all usually need a camera in our app and Wix has provided a very good library named react-native-camera-kit. Which is very easy to integrate into your app.

In this example, we will make a home screen to open Camera and in the camera, we will have a capture button, a front to back camera switch button, a flash setting button, and a cancel button to close the camera. So let’s get started.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run

npm install -g react-native-cli

Run the following commands to create a new React Native project

react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

This will make a project structure with an index file named App.js in your project directory.

Installation of Dependency

To use <CameraKitCameraScreen /> we need to install react-native-camera-kit package. To install this

Open the terminal and jump into your project

Run the following command

This command will copy all the dependencies into your node_module directory, You can find the directory in node_module the directory named react-native-camera-kit.

–save is optional, it is just to update the react-native-camera-kit dependency in your package.json file.

Installation of Pod

React Native 0.60 brings the auto-linking feature which links the library automatically and you just need to install the pod file using the following command

Permission to use the Camera for Android

We are using a Native API Camera so we need to add some permission into the AndroidManifest.xml file.

Please add the following permissions in your AndroidMnifest.xml. Go to CameraExample -> android -> app -> main -> AndroidMnifest.xml.

PermissionPurpose
CAMERATo access the camera
WRITE_EXTERNAL_STORAGETo store the content in the SD card
READ_EXTERNAL_STORAGETo view the content of the SD card


For more about the permission, you can see this post.

Permission to use the Camera for IOS

Please follow the below steps to add the permission in iOS project to use the camera.

Open the project CameraExample -> ios -> ScannerExample.xcworkspace in Xcode.

1. After opening the project in Xcode click on the project from the left sidebar and you will see multiple options in the workspace.

2. Select info tab which is info.plist

3. Click on the plus button to add a permission key “Privacy-Camera Usage Description” and a value which will be visible when permission dialog pops up.

Code to Run Camera in React Native Application

Open App.js in any code editor and replace the code with the following code.

App.js

Before running the app you need to make a directory called “assets ” to keep the images that we have used. Download and put all the images in assets as shown

                         
 

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

Android

      

      

IOS

 
      
 

That was the React Native Camera. If you have any doubt or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

14 thoughts on “React Native Camera”

  1. The code works fine on android but does not work properly on IOS. It seems the permission functions are only for android so the code keeps getting stuck in the try catch. Any ideas?

    • Hello,
      I have tried it on IOS also but let me confirm if there is any update in it..
      We will confirm you soon.
      Mean while if you found the problem and solution for that please comment to help others..

  2. in this package. in the galleryScreen. if i tap on image the image url is only appended to the state.
    how can i append the full image object with all the attributes i.e filename, size, url etc

    onTapImage(event) {
    const uri = event.nativeEvent.selected;

    if (this.state.images[uri]) {
    delete this.state.images[uri];
    } else {
    this.state.images[uri] = true;
    }
    this.setState({ images: {…this.state.images }})
    }

  3. Hi! I am trying to do as you but there is one problem. nvariant Violation: Invariant Violation: requireNativeComponent: “CameraView” was not found in the UIManager. I do not know what does it mean(((
    I use expo. Is it possible use this library with expo?
    Thank you!

    • Actually, we are using a native feature of the device which needs to add some additional code in the native structure manually or with the “react-native link” but in case of expo it is not possible as it gives you a sandboxed environment to start working.
      So sorry it is not possible on Expo.

  4. i’ve been tried your tutorial about RN Camera, which the icon can small size, i don’t know the size u wear in assets folder.
    and i want to ask you, how can i set the size in this code ?
    im surely want to know how set the size.
    thankyou before 🙂

    • Sorry but there is no way to set the size of the icon. You have to manually reduce the size of the icon. I have tried it on the request of 1 AboutReact family member like you but it wasn’t worked.
      I have used 32px*32px icon in the example.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.