14.1.3 • Published 2 days ago

expo-camera v14.1.3

Weekly downloads
26,576
License
MIT
Repository
github
Last release
2 days ago

expo-camera

Installation

iOS (Cocoapods)

If you're using Cocoapods, add the dependency to your Podfile:

pod 'EXCamera'

and run pod install.

iOS (no Cocoapods)

  1. In XCode, in the project navigator, right click LibrariesAdd Files to [your project's name]
  2. Go to node_modulesexpo-camera and add EXCamera.xcodeproj
  3. In XCode, in the project navigator, select your project. Add libEXCamera.a to your project's Build PhasesLink Binary With Libraries
  4. Run your project (Cmd+R).

Android

  1. Append the following lines to android/settings.gradle:
    include ':expo-camera'
    project(':expo-camera').projectDir = new File(rootProject.projectDir, '../node_modules/expo-camera/android')
    and if not already included
    include ':expo-permissions-interface'
    project(':expo-permissions-interface').projectDir = new File(rootProject.projectDir, '../node_modules/expo-permissions-interface/android')
  2. Insert the following lines inside the dependencies block in android/app/build.gradle:
    compile project(':expo-camera')
    and if not already included
    compile project(':expo-permissions-interface')

Introduction

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 use of 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.

Note: Only one Camera preview is supported by this library right now. When using navigation, the best practice is to unmount previously rendered Camera component so next screens can use camera without issues.

Note: Android devices can use one of two available Camera apis underneath. This was previously chosen automatically, based on the device's Android system version and camera hardware capabilities. As we experienced some issues with Android's Camera2 API, we decided to choose the older API as a default. However, using the newer one is still possible through setting useCamera2Api prop to true. The change we made should be barely visible - the only thing that is not supported using the old Android's API is setting focus depth.

Requires Permissions.CAMERA. Video recording requires Permissions.AUDIO_RECORDING. (See expo-permissions package).

Basic Example

import React from 'react';
import { Text, View, TouchableOpacity } from 'react-native';
import { Permissions } from 'expo-permissions';
import { Camera } from 'expo-camera';

export default class CameraExample extends React.Component {
  state = {
    hasCameraPermission: null,
    type: Camera.Constants.Type.back,
  };

  async componentWillMount() {
    const { status } = await Permissions.askAsync(Permissions.CAMERA);
    this.setState({ hasCameraPermission: status === 'granted' });
  }

  render() {
    const { hasCameraPermission } = this.state;
    if (hasCameraPermission === null) {
      return <View />;
    } else if (hasCameraPermission === false) {
      return <Text>No access to camera</Text>;
    } else {
      return (
        <View style={{ flex: 1 }}>
          <Camera style={{ flex: 1 }} type={this.state.type}>
            <View
              style={{
                flex: 1,
                backgroundColor: 'transparent',
                flexDirection: 'row',
              }}>
              <TouchableOpacity
                style={{
                  flex: 0.1,
                  alignSelf: 'flex-end',
                  alignItems: 'center',
                }}
                onPress={() => {
                  this.setState({
                    type:
                      this.state.type === Camera.Constants.Type.back
                        ? Camera.Constants.Type.front
                        : Camera.Constants.Type.back,
                  });
                }}>
                <Text style={{ fontSize: 18, marginBottom: 10, color: 'white' }}> Flip </Text>
              </TouchableOpacity>
            </View>
          </Camera>
        </View>
      );
    }
  }
}

props

  • type

Camera facing. Use one of Camera.Constants.Type. When Type.front, use the front-facing camera. When Type.back, use the back-facing camera. Default: Type.back.

  • flashMode

Camera flash mode. Use one of Camera.Constants.FlashMode. When on, the flash on your device will turn on when taking a picture, when off, it won't. Setting to auto will fire flash if required, torch turns on flash during the preview. Default: off.

  • autoFocus

State of camera auto focus. Use one of Camera.Constants.AutoFocus. When on, auto focus will be enabled, when off, it wont't and focus will lock as it was in the moment of change but it can be adjusted on some devices via focusDepth prop.

  • zoom (float)

A value between 0 and 1 being a percentage of device's max zoom. 0 - not zoomed, 1 - maximum zoom. Default: 0.

  • whiteBalance

Camera white balance. Use one of Camera.Constants.WhiteBalance: auto, sunny, cloudy, shadow, fluorescent, incandescent. If a device does not support any of these values previous one is used.

  • focusDepth (float)

Distance to plane of sharpest focus. A value between 0 and 1: 0 - infinity focus, 1 - focus as close as possible. Default: 0. For Android this is available only for some devices and when useCamera2Api is set to true.

  • ratio (string)

Android only. A string representing aspect ratio of the preview, eg. 4:3, 16:9, 1:1. To check if a ratio is supported by the device use getSupportedRatiosAsync. Default: 4:3.

  • onCameraReady (function)

Callback invoked when camera preview has been set.

  • onFacesDetected (function)

Callback invoked with results of face detection on the preview. It will receive an object containing:

  • faces (array) - array of faces objects:
    • faceID (number) -- a face identifier (used for tracking, if the same face appears on consecutive frames it will have the same faceID).
    • bounds (object) -- an object containing:
      • origin ({ x: number, y: number }) -- position of the top left corner of a square containing the face in view coordinates,
      • size ({ width: number, height: number }) -- size of the square containing the face in view coordinates,
    • rollAngle (number) -- roll angle of the face (bank),
    • yawAngle (number) -- yaw angle of the face (heading, turning head left or right),
    • smilingProbability (number) -- probability that the face is smiling,
    • leftEarPosition ({ x: number, y: number}) -- position of the left ear in view coordinates,
    • rightEarPosition ({ x: number, y: number}) -- position of the right ear in view coordinates,
    • leftEyePosition ({ x: number, y: number}) -- position of the left eye in view coordinates,
    • leftEyeOpenProbability (number) -- probability that the left eye is open,
    • rightEyePosition ({ x: number, y: number}) -- position of the right eye in view coordinates,
    • rightEyeOpenProbability (number) -- probability that the right eye is open,
    • leftCheekPosition ({ x: number, y: number}) -- position of the left cheek in view coordinates,
    • rightCheekPosition ({ x: number, y: number}) -- position of the right cheek in view coordinates,
    • mouthPosition ({ x: number, y: number}) -- position of the center of the mouth in view coordinates,
    • leftMouthPosition ({ x: number, y: number}) -- position of the left edge of the mouth in view coordinates,
    • rightMouthPosition ({ x: number, y: number}) -- position of the right edge of the mouth in view coordinates,
    • noseBasePosition ({ x: number, y: number}) -- position of the nose base in view coordinates.

smilingProbability, leftEyeOpenProbability and rightEyeOpenProbability are returned only if faceDetectionClassifications property is set to .all.

Positions of face landmarks are returned only if faceDetectionLandmarks property is set to .all.

See also FaceDetector component.

  • faceDetectionMode (Camera.Constants.FaceDetection.Mode)

Mode of the face detection. Use one of Camera.Constants.FaceDetection.Mode.{fast, accurate}.

  • faceDetectionLandmarks (Camera.Constants.FaceDetection.Landmarks)

Whether to detect landmarks on the faces. Use one of Camera.Constants.FaceDetection.Landmarks.{all, none}. See FaceDetector documentation for details.

  • faceDetectionClassifications (Camera.Constants.FaceDetection.Classifications)

Whether to run additional classifications on the faces. Use one of Camera.Constants.FaceDetection.Classifications.{all, none}. See FaceDetector documentation for details.

  • onMountError (function)

Callback invoked when camera preview could not been started. It is provided with an error object that contains a message.

  • onBarCodeRead (function)

Callback that is invoked when a bar code has been successfully read. The callback is provided with an Object of the shape { type: string, data: string }, where the type refers to the bar code type that was scanned and the data is the information encoded in the bar code (in this case of QR codes, this is often a URL)

  • barCodeTypes (Array)

An array of bar code types. Usage: Camera.Constants.BarCodeType.<codeType> where codeType is one of the listed above. Default: all supported bar code types. For example: barCodeTypes={[Camera.Constants.BarCodeType.qr]}

  • useCamera2Api (boolean)

Android only. Whether to use Android's Camera2 API. See Note at the top of this page.

Methods

To use methods that Camera exposes one has to create a components ref and invoke them using it.

// ...
<Camera
  ref={ref => {
    this.camera = ref;
  }}
/>;
// ...
snap = async () => {
  if (this.camera) {
    let photo = await this.camera.takePictureAsync();
  }
};

takePictureAsync

Takes a picture and saves it to app's cache directory. Photos are rotated to match device's orientation and scaled to match the preview. Anyway on Android it is essential to set ratio prop to get a picture with correct dimensions.

Arguments

  • options (object) --

    A map of options:

    • quality (number) -- Specify the quality of compression, from 0 to 1. 0 means compress for small size, 1 means compress for maximum quality.
    • base64 (boolean) -- Whether to also include the image data in Base64 format.
    • exif (boolean) -- Whether to also include the EXIF data for the image.

Returns

Returns a Promise that resolves to an object: { uri, width, height, exif, base64 } where uri is a URI to the local image file (useable as the source for an Image element) and width, height specify the dimensions of the image. base64 is included if the base64 option was truthy, and is a string containing the JPEG data of the image in Base64--prepend that with 'data:image/jpg;base64,' to get a data URI, which you can use as the source for an Image element for example. exif is included if the exif option was truthy, and is an object containing EXIF data for the image--the names of its properties are EXIF tags and their values are the values for those tags.

The local image URI is temporary. Use Expo.FileSystem.copyAsync to make a permanent copy of the image.

recordAsync

Starts recording a video that will be saved to cache directory. Videos are rotated to match device's orientation. Flipping camera during a recording results in stopping it.

Arguments

  • options (object) --

    A map of options:

    • quality (VideoQuality) -- Specify the quality of recorded video. Usage: Camera.Constants.VideoQuality['<value>'], possible values: for 16:9 resolution 2160p, 1080p, 720p, 480p (Android only) and for 4:3 4:3 (the size is 640x480). If the chosen quality is not available for a device, the highest available is chosen.
    • maxDuration (number) -- Maximum video duration in seconds.
    • maxFileSize (number) -- Maximum video file size in bytes.
    • mute (boolean) -- If present, video will be recorded with no sound.

Returns

Returns a Promise that resolves to an object containing video file uri property. The Promise is returned if stopRecording was invoked, one of maxDuration and maxFileSize is reached or camera preview is stopped.

stopRecording

Stops recording if any is in progress.

getSupportedRatiosAsync

Android only. Get aspect ratios that are supported by the device and can be passed via ratio prop.

Returns

Returns a Promise that resolves to an array of strings representing ratios, eg. ['4:3', '1:1'].

Supported bar code formats

Bar code formatiOSAndroid
aztecYesYes
codabarNoYes
code39YesYes
code93YesYes
code128YesYes
code138YesNo
code39mod43YesNo
datamatrixYesYes
ean13YesYes
ean8YesYes
interleaved2of5YesNo
itf14Yes*Yes
maxicodeNoYes
pdf417YesYes
rss14NoYes
rssexpandedNoYes
upc_aNoYes
upc_eYesYes
upc_eanNoYes
qrYesYes
  • sometimes when an ITF-14 barcode is recognized it's type is set to interleaved2of5.
native-cameraassociacao-digital-completeconsultadigitaltenor-gif-boothreact-native-holper-storybookraodaor-appraodaor-app-poetryraodaor-app1@expocraft/core@everything-registry/sub-chunk-1630enntte_app_almacen_garnicazippi-market-corezeninvesttrv-expounitx-ui@harrysong/taro-rn@harrysong/components-rnfindme-formskmandinirnshellscannersdksyrow-sdksales-sync-mobilesouge-shop-packagespfytensor-flow-comptest-package-everneticastickersmashtxradicalfaeriesgathering2022@kafudev/react-native-core@kafudev/react-native-shellexpopassportexposcannersdkexposdkassentifyexpo-media-pickerexpo-redux-template@holper/react-native-holper-storybook@hubspire/expo-app@infinitebrahmanuniverse/nolb-expoganeyipgo-saffe-react-nativefuckfuck@mifind/taro-rn@mobile-public-skeleton/mobile-componentsexpo-inputsexpo-componentsexpo-torq@kfox/expo@rtarojs/components-rn@rtarojs/taro-rn@portkey/react-native-sdkeasy-expoenntte_app_almacen_arestantenntte_app_almacen_ederlantafallaenntte_app_almacen_sabaterenntte_app_almacen_symagaenntte_app_almacen_aratubo@peersyst/react-native-components@puzzlpayroll/employee-onboarding@paintbox/native@secondcloset/mobile-components@ezri00/trackappjoseph-onboarding@tarojs/taro-rn@blackyu123/expoui@snapbot-app/examples@snapbot-app/time-rulerluxe-app-frameworkcenteridentity-expomuba-popup-permissionsassentifydemomairiesimplonakasbeauty-istanbul@tarojs/components-rnmake-photomijonappa2@vnxjs/vnmf-rn@vnxjs/components-rn@agreejs/taro-rn@andreciornavei/rn-essentialsaphricapassportsdkpassportexpopcmli.umbrella.react-nativepaintboxpocketxtra-ui-librarypocketxtra-ui-library-px@draftbit/nativepolseposetrackercom.loggi.teste.toolboxconsulta-digitalpuzzl-w2-onboardingdigitrecognizerreact-native-expo-redux-templatereact-native-json-formsreact-native-sdk-deltareact-native-sdk-metareact-native-sdk-portkey
15.0.2

2 days ago

15.0.1

4 days ago

15.0.0

8 days ago

14.1.3

10 days ago

14.1.2

18 days ago

14.1.0

1 month ago

14.1.1

1 month ago

14.0.6

2 months ago

14.0.5

2 months ago

14.0.4

3 months ago

14.0.3

3 months ago

14.0.2

3 months ago

14.0.1

4 months ago

14.0.0

5 months ago

13.9.0

5 months ago

13.8.0

6 months ago

13.7.0

7 months ago

13.4.4

8 months ago

13.6.0

8 months ago

13.4.3

8 months ago

13.5.1

9 months ago

13.5.0

9 months ago

13.4.2

10 months ago

13.4.1

10 months ago

13.4.0

11 months ago

13.3.0

12 months ago

13.2.0

1 year ago

13.2.1

1 year ago

12.5.0

1 year ago

13.0.0-beta.1

2 years ago

13.1.0

1 year ago

12.4.0

1 year ago

13.0.0

2 years ago

12.3.0

2 years ago

12.2.0

2 years ago

12.1.2

2 years ago

12.1.0

2 years ago

12.1.1

2 years ago

12.0.3

3 years ago

12.0.2

3 years ago

12.0.1

3 years ago

12.0.0

3 years ago

11.2.2

3 years ago

11.3.1

3 years ago

11.3.0

3 years ago

11.2.1

3 years ago

11.2.0

3 years ago

11.1.1

3 years ago

11.1.0

3 years ago

11.0.3

3 years ago

11.0.2

3 years ago

11.0.1

3 years ago

11.0.0

3 years ago

10.0.0

3 years ago

9.1.1

3 years ago

9.1.0

3 years ago

9.0.0

4 years ago

8.3.1

4 years ago

8.3.0

4 years ago

8.2.0

4 years ago

8.1.0

4 years ago

7.0.0-lifttv.2

4 years ago

8.0.0

4 years ago

7.0.0

5 years ago

7.0.0-rc.0

5 years ago

6.0.0

5 years ago

6.0.0-rc.0

5 years ago

5.0.1

5 years ago

5.0.0

5 years ago

5.0.0-rc.0

5 years ago

4.0.0

5 years ago

3.0.0

5 years ago

2.0.0

5 years ago

2.0.0-rc.3

5 years ago

2.0.0-rc.2

5 years ago

2.0.0-rc.1

5 years ago

2.0.0-rc.0

5 years ago

1.0.2

5 years ago

1.2.0

5 years ago

1.2.0-rc.1

5 years ago

1.2.0-rc.0

6 years ago

1.1.1

6 years ago

1.1.1-rc.0

6 years ago

1.1.0

6 years ago

1.1.0-rc.1

6 years ago

1.1.0-rc.0

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

1.0.0-rc.2

6 years ago

1.0.0-rc.1

6 years ago

1.0.0-rc.0

6 years ago

0.0.1

6 years ago