2021-03-03 12:37:43 +01:00
---
id: setup
title: Getting Started
sidebar_label: Getting Started
2021-03-23 15:25:27 +01:00
slug: /guides
2021-03-03 12:37:43 +01:00
---
2021-02-24 22:19:28 +01:00
2023-09-26 11:39:17 +02:00
import Tabs from '@theme/Tabs'
import TabItem from '@theme/TabItem'
import useBaseUrl from '@docusaurus/useBaseUrl'
2021-03-04 17:02:14 +01:00
2023-09-26 13:09:44 +02:00
<div class="image-container">
<img width="283" src={useBaseUrl("img/example_intro.png")} />
2021-02-24 22:19:28 +01:00
</div>
## Installing the library
2023-09-26 14:42:22 +02:00
Install [react-native-vision-camera](https://www.npmjs.com/package/react-native-vision-camera) through npm:
2021-02-24 22:19:28 +01:00
2021-07-07 16:13:15 +02:00
<Tabs
groupId="environment"
defaultValue="rn"
values={[
{label: 'React Native', value: 'rn'},
{label: 'Expo', value: 'expo'}
]}>
<TabItem value="rn">
2021-05-06 22:39:13 +02:00
```bash
2021-02-24 22:19:28 +01:00
npm i react-native-vision-camera
npx pod-install
```
2021-07-07 16:13:15 +02:00
</TabItem>
<TabItem value="expo">
```bash
expo install react-native-vision-camera
```
</TabItem>
</Tabs>
2023-09-01 17:31:10 +02:00
VisionCamera requires **iOS 12 or higher**, and **Android-SDK version 26 or higher**. See [Troubleshooting](/docs/guides/troubleshooting) if you're having installation issues.
2021-05-06 14:11:55 +02:00
2023-09-26 14:42:22 +02:00
> **(Optional)** If you want to use [Frame Processors](/docs/guides/frame-processors), you need to install [react-native-worklets-core](https://github.com/margelo/react-native-worklets-core) 0.2.0 or higher.
2021-05-06 16:21:08 +02:00
2021-02-24 22:19:28 +01:00
## Updating manifests
2023-09-25 12:57:03 +02:00
To use the Camera or Microphone you must first specify that your app requires camera and microphone permissions.
2021-02-24 22:19:28 +01:00
2021-07-07 14:55:25 +02:00
<Tabs
2021-07-07 16:13:15 +02:00
groupId="environment"
defaultValue="rn"
2021-07-07 14:55:25 +02:00
values={[
2021-07-07 16:13:15 +02:00
{label: 'React Native', value: 'rn'},
2021-07-07 14:55:25 +02:00
{label: 'Expo', value: 'expo'}
]}>
2021-07-07 16:13:15 +02:00
<TabItem value="rn">
2021-07-07 14:55:25 +02:00
2021-02-24 22:19:28 +01:00
### iOS
Open your project's `Info.plist` and add the following lines inside the outermost `<dict>` tag:
```xml
<key>NSCameraUsageDescription</key>
2021-07-07 16:13:15 +02:00
<string>$(PRODUCT_NAME) needs access to your Camera.</string>
2021-07-07 14:55:25 +02:00
<!-- optionally, if you want to record audio: -->
2021-02-24 22:19:28 +01:00
<key>NSMicrophoneUsageDescription</key>
2021-07-07 16:13:15 +02:00
<string>$(PRODUCT_NAME) needs access to your Microphone.</string>
2021-02-24 22:19:28 +01:00
```
### Android
Open your project's `AndroidManifest.xml` and add the following lines inside the `<manifest>` tag:
```xml
<uses-permission android:name="android.permission.CAMERA" />
2021-07-07 16:13:15 +02:00
2021-07-07 14:55:25 +02:00
<!-- optionally, if you want to record audio: -->
2021-02-24 22:19:28 +01:00
<uses-permission android:name="android.permission.RECORD_AUDIO" />
```
2021-07-07 14:55:25 +02:00
</TabItem>
<TabItem value="expo">
2021-07-07 16:06:01 +02:00
### Managed Expo
2021-07-07 14:55:25 +02:00
Add the VisionCamera plugin to your Expo config (`app.json`, `app.config.json` or `app.config.js`):
2021-07-26 15:32:43 +02:00
```json
2021-07-07 14:55:25 +02:00
{
"name": "my app",
"plugins": [
[
"react-native-vision-camera",
{
2021-07-07 16:13:15 +02:00
"cameraPermissionText": "$(PRODUCT_NAME) needs access to your Camera.",
2021-07-07 14:55:25 +02:00
// optionally, if you want to record audio:
"enableMicrophonePermission": true,
2021-07-07 16:13:15 +02:00
"microphonePermissionText": "$(PRODUCT_NAME) needs access to your Microphone."
2021-07-07 14:55:25 +02:00
}
]
]
}
```
2021-07-07 16:13:15 +02:00
Finally, compile the mods:
2021-07-07 16:06:01 +02:00
```bash
expo prebuild
```
To apply the changes, build a new binary with EAS:
```bash
eas build
```
2021-07-07 14:55:25 +02:00
</TabItem>
</Tabs>
2021-05-06 14:11:55 +02:00
## Getting/Requesting Permissions
2021-02-24 22:19:28 +01:00
2023-09-25 12:57:03 +02:00
Next, ask the user for Camera or Microphone permissions using the Permissions API:
<Tabs
groupId="component-style"
defaultValue="hooks"
values={[
{label: 'Hooks API', value: 'hooks'},
{label: 'Imperative API', value: 'imperative'}
]}>
<TabItem value="hooks">
Simply use the `useCameraPermission` or `useMicrophonePermission` hook:
```ts
const { hasPermission, requestPermission } = useCameraPermission()
```
And if you want to use the microphone:
```ts
const { hasPermission, requestPermission } = useMicrophonePermission()
```
There could be three states to this:
1. First time opening the app, `hasPermission` is false. Call `requestPermission()` now.
2023-09-26 14:42:22 +02:00
2. User already granted permission, `hasPermission` is true. Continue with [using the `<Camera>` view](#use-the-camera-view).
2023-09-25 12:57:03 +02:00
3. User explicitly denied permission, `hasPermission` is false and `requestPermission()` will return false. Tell the user that he needs to grant Camera Permission, potentially by using the [`Linking` API](https://reactnative.dev/docs/linking#opensettings) to open the App Settings.
</TabItem>
<TabItem value="imperative">
2021-02-24 22:19:28 +01:00
### Getting Permissions
Simply use the **get** functions to find out if a user has granted or denied permission before:
```ts
const cameraPermission = await Camera.getCameraPermissionStatus()
const microphonePermission = await Camera.getMicrophonePermissionStatus()
```
A permission status can have the following values:
2023-09-26 14:42:22 +02:00
* `granted`: Your app is authorized to use said permission. Continue with [using the `<Camera>` view](#use-the-camera-view).
2021-02-24 22:19:28 +01:00
* `not-determined`: Your app has not yet requested permission from the user. [Continue by calling the **request** functions.](#requesting-permissions)
* `denied`: Your app has already requested permissions from the user, but was explicitly denied. You cannot use the **request** functions again, but you can use the [`Linking` API](https://reactnative.dev/docs/linking#opensettings) to redirect the user to the Settings App where he can manually grant the permission.
2023-09-25 12:57:03 +02:00
* `restricted`: Your app cannot use the Camera or Microphone because that functionality has been restricted, possibly due to active restrictions such as parental controls being in place.
2021-02-24 22:19:28 +01:00
### Requesting Permissions
Use the **request** functions to prompt the user to give your app permission to use the Camera or Microphone.
2021-05-06 14:11:55 +02:00
:::note
The **request** functions only have effect if the current permission status is `not-determined`.
2021-03-03 12:37:43 +01:00
:::
2021-02-24 22:19:28 +01:00
```ts
const newCameraPermission = await Camera.requestCameraPermission()
const newMicrophonePermission = await Camera.requestMicrophonePermission()
```
The permission request status can have the following values:
2023-09-26 14:42:22 +02:00
* `granted`: Your app is authorized to use said permission. Continue with [using the `<Camera>` view](#use-the-camera-view).
2021-03-24 12:42:11 +01:00
* `denied`: The user explicitly denied the permission request alert. You cannot use the **request** functions again, but you can use the [`Linking` API](https://reactnative.dev/docs/linking#opensettings) to redirect the user to the Settings App where he can manually grant the permission.
2023-09-25 12:57:03 +02:00
* `restricted`: Your app cannot use the Camera or Microphone because that functionality has been restricted, possibly due to active restrictions such as parental controls being in place.
</TabItem>
</Tabs>
2021-05-06 14:11:55 +02:00
## Use the `<Camera>` view
2023-09-25 12:57:03 +02:00
If your app has permission to use the Camera and Microphone, simply use the [`useCameraDevice(...)`](/docs/api#usecameradevice) hook to get a Camera device (see [Camera Devices](/docs/guides/devices)) and mount the `<Camera>` view:
2021-05-06 14:11:55 +02:00
```tsx
function App() {
2023-09-25 12:57:03 +02:00
const device = useCameraDevice('back')
2021-05-06 14:11:55 +02:00
2023-09-25 12:57:03 +02:00
if (device == null) return <NoCameraDeviceError />
2021-05-06 14:11:55 +02:00
return (
<Camera
style={StyleSheet.absoluteFill}
device={device}
isActive={true}
/>
)
}
```
2021-02-24 22:19:28 +01:00
<br />
2021-03-23 15:49:58 +01:00
#### 🎉 Hooray! You're ready to learn about [Camera Devices](/docs/guides/devices)!