React Native Camera

What Is React Native Camera?


React Native Camera (RNCamera) is the go-to thing whilst it comes to imposing digicam capability in a React Native app. This component facilitates you communicate with the local OS via some simple functions so you can use tool hardware. 



You can build your apps round these capabilities without entering into the hassle of local code. RNCamera already supports:

  • Photographs
  • Videos
  • Face detection
  • Barcode scanning
  • Text recognition (Android only)
Note that RNCamera used to are available two flavors:

  • RNCamera
  • RCTCamera (deprecated)
…so ensure to use RNCamera so you can keep getting the cutting-edge updates.

Creating Your First App Using RNCamera
Before we begin our React Native QR scanner, there are some dependencies we’ll want to install. 

Follow for more at Reactjs Certification Course

Installing RNCamera’s Dependencies
Our setup needs no less than JDK version 1.7 (that you maximum probably have) and if you’re on Android you’ll require build tools version more modern than 25.0.2. (To be sure, there may be an extra particular and up-to-date list in the docs.)

First, let’s get started with the aid of creating a new React Native project:

react-native init CameraExample

Now let’s deploy the first version of our React Native Camera example over our phone.
cd CameraExample
react-native run-android
It should look like the screenshot below:
npm install react-native-camera --save
react-native link react-native-camera
You must also add the following permissions to android/app/src/main/AndroidManifest.xml:
   package="com.cameraexample">

     <uses-permission android:name="android.permission.INTERNET" />
+    <uses-permission android:name="android.permission.CAMERA" />
+    <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" />

     <application
       android:name=".MainApplication"
You’ll also need to set the dimension strategy in android/app/build.gradle—it has no default and you’ll get an error if you don’t define it:
     defaultConfig {
         applicationId "com.cameraexample"
         minSdkVersion rootProject.ext.minSdkVersion
+        missingDimensionStrategy 'react-native-camera', 'general'
         targetSdkVersion rootProject.ext.targetSdkVersion
         versionCode 1
         versionName "1.0"
Note: The dimension strategy should normally be set to general as above. However, you can set it to mlkit instead, if you’d like to use MLKit for text/face/barcode recognition.
After the installation, you will need to use run-android to install the new dependencies:
react-native run-android
If everything went well, you should see the same React Native welcome screen on your device or simulator again.
If you want to learn more about React, then The Complete Reactjs Online Traning is a great course, to begin with.  

Setting Up the Camera
First, let’s begin via editing App.Js and importing RNCamera there:

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

Next, we’ll regulate the render characteristic to consist of our newly imported RNCamera. Notice the style attribute introduced to the digicam for it to take the whole screen. Without those styles, you could now not be capable of seeing the digicam render on your screen, as it will not occupy any space:

render() {
     return (
       <View style={styles.container}>
-        <Text style={styles.welcome}>Welcome to React Native!</Text>
-        <Text style={styles.instructions}>To get started, edit App.js</Text>
-        <Text style={styles.instructions}>{instructions}</Text>
+        <RNCamera
+          ref={ref => {
+            this.camera = ref;
+          }}
+          style={{
+            flex: 1,
+            width: '100%',
+          }}
+        >
+        </RNCamera>
       </View>
     );
   }

After adding this code, your interface should have the camera on, full-screen

Now we can see barcodes but we can’t read them yet. Let’s use RNCamera’s algorithm to recognize what is written inside each of them.

In React Native you may access the camera by using first installing it the use of NPM: npm install react-native-camera --store react-native hyperlink react-native-camera

Then use this in your Component:
takePicture() {
const options = {};
//options.location = ...
this.camera.capture({metadata: options})
  .then((data) => console.log(data))
  .catch(err => console.error(err));
}

Comments

Popular posts from this blog

Informatica Knowledge Base (IKB)

Installing the On-Premises Data Gateway in Power BI

Is Power BI available for Mac?