Skip to content
This repository has been archived by the owner on Jun 16, 2023. It is now read-only.

Commit

Permalink
feat: adds autoFocusPointOfInterest to iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
beaur committed Jun 18, 2018
1 parent c3696c5 commit 39cc29d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
8 changes: 8 additions & 0 deletions docs/RNCamera.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ Most cameras have a Auto Focus feature. It adjusts your camera lens position aut

Use the `autoFocus` property to specify the auto focus setting of your camera. `RNCamera.Constants.AutoFocus.on` turns it ON, `RNCamera.Constants.AutoFocus.off` turns it OFF.

#### `autoFocusPointOfInterest`

Values: Object `{ x: 0.5, y: 0.5 }`.

Setting this property causes the auto focus feature of the camera to attempt to focus on the part of the image at this coordiate.

Coordinates values are measured as floats from `0` to `1.0`. `{ x: 0, y: 0 }` will focus on the top left of the image, `{ x: 1, y: 1 }` will be the bottom right. Values are based on landscape mode with the home button on the right—this applies even if the device is in portrait mode.

#### `iOS` `captureAudio`

Values: `true` (Boolean), `false` (default)
Expand Down
2 changes: 2 additions & 0 deletions ios/RN/RNCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
@property (assign, nonatomic) NSInteger flashMode;
@property (assign, nonatomic) CGFloat zoom;
@property (assign, nonatomic) NSInteger autoFocus;
@property (copy, nonatomic) NSDictionary *autoFocusPointOfInterest;
@property (assign, nonatomic) float focusDepth;
@property (assign, nonatomic) NSInteger whiteBalance;
@property (nonatomic, assign, getter=isReadingBarCodes) BOOL barCodeReading;
Expand All @@ -37,6 +38,7 @@
- (void)updateFlashMode;
- (void)updateFocusMode;
- (void)updateFocusDepth;
- (void)updateAutoFocusPointOfInterest;
- (void)updateZoom;
- (void)updateWhiteBalance;
- (void)updateFaceDetecting:(id)isDetectingFaces;
Expand Down
29 changes: 29 additions & 0 deletions ios/RN/RNCamera.m
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,34 @@ - (void)updateFlashMode
[device unlockForConfiguration];
}

- (void)updateAutoFocusPointOfInterest
{
AVCaptureDevice *device = [self.videoCaptureDeviceInput device];
NSError *error = nil;

if (![device lockForConfiguration:&error]) {
if (error) {
RCTLogError(@"%s: %@", __func__, error);
}
return;
}

if ([self.autoFocusPointOfInterest objectForKey:@"x"] && [self.autoFocusPointOfInterest objectForKey:@"y"]) {
float xValue = [self.autoFocusPointOfInterest[@"x"] floatValue];
float yValue = [self.autoFocusPointOfInterest[@"y"] floatValue];
if ([device isFocusPointOfInterestSupported]) {
CGPoint autofocusPoint = CGPointMake(xValue, yValue);
[device setFocusPointOfInterest:autofocusPoint];
[device setFocusMode:AVCaptureFocusModeContinuousAutoFocus];
}
else {
RCTLogWarn(@"AutoFocusPointOfInterest not supported");
}
}

[device unlockForConfiguration];
}

- (void)updateFocusMode
{
AVCaptureDevice *device = [self.videoCaptureDeviceInput device];
Expand Down Expand Up @@ -560,6 +588,7 @@ - (void)initializeCaptureSessionInput
[self updateZoom];
[self updateFocusMode];
[self updateFocusDepth];
[self updateAutoFocusPointOfInterest];
[self updateWhiteBalance];
[self.previewLayer.connection setVideoOrientation:orientation];
[self _updateMetadataObjectsToRecognize];
Expand Down
6 changes: 6 additions & 0 deletions ios/RN/RNCameraManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ + (NSDictionary *)faceDetectorConstants
[view updateFocusMode];
}

RCT_CUSTOM_VIEW_PROPERTY(autoFocusPointOfInterest, NSDictionary, RNCamera)
{
[view setAutoFocusPointOfInterest:[RCTConvert NSDictionary:json]];
[view updateAutoFocusPointOfInterest];
}

RCT_CUSTOM_VIEW_PROPERTY(focusDepth, NSNumber, RNCamera)
{
[view setFocusDepth:[RCTConvert float:json]];
Expand Down
6 changes: 4 additions & 2 deletions src/RNCamera.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const styles = StyleSheet.create({
},
});

type Orientation = "auto"|"landscapeLeft"|"landscapeRight"|"portrait"|"portraitUpsideDown";
type Orientation = 'auto' | 'landscapeLeft' | 'landscapeRight' | 'portrait' | 'portraitUpsideDown';

type PictureOptions = {
quality?: number,
Expand Down Expand Up @@ -90,6 +90,7 @@ type PropsType = typeof View.props & {
whiteBalance?: number | string,
faceDetectionLandmarks?: number,
autoFocus?: string | boolean | number,
autoFocusPointOfInterest?: { x: number, y: number },
faceDetectionClassifications?: number,
onFacesDetected?: ({ faces: Array<TrackedFaceFeature> }) => void,
onTextRecognized?: ({ textBlocks: Array<TrackedTextFeature> }) => void,
Expand Down Expand Up @@ -188,6 +189,7 @@ export default class Camera extends React.Component<PropsType, StateType> {
flashMode: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
whiteBalance: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
autoFocus: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool]),
autoFocusPointOfInterest: PropTypes.shape({ x: PropTypes.number, y: PropTypes.number }),
permissionDialogTitle: PropTypes.string,
permissionDialogMessage: PropTypes.string,
notAuthorizedView: PropTypes.element,
Expand Down Expand Up @@ -318,7 +320,7 @@ export default class Camera extends React.Component<PropsType, StateType> {
}
};

async componentWillMount() {
async UNSAFE_componentWillMount() {
const hasVideoAndAudio = this.props.captureAudio;
const isAuthorized = await requestPermissions(
hasVideoAndAudio,
Expand Down

0 comments on commit 39cc29d

Please sign in to comment.