Contents
React Native Camera Kit
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
cd ProjectName
Run the following command
npm install react-native-camera-kit --save
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
cd ios && pod install && cd ..
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.
<uses-permission
android:name="android.permission.CAMERA"
/>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
/>
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
/>
Permission | Purpose |
---|---|
CAMERA | To access the camera |
WRITE_EXTERNAL_STORAGE | To store the content in the SD card |
READ_EXTERNAL_STORAGE | To 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
// React Native Camera
// https://aboutreact.com/react-native-camera/
// import React in our code
import React, {useState} from 'react';
// import all the components we are going to use
import {
SafeAreaView,
StyleSheet,
Text,
View,
PermissionsAndroid,
Alert,
Platform,
TouchableHighlight,
} from 'react-native';
// import CameraKitCameraScreen
import {CameraKitCameraScreen} from 'react-native-camera-kit';
const App = () => {
const [isPermitted, setIsPermitted] = useState(false);
const [captureImages, setCaptureImages] = useState([]);
const requestCameraPermission = async () => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.CAMERA,
{
title: 'Camera Permission',
message: 'App needs camera permission',
},
);
// If CAMERA Permission is granted
return granted === PermissionsAndroid.RESULTS.GRANTED;
} catch (err) {
console.warn(err);
return false;
}
};
const requestExternalWritePermission = async () => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
{
title: 'External Storage Write Permission',
message: 'App needs write permission',
},
);
// If WRITE_EXTERNAL_STORAGE Permission is granted
return granted === PermissionsAndroid.RESULTS.GRANTED;
} catch (err) {
console.warn(err);
alert('Write permission err', err);
}
return false;
};
const requestExternalReadPermission = async () => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
{
title: 'Read Storage Permission',
message: 'App needs Read Storage Permission',
},
);
// If READ_EXTERNAL_STORAGE Permission is granted
return granted === PermissionsAndroid.RESULTS.GRANTED;
} catch (err) {
console.warn(err);
alert('Read permission err', err);
}
return false;
};
const openCamera = async () => {
if (Platform.OS === 'android') {
if (await requestCameraPermission()) {
if (await requestExternalWritePermission()) {
if (await requestExternalReadPermission()) {
setIsPermitted(true);
} else alert('READ_EXTERNAL_STORAGE permission denied');
} else alert('WRITE_EXTERNAL_STORAGE permission denied');
} else alert('CAMERA permission denied');
} else {
setIsPermitted(true);
}
};
const onBottomButtonPressed = (event) => {
const images = JSON.stringify(event.captureImages);
if (event.type === 'left') {
setIsPermitted(false);
} else if (event.type === 'right') {
setIsPermitted(false);
setCaptureImages(images);
} else {
Alert.alert(
event.type,
images,
[{text: 'OK', onPress: () => console.log('OK Pressed')}],
{cancelable: false},
);
}
};
return (
<SafeAreaView style={{flex: 1}}>
{isPermitted ? (
<View style={{flex: 1}}>
<CameraKitCameraScreen
// Buttons to perform action done and cancel
actions={{
rightButtonText: 'Done',
leftButtonText: 'Cancel'
}}
onBottomButtonPressed={
(event) => onBottomButtonPressed(event)
}
flashImages={{
// Flash button images
on: require('./assets/flashon.png'),
off: require('./assets/flashoff.png'),
auto: require('./assets/flashauto.png'),
}}
cameraFlipImage={require('./assets/flip.png')}
captureButtonImage={require('./assets/capture.png')}
/>
</View>
) : (
<View style={styles.container}>
<Text style={styles.titleText}>React Native Camera</Text>
<Text style={styles.textStyle}>{captureImages}</Text>
<TouchableHighlight
onPress={openCamera}
style={styles.buttonStyle}
>
<Text style={styles.buttonTextStyle}>Open Camera</Text>
</TouchableHighlight>
</View>
)}
</SafeAreaView>
);
};
export default App;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
padding: 10,
alignItems: 'center',
},
titleText: {
fontSize: 22,
textAlign: 'center',
fontWeight: 'bold',
},
textStyle: {
color: 'black',
fontSize: 16,
textAlign: 'center',
padding: 10,
marginTop: 16,
},
buttonStyle: {
fontSize: 16,
color: 'white',
backgroundColor: 'green',
padding: 5,
marginTop: 32,
minWidth: 250,
},
buttonTextStyle: {
padding: 5,
color: 'white',
textAlign: 'center',
},
});
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 (macOS only)
react-native run-ios
Output Screenshots
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. 🙂
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.
Hi i am trying scan Barcode.
and read JAN code.
please teach how to read JAN code.
Can you please look at the below example. If you still facing the issue then please revert me on aboutreact11@gmail.com
https://aboutreact.com/react-native-scan-qr-code/
hello bro my camera not access why i.m use expo
It will not work in Expo.
Hello. Thanks for the great tutorial.
Could you please let me know if there is a way to resize the camera view? For instance, if I want the camera view to take up only half the screen size, is there a way to achieve that?
Once again, thank you for the tutorial.
Also, I can’t see the image at the bottom. Any help in that case will be appreciated.
Hi Sagar,
I haven’t tried it but you can try to change the style of CameraKitCameraScreen to make it as per your requirement. I’ll try it once and let you know how to do it.