What is expo-camera?
The expo-camera package is a part of the Expo ecosystem and provides a comprehensive API for accessing and using the device's camera in React Native applications. It allows developers to capture photos, record videos, and access various camera settings and features.
What are expo-camera's main functionalities?
Capture Photos
This feature allows you to capture photos using the device's camera. The code sample demonstrates how to set up the Camera component and take a picture when a button is pressed.
```javascript
import React, { useRef } from 'react';
import { View, Button } from 'react-native';
import { Camera } from 'expo-camera';
export default function App() {
const cameraRef = useRef(null);
const takePicture = async () => {
if (cameraRef.current) {
const photo = await cameraRef.current.takePictureAsync();
console.log(photo);
}
};
return (
<View style={{ flex: 1 }}>
<Camera style={{ flex: 1 }} ref={cameraRef} />
<Button title="Take Picture" onPress={takePicture} />
</View>
);
}
```
Record Videos
This feature allows you to record videos using the device's camera. The code sample demonstrates how to set up the Camera component and start/stop video recording when a button is pressed.
```javascript
import React, { useRef, useState } from 'react';
import { View, Button } from 'react-native';
import { Camera } from 'expo-camera';
export default function App() {
const cameraRef = useRef(null);
const [isRecording, setIsRecording] = useState(false);
const recordVideo = async () => {
if (cameraRef.current) {
if (isRecording) {
cameraRef.current.stopRecording();
} else {
setIsRecording(true);
const video = await cameraRef.current.recordAsync();
console.log(video);
setIsRecording(false);
}
}
};
return (
<View style={{ flex: 1 }}>
<Camera style={{ flex: 1 }} ref={cameraRef} />
<Button title={isRecording ? "Stop Recording" : "Record Video"} onPress={recordVideo} />
</View>
);
}
```
Access Camera Settings
This feature allows you to access and modify camera settings such as switching between the front and back cameras. The code sample demonstrates how to request camera permissions and toggle the camera type.
```javascript
import React, { useState, useEffect } from 'react';
import { View, Text } from 'react-native';
import { Camera } from 'expo-camera';
export default function App() {
const [hasPermission, setHasPermission] = useState(null);
const [cameraType, setCameraType] = useState(Camera.Constants.Type.back);
useEffect(() => {
(async () => {
const { status } = await Camera.requestPermissionsAsync();
setHasPermission(status === 'granted');
})();
}, []);
if (hasPermission === null) {
return <View />;
}
if (hasPermission === false) {
return <Text>No access to camera</Text>;
}
return (
<View style={{ flex: 1 }}>
<Camera style={{ flex: 1 }} type={cameraType} />
<Button
title="Flip Camera"
onPress={() => {
setCameraType(
cameraType === Camera.Constants.Type.back
? Camera.Constants.Type.front
: Camera.Constants.Type.back
);
}}
/>
</View>
);
}
```
Other packages similar to expo-camera
react-native-camera
The react-native-camera package is a popular alternative to expo-camera. It provides similar functionalities such as capturing photos, recording videos, and accessing camera settings. However, it requires linking and additional setup compared to the more streamlined experience of expo-camera within the Expo ecosystem.
react-native-vision-camera
The react-native-vision-camera package offers advanced camera functionalities, including frame processing and integration with machine learning models. It is more feature-rich compared to expo-camera but also requires more complex setup and configuration.
A React component that renders a preview for the device's either front or back camera. Camera's parameters like zoom, auto focus, white balance and flash mode are adjustable. With expo-camera, one can also take photos and record videos that are saved to the app's cache. Morever, the component is also capable of detecting faces and bar codes appearing on the preview.
API documentation
Installation in managed Expo projects
For managed Expo projects, please follow the installation instructions in the API documentation for the latest stable release.
Installation in bare React Native projects
For bare React Native projects, you must ensure that you have installed and configured the expo
package before continuing.
Add the package to your npm dependencies
npx expo install expo-camera
Configure for Android
This package automatically adds the CAMERA
permission to your app. If you want to record videos with audio, you have to include the RECORD_AUDIO
.
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
Adjust the android/build.gradle
to add a new maven
block after all other repositories as described below:
allprojects {
repositories {
// * Your other repositories here *
// * Add a new maven block after other repositories / blocks *
maven {
// expo-camera bundles a custom com.google.android:cameraview
url "$rootDir/../node_modules/expo-camera/android/maven"
}
}
}
The sourcecode for cameraview
can be found at expo/cameraview
.
Configure for iOS
Add NSCameraUsageDescription
and NSMicrophoneUsageDescription
keys to your Info.plist
:
<key>NSCameraUsageDescription</key>
<string>Allow $(PRODUCT_NAME) to use the camera</string>
<key>NSMicrophoneUsageDescription</key>
<string>Allow $(PRODUCT_NAME) to use the microphone</string>
Run npx pod-install
after installing the npm package.
Contributing
Contributions are very welcome! Please refer to guidelines described in the contributing guide.