feat(camera): remove deprecated RCTCamera, fix #1890 (#2215)

This commit is contained in:
Sibelius Seraphini 2019-04-17 22:54:48 -03:00 committed by GitHub
parent 0a0430aab3
commit c76a963026
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 11 additions and 863 deletions

View File

@ -1,42 +1,43 @@
---
name: Bug report
about: Create a report to help us improve
---
# Bug Report
**To Do First**
- [ ] Did you try latest release?
- [ ] Did you try master?
- [ ] Did you look for existing matching issues?
**Related Modules**
<!--Comment in the related ones-->
<!--RNCamera-->
<!--FaceDetector-->
<!--RCTCamera(deprecated)-->
**Platforms**
<!--Comment in the related ones-->
<!--Android-->
<!--iOS-->
**Versions**
<!--Please add the used versions/branches or leave blank and comment in the optionals if used-->
- Android:
- iOS:
- react-native-camera:
- react-native:
- react:
<!---react-navigation:-->
<!---react-navigation:-->
**Description/Current Behaviour**
<!--place your bug description below-->
**Expected Behaviour**
<!--place your expected behaviour below-->
**Steps to Reproduce**
<!--describe how to produce the error below-->
<!--**Does it work with Expo Camera?**-->
@ -44,7 +45,8 @@ about: Create a report to help us improve
You should open an issue there as well, so we can cooperate in a solution.-->
**Additionals**
<!--place screenshots/suggestions and other additional infos below-->
> Love react-native-camera? Please consider supporting our collective: 👉 https://opencollective.com/react-native-camera/donate
> Love react-native-camera? Please consider supporting our collective: 👉 https://opencollective.com/react-native-camera/donate
> Want this issue to be resolved faster? Please consider adding a bounty to it https://issuehunt.io/repos/33218414

View File

@ -130,10 +130,6 @@ See this [doc](./docs/migrationV2.md)
[RNCamera](./docs/RNCamera.md)
### Docs old RCTCamera
[RCTCamera](./docs/RCTCamera.md)
## Getting started
### Requirements
@ -555,11 +551,3 @@ Follow the [Q & A](./docs/QA.md) section if you are having compilation issues.
### RNCamera
Take a look into this [documentation](./docs/RNCamera.md).
### RCTCamera
Since `1.0.0`, RCTCamera is deprecated, but if you want to use it, you can see its [documentation](./docs/RCTCamera.md).
---
Thanks to Brent Vatne (@brentvatne) for the `react-native-video` module which provided me with a great example of how to set up this module.

View File

@ -1,398 +0,0 @@
# RCTCamera
As of 1.0.0 release, RCTCamera is deprecated. Please use RNCamera for the latest fixes and improvements.
## Usage
All you need is to `import` `Camera` from the `react-native-camera` module and then use the
`<Camera/>` tag.
```javascript
'use strict';
import React, { Component } from 'react';
import { AppRegistry, Dimensions, StyleSheet, Text, TouchableHighlight, View } from 'react-native';
import Camera from 'react-native-camera';
class BadInstagramCloneApp extends Component {
_requestPermissions = async () => {
if (Platform.OS === 'android') {
const result = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.CAMERA)
return result === PermissionsAndroid.RESULTS.GRANTED || result === true
}
return true
}
_takePicture = async () => {
if (this.camera) {
const options = { quality: 0.5, base64: true }
const data = await this.camera.takePictureAsync(options)
console.log(data.uri)
}
}
_onBarCodeRead = (e) => {
console.log(`Barcode Found! Type: ${e.type}\nData: ${e.data}`)
}
componentDidMount = () => {
({ _, status }) => {
if (status !== 'PERMISSION_GRANTED') {
this._requestPermissions()
}
}
}
render() {
return (
<View style={styles.container}>
<Camera
ref={(ref) => {
this.camera = ref;
}}
onBarCodeRead={(e) => this._onBarCodeRead(e)}
style={styles.preview}>
<Text style={styles.capture} onPress={() => this._takePicture()}>[CAPTURE]</Text>
</Camera>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
backgroundColor: 'black'
},
preview: {
flex: 1,
justifyContent: 'flex-end',
alignItems: 'center'
},
capture: {
flex: 0,
backgroundColor: '#fff',
borderRadius: 5,
padding: 15,
paddingHorizontal: 20,
alignSelf: 'center',
margin: 20
}
});
AppRegistry.registerComponent('BadInstagramCloneApp', () => BadInstagramCloneApp);
```
## Properties
#### `aspect`
Values: `Camera.constants.Aspect.fit` or `"fit"`, `Camera.constants.Aspect.fill` or `"fill"` (default), `Camera.constants.Aspect.stretch` or `"stretch"`
The `aspect` property allows you to define how your viewfinder renders the camera's view. For instance, if you have a square viewfinder and you want to fill it entirely, you have two options: `"fill"`, where the aspect ratio of the camera's view is preserved by cropping the view or `"stretch"`, where the aspect ratio is skewed in order to fit the entire image inside the viewfinder. The other option is `"fit"`, which ensures the camera's entire view fits inside your viewfinder without altering the aspect ratio.
#### `cropToPreview`
Values: `true` or `false` (default)
Will crop the captured image to match the content that is displayed in the preview view. Works on both `Android` and `iOS`. Will be ignored if `captureMode` is other then `Camera.constants.CaptureMode.still`.
#### `iOS` `captureAudio`
Values: `true` (Boolean), `false` (default)
_Applies to video capture mode only._ Specifies whether or not audio should be captured with the video.
#### `captureMode`
Values: `Camera.constants.CaptureMode.still` (default), `Camera.constants.CaptureMode.video`
The type of capture that will be performed by the camera - either a still image or video.
#### `captureTarget`
Values: `Camera.constants.CaptureTarget.cameraRoll` (default), `Camera.constants.CaptureTarget.disk`, `Camera.constants.CaptureTarget.temp`, ~~`Camera.constants.CaptureTarget.memory`~~ (deprecated),
This property allows you to specify the target output of the captured image data. The disk output has been shown to improve capture response time, so that is the recommended value. When using the deprecated memory output, the image binary is sent back as a base64-encoded string.
#### `captureQuality`
Values: `Camera.constants.CaptureQuality.high` or `"high"` (default), `Camera.constants.CaptureQuality.medium` or `"medium"`, `Camera.constants.CaptureQuality.low` or `"low"`, `Camera.constants.CaptureQuality.photo` or `"photo"`, `Camera.constants.CaptureQuality["1080p"]` or `"1080p"`, `Camera.constants.CaptureQuality["720p"]` or `"720p"`, `Camera.constants.CaptureQuality["480p"]` or `"480p"`.
This property allows you to specify the quality output of the captured image or video. By default the quality is set to high.
When choosing more-specific quality settings (1080p, 720p, 480p), note that each platform and device supports different valid picture/video sizes, and actual resolution within each of these quality settings might differ. There should not be too much variance (if any) for iOS; 1080p should give 1920x1080, 720p should give 1280x720, and 480p should give 640x480 (note that iOS 480p therefore is NOT the typical 16:9 HD aspect ratio, and the typically-HD camera preview screen may differ greatly in aspect from what you actually record!!). For Android, expect more variance: on most Androids, 1080p _should_ give 1920x1080 and 720p _should_ give 1280x720; however, 480p will at "best" be 853x480 (16:9 HD aspect ratio), but falls back/down to 800x480, 720x480, or "worse", depending on what is closest-but-less-than 853x480 and available on the actual device. If your application requires knowledge of the precise resolution of the output image/video, you might consider manually determine the actual resolution itself after capture has completed (particularly for 480p on Android).
#### `type`
Values: `Camera.constants.Type.front` or `"front"`, `Camera.constants.Type.back` or `"back"` (default)
Use the `type` property to specify which camera to use.
#### `orientation`
Values:
`Camera.constants.Orientation.auto` or `"auto"` (default),
`Camera.constants.Orientation.landscapeLeft` or `"landscapeLeft"`, `Camera.constants.Orientation.landscapeRight` or `"landscapeRight"`, `Camera.constants.Orientation.portrait` or `"portrait"`, `Camera.constants.Orientation.portraitUpsideDown` or `"portraitUpsideDown"`
The `orientation` property allows you to specify the current orientation of the phone to ensure the viewfinder is "the right way up."
#### `Android` `playSoundOnCapture`
Values: `true` (default) or `false`
This property allows you to specify whether a shutter sound is played on capture. It is currently android only, pending [a reasonable mute implementation](http://stackoverflow.com/questions/4401232/avfoundation-how-to-turn-off-the-shutter-sound-when-capturestillimageasynchrono) in iOS.
#### `onBarCodeRead`
Will call the specified method when a barcode is detected in the camera's view.
Event contains `data` (the data in the barcode) and `bounds` (the rectangle which outlines the barcode.)
The following barcode types can be recognised:
- `aztec`
- `code128`
- `code39`
- `code39mod43`
- `code93`
- `ean13` (`iOS` converts `upca` barcodes to `ean13` by adding a leading 0)
- `ean8`
- `pdf417`
- `qr`
- `upce`
- `interleaved2of5` (when available)
- `itf14` (when available)
- `datamatrix` (when available)
The barcode type is provided in the `data` object.
#### `barCodeTypes`
An array of barcode types to search for. Defaults to all types listed above. No effect if `onBarCodeRead` is undefined.
Example: `<Camera barCodeTypes={[Camera.constants.BarCodeType.qr]} />`
#### `flashMode`
Values:
`Camera.constants.FlashMode.on`,
`Camera.constants.FlashMode.off`,
`Camera.constants.FlashMode.auto`
Use the `flashMode` property to specify the camera flash mode.
#### `torchMode`
Values:
`Camera.constants.TorchMode.on`,
`Camera.constants.TorchMode.off`,
`Camera.constants.TorchMode.auto`
Use the `torchMode` property to specify the camera torch mode.
#### `onFocusChanged: Event { nativeEvent: { touchPoint: { x, y } }`
iOS: Called when a touch focus gesture has been made.
By default, `onFocusChanged` is not defined and tap-to-focus is disabled.
Android: This callback is not yet implemented. However, Android will
automatically do tap-to-focus if the device supports auto-focus; there is
currently no way to manage this from javascript.
To get autofocus/tap to focus functionalities working correctly in android
make sure that the proper permissions are set in your `AndroidManifest.xml`:
```java
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
```
#### `iOS` `defaultOnFocusComponent`
Values:
`true` (default)
`false`
If `defaultOnFocusComponent` set to false, default internal implementation of visual feedback for tap-to-focus gesture will be disabled.
#### `iOS` `onZoomChanged: Event { nativeEvent: { velocity, zoomFactor } }`
iOS: Called when focus has changed.
By default, `onZoomChanged` is not defined and pinch-to-zoom is disabled.
Android: This callback is not yet implemented. However, Android will
automatically handle pinch-to-zoom; there is currently no way to manage this
from javascript.
#### `iOS` `keepAwake`
If set to `true`, the device will not sleep while the camera preview is visible. This mimics the behavior of the default camera app, which keeps the device awake while open.
#### `Android` `clearWindowBackground`
Values:
`true`
`false` (default)
If you encounter performance issue while using a window background drawable (typically defined in theme to emulate splashscreen behavior), set this to true to automatically clear window background once camera is started.
#### `Android` `permissionDialogTitle`
Starting on android M individual permissions must be granted for certain services, the camera is one of them, you can use this to change the title of the dialog prompt requesting permissions.
#### `Android` `permissionDialogMessage`
Starting on android M individual permissions must be granted for certain services, the camera is one of them, you can use this to change the content of the dialog prompt requesting permissions.
#### `notAuthorizedView`
By default a `Camera not authorized` message will be displayed when access to the camera has been denied, if set displays the passed react element instead of the default one.
#### `pendingAuthorizationView`
By default a <ActivityIndicator> will be displayed while the component is waiting for the user to grant/deny access to the camera, if set displays the passed react element instead of the default one.
#### `pendingAuthorizationView`
#### `mirrorImage`
If set to `true`, the image returned will be mirrored.
#### `mirrorVideo`
If set to `true`, the video returned will be mirrored.
#### `fixOrientation` (_deprecated_)
If set to `true`, the image returned will be rotated to the _right way up_. WARNING: It uses a significant amount of memory and my cause your application to crash if the device cannot provide enough RAM to perform the rotation.
(_If you find that you need to use this option because your images are incorrectly oriented by default,
could please submit a PR and include the make model of the device. We believe that it's not
required functionality any more and would like to remove it._)
## Component instance methods
You can access component methods by adding a `ref` (ie. `ref="camera"`) prop to your `<Camera>` element, then you can use `this.refs.camera.capture(cb)`, etc. inside your component.
#### `capture([options]): Promise`
Captures data from the camera. What is captured is based on the `captureMode` and `captureTarget` props. `captureMode` tells the camera whether you want a still image or video. `captureTarget` allows you to specify how you want the data to be captured and sent back to you. See `captureTarget` under Properties to see the available values.
Supported options:
- `audio` (See `captureAudio` under Properties)
- `mode` (See `captureMode` under Properties)
- `target` (See `captureTarget` under Properties)
- `metadata` This is metadata to be added to the captured image.
- `metadata.location` This is the object returned from `navigator.geolocation.getCurrentPosition()` (React Native's geolocation polyfill). It will add GPS metadata to the image.
- `rotation` This will rotate the image by the number of degrees specified.
- `jpegQuality` (integer between 1 and 100) This property is used to compress the output jpeg file with 100% meaning no jpeg compression will be applied.
- `totalSeconds` This will limit video length by number of seconds specified. Only works in video capture mode.
The promise will be fulfilled with an object with some of the following properties:
- `data`: Returns a base64-encoded string with the capture data (only returned with the deprecated `Camera.constants.CaptureTarget.memory`)
- `path`: Returns the path of the captured image or video file on disk
- `width`: (not yet implemented for Android video) returns the image or video file's frame width (taking image orientation into account)
- `height`: (not yet implemented for Android video) returns the image or video file's frame height (taking image orientation into account)
- `duration`: (currently iOS video only) video file duration
- `size`: (currently iOS video only) video file size (in bytes)
#### `iOS` `getFOV(): Promise`
Returns the camera's current field of view.
#### `hasFlash(): Promise`
Returns whether or not the camera has flash capabilities.
#### `stopCapture()`
Ends the current capture session for video captures. Only applies when the current `captureMode` is `video`.
#### `stopPreview()`
Stops the camera preview from running, and natively will make the current capture session pause.
#### `startPreview()`
Starts the camera preview again if previously stopped.
## Component static methods
#### `iOS` `Camera.checkDeviceAuthorizationStatus(): Promise`
Exposes the native API for checking if the device has authorized access to the camera (camera and microphone permissions). Can be used to call before loading the Camera component to ensure proper UX. The promise will be fulfilled with `true` or `false` depending on whether the device is authorized. Note, [as of iOS 10](https://developer.apple.com/library/content/documentation/AudioVideo/Conceptual/PhotoCaptureGuide/#//apple_ref/doc/uid/TP40017511-CH1-DontLinkElementID_3), you will need to add `NSCameraUsageDescription` and `NSMicrophoneUsageDescription` to your XCode project's Info.plist file or you might experience a crash.
#### `iOS` `Camera.checkVideoAuthorizationStatus(): Promise`
The same as `Camera.checkDeviceAuthorizationStatus()` but only checks the camera permission. Note, as of iOS 10, you will need to add `NSCameraUsageDescription` to your XCode project's Info.plist file or you might experience a crash.
#### `iOS` `Camera.checkAudioAuthorizationStatus(): Promise`
The same as `Camera.checkDeviceAuthorizationStatus()` but only checks the microphone permission. Note, as of iOS 10, you will need to add `NSMicrophoneUsageDescription` to your XCode project's Info.plist file or you might experience a crash.
## Subviews
This component supports subviews, so if you wish to use the camera view as a background or if you want to layout buttons/images/etc. inside the camera then you can do that.
## Example
To see more of the `RCTCamera` in action, you can check out the source in [Example](https://github.com/lwansbrough/react-native-camera/tree/master/Example) folder.
## Open Collective
We are just beginning a funding campaign for react-native-camera. Contributions are greatly appreciated. When we gain more than \$250 we will begin distributing funds to core maintainers in a fully transparent manner. Feedback for this process is welcomed, we will continue to evolve the strategy as we grow and learn more.
### Backers
Support us with a monthly donation and help us continue our activities. [[Become a backer](https://opencollective.com/react-native-camera#backer)]
<a href="https://opencollective.com/react-native-camera/backer/0/website" target="_blank"><img src="https://opencollective.com/react-native-camera/backer/0/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/backer/1/website" target="_blank"><img src="https://opencollective.com/react-native-camera/backer/1/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/backer/2/website" target="_blank"><img src="https://opencollective.com/react-native-camera/backer/2/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/backer/3/website" target="_blank"><img src="https://opencollective.com/react-native-camera/backer/3/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/backer/4/website" target="_blank"><img src="https://opencollective.com/react-native-camera/backer/4/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/backer/5/website" target="_blank"><img src="https://opencollective.com/react-native-camera/backer/5/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/backer/6/website" target="_blank"><img src="https://opencollective.com/react-native-camera/backer/6/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/backer/7/website" target="_blank"><img src="https://opencollective.com/react-native-camera/backer/7/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/backer/8/website" target="_blank"><img src="https://opencollective.com/react-native-camera/backer/8/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/backer/9/website" target="_blank"><img src="https://opencollective.com/react-native-camera/backer/9/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/backer/10/website" target="_blank"><img src="https://opencollective.com/react-native-camera/backer/10/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/backer/11/website" target="_blank"><img src="https://opencollective.com/react-native-camera/backer/11/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/backer/12/website" target="_blank"><img src="https://opencollective.com/react-native-camera/backer/12/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/backer/13/website" target="_blank"><img src="https://opencollective.com/react-native-camera/backer/13/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/backer/14/website" target="_blank"><img src="https://opencollective.com/react-native-camera/backer/14/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/backer/15/website" target="_blank"><img src="https://opencollective.com/react-native-camera/backer/15/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/backer/16/website" target="_blank"><img src="https://opencollective.com/react-native-camera/backer/16/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/backer/17/website" target="_blank"><img src="https://opencollective.com/react-native-camera/backer/17/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/backer/18/website" target="_blank"><img src="https://opencollective.com/react-native-camera/backer/18/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/backer/19/website" target="_blank"><img src="https://opencollective.com/react-native-camera/backer/19/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/backer/20/website" target="_blank"><img src="https://opencollective.com/react-native-camera/backer/20/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/backer/21/website" target="_blank"><img src="https://opencollective.com/react-native-camera/backer/21/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/backer/22/website" target="_blank"><img src="https://opencollective.com/react-native-camera/backer/22/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/backer/23/website" target="_blank"><img src="https://opencollective.com/react-native-camera/backer/23/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/backer/24/website" target="_blank"><img src="https://opencollective.com/react-native-camera/backer/24/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/backer/25/website" target="_blank"><img src="https://opencollective.com/react-native-camera/backer/25/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/backer/26/website" target="_blank"><img src="https://opencollective.com/react-native-camera/backer/26/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/backer/27/website" target="_blank"><img src="https://opencollective.com/react-native-camera/backer/27/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/backer/28/website" target="_blank"><img src="https://opencollective.com/react-native-camera/backer/28/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/backer/29/website" target="_blank"><img src="https://opencollective.com/react-native-camera/backer/29/avatar.svg"></a>
### Sponsors
Become a sponsor and get your logo on our README on Github with a link to your site. [[Become a sponsor](https://opencollective.com/react-native-camera#sponsor)]
<a href="https://opencollective.com/react-native-camera/sponsor/0/website" target="_blank"><img src="https://opencollective.com/react-native-camera/sponsor/0/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/sponsor/1/website" target="_blank"><img src="https://opencollective.com/react-native-camera/sponsor/1/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/sponsor/2/website" target="_blank"><img src="https://opencollective.com/react-native-camera/sponsor/2/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/sponsor/3/website" target="_blank"><img src="https://opencollective.com/react-native-camera/sponsor/3/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/sponsor/4/website" target="_blank"><img src="https://opencollective.com/react-native-camera/sponsor/4/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/sponsor/5/website" target="_blank"><img src="https://opencollective.com/react-native-camera/sponsor/5/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/sponsor/6/website" target="_blank"><img src="https://opencollective.com/react-native-camera/sponsor/6/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/sponsor/7/website" target="_blank"><img src="https://opencollective.com/react-native-camera/sponsor/7/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/sponsor/8/website" target="_blank"><img src="https://opencollective.com/react-native-camera/sponsor/8/avatar.svg"></a>
<a href="https://opencollective.com/react-native-camera/sponsor/9/website" target="_blank"><img src="https://opencollective.com/react-native-camera/sponsor/9/avatar.svg"></a>
---
Thanks to Brent Vatne (@brentvatne) for the `react-native-video` module which provided me with a great example of how to set up this module.

View File

@ -1,440 +0,0 @@
// @flow
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {
DeviceEventEmitter, // android
NativeAppEventEmitter, // ios
NativeModules,
Platform,
StyleSheet,
findNodeHandle,
requireNativeComponent,
ViewPropTypes,
ActivityIndicator,
View,
Text,
UIManager,
PermissionsAndroid,
} from 'react-native';
const requestPermissions = async (
hasVideoAndAudio,
CameraManager,
permissionDialogTitle,
permissionDialogMessage,
): Promise<boolean> => {
if (Platform.OS === 'ios') {
let check = hasVideoAndAudio
? CameraManager.checkDeviceAuthorizationStatus
: CameraManager.checkVideoAuthorizationStatus;
if (check) return await check();
} else if (Platform.OS === 'android') {
let params = undefined;
if (permissionDialogTitle || permissionDialogMessage)
params = { title: permissionDialogTitle, message: permissionDialogMessage };
const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.CAMERA, params);
if (!hasVideoAndAudio)
return granted === PermissionsAndroid.RESULTS.GRANTED || granted === true;
const grantedAudio = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.RECORD_AUDIO,
params,
);
return (
(granted === PermissionsAndroid.RESULTS.GRANTED || granted === true) &&
(grantedAudio === PermissionsAndroid.RESULTS.GRANTED || grantedAudio === true)
);
}
return true;
};
const styles = StyleSheet.create({
base: {},
authorizationContainer: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
notAuthorizedText: {
textAlign: 'center',
fontSize: 16,
},
});
const CameraManager = NativeModules.CameraManager || NativeModules.CameraModule;
function convertNativeProps(props) {
const newProps = { ...props };
if (typeof props.aspect === 'string') {
newProps.aspect = Camera.constants.Aspect[props.aspect];
}
if (typeof props.flashMode === 'string') {
newProps.flashMode = Camera.constants.FlashMode[props.flashMode];
}
if (typeof props.zoom === 'string' || typeof props.zoom === 'number') {
if (props.zoom >= 0 && props.zoom <= 100) {
newProps.zoom = parseInt(props.zoom);
}
}
if (typeof props.orientation === 'string') {
newProps.orientation = Camera.constants.Orientation[props.orientation];
}
if (typeof props.torchMode === 'string') {
newProps.torchMode = Camera.constants.TorchMode[props.torchMode];
}
if (typeof props.type === 'string') {
newProps.type = Camera.constants.Type[props.type];
}
if (typeof props.captureQuality === 'string') {
newProps.captureQuality = Camera.constants.CaptureQuality[props.captureQuality];
}
if (typeof props.captureMode === 'string') {
newProps.captureMode = Camera.constants.CaptureMode[props.captureMode];
}
if (typeof props.captureTarget === 'string') {
newProps.captureTarget = Camera.constants.CaptureTarget[props.captureTarget];
}
// do not register barCodeTypes if no barcode listener
if (typeof props.onBarCodeRead !== 'function') {
newProps.barCodeTypes = [];
}
newProps.barcodeScannerEnabled = typeof props.onBarCodeRead === 'function';
return newProps;
}
export default class Camera extends Component {
static constants = {
Aspect: CameraManager.Aspect,
BarCodeType: CameraManager.BarCodeType,
Type: CameraManager.Type,
CaptureMode: CameraManager.CaptureMode,
CaptureTarget: CameraManager.CaptureTarget,
CaptureQuality: CameraManager.CaptureQuality,
Orientation: CameraManager.Orientation,
FlashMode: CameraManager.FlashMode,
Zoom: CameraManager.Zoom,
TorchMode: CameraManager.TorchMode,
};
static propTypes = {
...ViewPropTypes,
aspect: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
captureAudio: PropTypes.bool,
captureMode: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
captureQuality: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
captureTarget: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
defaultOnFocusComponent: PropTypes.bool,
flashMode: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
zoom: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
keepAwake: PropTypes.bool,
onBarCodeRead: PropTypes.func,
barcodeScannerEnabled: PropTypes.bool,
cropToPreview: PropTypes.bool,
clearWindowBackground: PropTypes.bool,
onFocusChanged: PropTypes.func,
onZoomChanged: PropTypes.func,
mirrorImage: PropTypes.bool,
mirrorVideo: PropTypes.bool,
fixOrientation: PropTypes.bool,
barCodeTypes: PropTypes.array,
orientation: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
playSoundOnCapture: PropTypes.bool,
torchMode: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
type: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
permissionDialogTitle: PropTypes.string,
permissionDialogMessage: PropTypes.string,
notAuthorizedView: PropTypes.element,
pendingAuthorizationView: PropTypes.element,
};
static defaultProps = {
aspect: CameraManager.Aspect.fill,
type: CameraManager.Type.back,
orientation: CameraManager.Orientation.auto,
fixOrientation: false,
captureAudio: false,
captureMode: CameraManager.CaptureMode.still,
captureTarget: CameraManager.CaptureTarget.cameraRoll,
captureQuality: CameraManager.CaptureQuality.high,
defaultOnFocusComponent: true,
flashMode: CameraManager.FlashMode.off,
zoom: 0,
playSoundOnCapture: true,
torchMode: CameraManager.TorchMode.off,
mirrorImage: false,
mirrorVideo: false,
cropToPreview: false,
clearWindowBackground: false,
barCodeTypes: Object.values(CameraManager.BarCodeType),
permissionDialogTitle: '',
permissionDialogMessage: '',
notAuthorizedView: (
<View style={styles.authorizationContainer}>
<Text style={styles.notAuthorizedText}>Camera not authorized</Text>
</View>
),
pendingAuthorizationView: (
<View style={styles.authorizationContainer}>
<ActivityIndicator size="small" />
</View>
),
};
static checkDeviceAuthorizationStatus = CameraManager.checkDeviceAuthorizationStatus;
static checkVideoAuthorizationStatus = CameraManager.checkVideoAuthorizationStatus;
static checkAudioAuthorizationStatus = CameraManager.checkAudioAuthorizationStatus;
setNativeProps(props) {
// eslint-disable-next-line
this._cameraRef.setNativeProps(props);
}
constructor() {
super();
this.state = {
isAuthorized: false,
isAuthorizationChecked: false,
isRecording: false,
};
this._cameraRef = null;
this._cameraHandle = null;
}
// eslint-disable-next-line
async componentWillMount() {
this._addOnBarCodeReadListener();
this._addOnFocusChanged();
this._addOnZoomChanged();
let { captureMode } = convertNativeProps({ captureMode: this.props.captureMode });
let hasVideoAndAudio =
this.props.captureAudio && captureMode === Camera.constants.CaptureMode.video;
const isAuthorized = await requestPermissions(
hasVideoAndAudio,
Camera,
this.props.permissionDialogTitle,
this.props.permissionDialogMessage,
);
this.setState({ isAuthorized, isAuthorizationChecked: true });
}
componentWillUnmount() {
this._removeOnBarCodeReadListener();
this._removeOnFocusChanged();
this._removeOnZoomChanged();
if (this.state.isRecording) {
this.stopCapture();
}
}
// eslint-disable-next-line
componentWillReceiveProps(newProps) {
const { onBarCodeRead, onFocusChanged, onZoomChanged } = this.props;
if (onBarCodeRead !== newProps.onBarCodeRead) {
this._addOnBarCodeReadListener(newProps);
}
if (onFocusChanged !== !newProps.onFocusChanged) {
this._addOnFocusChanged(newProps);
}
if (onZoomChanged !== !newProps.onZoomChanged) {
this._addOnZoomChanged(newProps);
}
}
_addOnBarCodeReadListener(props) {
const { onBarCodeRead } = props || this.props;
this._removeOnBarCodeReadListener();
if (onBarCodeRead) {
this.cameraBarCodeReadListener = Platform.select({
ios: NativeAppEventEmitter.addListener('CameraBarCodeRead', this._onBarCodeRead),
android: DeviceEventEmitter.addListener('CameraBarCodeReadAndroid', this._onBarCodeRead),
windows: DeviceEventEmitter.addListener('CameraBarCodeReadWindows', this._onBarCodeRead),
});
}
}
_addOnFocusChanged(props) {
if (Platform.OS === 'ios') {
const { onFocusChanged } = props || this.props;
this.focusListener = NativeAppEventEmitter.addListener('focusChanged', onFocusChanged);
}
}
_addOnZoomChanged(props) {
if (Platform.OS === 'ios') {
const { onZoomChanged } = props || this.props;
this.zoomListener = NativeAppEventEmitter.addListener('zoomChanged', onZoomChanged);
}
}
_removeOnBarCodeReadListener() {
const listener = this.cameraBarCodeReadListener;
if (listener) {
listener.remove();
}
}
_removeOnFocusChanged() {
const listener = this.focusListener;
if (listener) {
listener.remove();
}
}
_removeOnZoomChanged() {
const listener = this.zoomListener;
if (listener) {
listener.remove();
}
}
_setReference = ref => {
if (ref) {
this._cameraRef = ref;
this._cameraHandle = findNodeHandle(ref);
} else {
this._cameraRef = null;
this._cameraHandle = null;
}
};
render() {
// TODO - style is not used, figure it out why
// eslint-disable-next-line
const style = [styles.base, this.props.style];
const nativeProps = convertNativeProps(this.props);
if (this.state.isAuthorized) {
return <RCTCamera ref={this._setReference} {...nativeProps} />;
} else if (!this.state.isAuthorizationChecked) {
return this.props.pendingAuthorizationView;
} else {
return this.props.notAuthorizedView;
}
}
_onBarCodeRead = data => {
if (this.props.onBarCodeRead) {
this.props.onBarCodeRead(data);
}
};
capture(options) {
const props = convertNativeProps(this.props);
options = {
audio: props.captureAudio,
barCodeTypes: props.barCodeTypes,
mode: props.captureMode,
playSoundOnCapture: props.playSoundOnCapture,
target: props.captureTarget,
quality: props.captureQuality,
type: props.type,
title: '',
description: '',
mirrorImage: props.mirrorImage,
mirrorVideo: props.mirrorVideo,
fixOrientation: props.fixOrientation,
cropToPreview: props.cropToPreview,
...options,
};
if (Platform.OS === 'windows') {
options['view'] = this._cameraHandle;
}
if (options.mode === Camera.constants.CaptureMode.video) {
options.totalSeconds = options.totalSeconds > -1 ? options.totalSeconds : -1;
options.preferredTimeScale = options.preferredTimeScale || 30;
options.cropToPreview = false;
this.setState({ isRecording: true });
}
return CameraManager.capture(options);
}
startPreview() {
if (Platform.OS === 'android') {
UIManager.dispatchViewManagerCommand(
this._cameraHandle,
UIManager.RCTCamera.Commands.startPreview,
[],
);
} else {
CameraManager.startPreview();
}
}
stopPreview() {
if (Platform.OS === 'android') {
UIManager.dispatchViewManagerCommand(
this._cameraHandle,
UIManager.RCTCamera.Commands.stopPreview,
[],
);
} else {
CameraManager.stopPreview();
}
}
stopCapture() {
if (this.state.isRecording) {
this.setState({ isRecording: false });
return CameraManager.stopCapture();
}
return Promise.resolve('Not Recording.');
}
getFOV() {
return CameraManager.getFOV();
}
hasFlash() {
if (Platform.OS === 'android') {
const props = convertNativeProps(this.props);
return CameraManager.hasFlash({
type: props.type,
});
} else if (Platform.OS === 'windows') {
return CameraManager.hasFlash({
view: this._cameraHandle,
});
}
return CameraManager.hasFlash();
}
setZoom(zoom) {
if (Platform.OS === 'android') {
const props = convertNativeProps(this.props);
return CameraManager.setZoom(
{
type: props.type,
},
zoom,
);
}
return CameraManager.setZoom(zoom);
}
}
export const constants = Camera.constants;
const RCTCamera = requireNativeComponent('RCTCamera', Camera, {
nativeOnly: {
testID: true,
renderToHardwareTextureAndroid: true,
accessibilityLabel: true,
importantForAccessibility: true,
accessibilityLiveRegion: true,
accessibilityComponentType: true,
onLayout: true,
},
});

View File

@ -1,10 +1,6 @@
// @flow
import Camera from './Camera';
import RNCamera, { type Status as _CameraStatus } from './RNCamera';
import FaceDetector from './FaceDetector';
export type CameraStatus = _CameraStatus;
export { RNCamera, FaceDetector };
export default Camera;