This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

Expo 相机 iconExpo 相机

一个 React 组件,用于渲染设备前置或后置摄像头的预览。

Android (device only)
iOS (device only)
Web
Included in Expo Go
Recommended version:
~58.0.9

expo-camera 提供了一个 React 组件,用于渲染设备前置或后置摄像头的预览。摄像头的参数,如缩放、手电筒和闪光模式,都可以调整。使用 CameraView,你可以拍照和录制视频,这些内容将保存到应用的缓存中。该组件还能够检测预览中出现的条形码。在你的设备上运行 示例,即可看到所有这些功能的协同效果。

安装

🌐 Installation

Terminal
- npx expo install expo-camera
- yarn expo install expo-camera
- pnpm expo install expo-camera
- bun expo install expo-camera

If you are installing this in an existing React Native app, make sure to install expo in your project.

应用配置中的配置

🌐 Configuration in app config

如果你在项目中使用配置插件(连续原生生成 (CNG)),你可以使用其内置的 配置插件 来配置 expo-camera。该插件允许你配置一些无法在运行时设置的属性,这些属性需要构建新的应用二进制文件才能生效。如果你的应用不使用 CNG,那么你需要手动配置该库。

🌐 You can configure expo-camera using its built-in config plugin if you use config plugins in your project (Continuous Native Generation (CNG)). The plugin allows you to configure various properties that cannot be set at runtime and require building a new app binary to take effect. If your app does not use CNG, then you'll need to manually configure the library.

Example app.json with config plugin

app.json
{ "expo": { "plugins": [ [ "expo-camera", { "cameraPermission": "Allow $(PRODUCT_NAME) to access your camera", "microphonePermission": "Allow $(PRODUCT_NAME) to access your microphone", "recordAudioAndroid": true, "barcodeScannerEnabled": true } ] ] } }

Configurable properties

NameDefaultDescription
cameraPermission"Allow $(PRODUCT_NAME) to access your camera"
Only for: 
iOS

A string to set the NSCameraUsageDescription permission message.

microphonePermission"Allow $(PRODUCT_NAME) to access your microphone"
Only for: 
iOS

A string to set the NSMicrophoneUsageDescription permission message.

recordAudioAndroidtrue
Only for: 
Android

A boolean that determines whether to enable the RECORD_AUDIO permission on Android.

barcodeScannerEnabledtrue

A boolean that determines whether to enable barcode scanning support. Disabling this reduces app size when barcode scanning is not needed. On Android, this option only takes effect when expo-camera builds from source. Prebuilt modules already include the barcode scanning libraries. To make this option apply, add expo-camera to buildFromSource in package.json.

要本地化 iOS 相机或麦克风权限消息,请在每个 语言文件 的 ios 对象中添加 NSCameraUsageDescription 或 NSMicrophoneUsageDescription。保持 cameraPermission 和 microphonePermission 为默认值。Expo 会在预构建期间将本地化的值写入 InfoPlist.strings。

🌐 To localize the iOS camera or microphone permission message, add NSCameraUsageDescription or NSMicrophoneUsageDescription to the ios object in each locale file. Keep cameraPermission and microphonePermission as the default values. Expo writes the localized values to InfoPlist.strings during prebuild.

Are you using this library in an existing React Native app?

如果你没有使用连续原生生成(CNG)(而是手动使用原生 Android 和 iOS 项目),那么你需要在原生项目中配置以下权限:

🌐 If you're not using Continuous Native Generation (CNG) (you're using native android and ios projects manually), then you need to configure following permissions in your native projects:

安卓

  • expo-camera 会自动将 android.permission.CAMERA 权限添加到你项目的 android/app/src/main/AndroidManifest.xml 中。如果你想录制带音频的视频,请添加 RECORD_AUDIO 权限:

    <!-- Added permission --> <uses-permission android:name="android.permission.CAMERA" /> <!-- Only add when recording videos with audio --> <uses-permission android:name="android.permission.RECORD_AUDIO" />
  • 然后,调整 android/build.gradle 文件,在所有其他仓库之后添加新的 maven 块,如下所示:

    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" } } }

iOS

  • 将 NSCameraUsageDescription 和 NSMicrophoneUsageDescription 键添加到你项目的 ios/[app]/Info.plist 文件中:

    <key>NSCameraUsageDescription</key> <string>Allow $(PRODUCT_NAME) to access your camera</string> <key>NSMicrophoneUsageDescription</key> <string>Allow $(PRODUCT_NAME) to access your microphone</string>

用法

🌐 Usage

Basic Camera usage
import { CameraView, CameraType, useCameraPermissions } from 'expo-camera'; import { useState } from 'react'; import { Button, StyleSheet, Text, TouchableOpacity, View } from 'react-native'; export default function App() { const [facing, setFacing] = useState<CameraType>('back'); const [permission, requestPermission] = useCameraPermissions(); if (!permission) { // Camera permissions are still loading. return <View />; } if (!permission.granted) { // Camera permissions are not granted yet. return ( <View style={styles.container}> <Text style={styles.message}>We need your permission to show the camera</Text> <Button onPress={requestPermission} title="授予许可" /> </View> ); } function toggleCameraFacing() { setFacing(current => (current === 'back' ? 'front' : 'back')); } return ( <View style={styles.container}> <CameraView style={styles.camera} facing={facing} /> <View style={styles.buttonContainer}> <TouchableOpacity style={styles.button} onPress={toggleCameraFacing}> <Text style={styles.text}>Flip Camera</Text> </TouchableOpacity> </View> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', }, message: { textAlign: 'center', paddingBottom: 10, }, camera: { flex: 1, }, buttonContainer: { position: 'absolute', bottom: 64, flexDirection: 'row', backgroundColor: 'transparent', width: '100%', paddingHorizontal: 64, }, button: { flex: 1, alignItems: 'center', }, text: { fontSize: 24, fontWeight: 'bold', color: 'white', }, });

高级用法

🌐 Advanced usage

相机应用示例

一个完整的示例,展示如何拍照并显示。使用 TypeScript 编写。

Web 支持

🌐 Web support

大多数浏览器都支持某种版本的网络摄像头功能,你可以在这里查看 网络摄像头浏览器支持情况。图片 URI 总是以 base64 字符串的形式返回,因为浏览器中无法访问本地文件系统路径。

🌐 Most browsers support a version of web camera functionality, you can check out the web camera browser support here. Image URIs are always returned as base64 strings because local file system paths are unavailable in the browser.

Chrome iframe 使用情况

🌐 Chrome iframe usage

使用 Chrome 64 及以上版本 时,如果你尝试在跨源 iframe 中使用网络摄像头,将无法显示任何内容。要在你的 iframe 中添加对摄像头的支持,只需在 iframe 元素中添加属性 allow="microphone; camera;":

🌐 When using Chrome versions 64+, if you try to use a web camera in a cross-origin iframe nothing will render. To add support for cameras in your iframe simply add the attribute allow="microphone; camera;" to the iframe element:

<iframe src="..." allow="microphone; camera;"> <!-- <CameraView /> --> </iframe>

应用接口

🌐 API

import { CameraView } from 'expo-camera';

No API data file found, sorry!

权限

🌐 Permissions

安卓

🌐 Android

此包会自动将 CAMERA 权限添加到你的应用中。如果你想录制带音频的视频,必须在 app.json 中的 expo.android.permissions 数组里包含 RECORD_AUDIO。

🌐 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 in your app.json inside the expo.android.permissions array.

Android permissionDescription

CAMERA

Required to be able to access the camera device.

RECORD_AUDIO

Allows an application to record audio.

iOS

该库使用以下用法描述键:

🌐 The following usage description keys are used by this library:

Info.plist keyDescription

NSCameraUsageDescription

A message that tells the user why the app is requesting access to the device’s camera.

NSMicrophoneUsageDescription

A message that tells the user why the app is requesting access to the device’s microphone.