Skip to content

Commit

Permalink
feat(ios): Reset cached exposure limits to avoid a crash. (react-nati…
Browse files Browse the repository at this point in the history
…ve-camera#2948)

Add exception handling to exposure updates just in case.

Co-authored-by: Cristiano Coelho <[email protected]>
  • Loading branch information
cristianoccazinsp and cristianocca authored Aug 21, 2020
1 parent 4763777 commit 7109ac7
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions ios/RN/RNCamera.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ - (id)initWithBridge:(RCTBridge *)bridge
self.previewLayer.needsDisplayOnBoundsChange = YES;
#endif
self.rectOfInterest = CGRectMake(0, 0, 1.0, 1.0);

UITapGestureRecognizer * tapHandler=[self createTapGestureRecognizer];
[self addGestureRecognizer:tapHandler];
UITapGestureRecognizer * doubleTabHandler=[self createDoubleTapGestureRecognizer];
Expand Down Expand Up @@ -107,14 +107,14 @@ -(UITapGestureRecognizer*)createDoubleTapGestureRecognizer
UITapGestureRecognizer *doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
doubleTapGestureRecognizer.numberOfTapsRequired = 2;
return doubleTapGestureRecognizer;

}
-(UITapGestureRecognizer*)createTapGestureRecognizer
{
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
tapGestureRecognizer.numberOfTapsRequired = 1;
return tapGestureRecognizer;

}
-(void)handleDoubleTap:(UITapGestureRecognizer*)doubleTapRecognizer {
[self handleTouch:doubleTapRecognizer isDoubleTap:true];
Expand Down Expand Up @@ -389,7 +389,7 @@ - (void)updateFlashMode
RCTLogWarn(@"%s: device doesn't support flash mode", __func__);
return;
}

[self lockDevice:device andApplySettings:^{
if ([device isTorchActive]) {
[device setTorchMode:AVCaptureTorchModeOff];
Expand Down Expand Up @@ -585,7 +585,7 @@ - (void)updateZoom {
[self lockDevice:device andApplySettings:^{
float maxZoom = [self getMaxZoomFactor:device];
device.videoZoomFactor = (maxZoom - 1) * self.zoom + 1;
}];
}];
}

- (void)updateWhiteBalance {
Expand All @@ -610,7 +610,7 @@ - (void)applyDefaultWhiteBalance {
.tint = 0,
};
AVCaptureWhiteBalanceGains rgbGains = [device deviceWhiteBalanceGainsForTemperatureAndTintValues:temperatureAndTint];

@try{
[device setWhiteBalanceModeLockedWithDeviceWhiteBalanceGains:rgbGains completionHandler:nil];
}
Expand All @@ -636,11 +636,11 @@ - (void)applyCustomWhiteBalance {
CGFloat redGain = rgbGains.redGain + self.customWhiteBalanceSettings.redGainOffset;
CGFloat greenGain = rgbGains.greenGain + self.customWhiteBalanceSettings.greenGainOffset;
CGFloat blueGain = rgbGains.blueGain + self.customWhiteBalanceSettings.blueGainOffset;

rgbGains.redGain = MAX(1.0f, MIN(device.maxWhiteBalanceGain, redGain));
rgbGains.greenGain = MAX(1.0f, MIN(device.maxWhiteBalanceGain, greenGain));
rgbGains.blueGain = MAX(1.0f, MIN(device.maxWhiteBalanceGain, blueGain));

@try{
[device setWhiteBalanceModeLockedWithDeviceWhiteBalanceGains:rgbGains completionHandler:nil];
} @catch(NSException *exception){
Expand Down Expand Up @@ -690,7 +690,13 @@ - (void)updateExposure
}

// Only set the ISO for now, duration will be default as a change might affect frame rate.
[device setExposureModeCustomWithDuration:AVCaptureExposureDurationCurrent ISO:appliedExposure completionHandler:nil];
@try{
[device setExposureModeCustomWithDuration:AVCaptureExposureDurationCurrent ISO:appliedExposure completionHandler:nil];
}
@catch(NSException *exception){
RCTLogError(@"Failed to update exposure: %@", exception);
}

} else {
RCTLog(@"Device does not support AVCaptureExposureModeCustom");
}
Expand Down Expand Up @@ -1508,6 +1514,13 @@ - (void)initializeCaptureSessionInput
self.session.sessionPreset = [self getDefaultPreset];


// reset iso cached values, these might be different
// from camera to camera. Otherwise, the camera may crash
// when changing cameras and exposure.
self.exposureIsoMin = 0;
self.exposureIsoMax = 0;


if ([self.session canAddInput:captureDeviceInput]) {
[self.session addInput:captureDeviceInput];

Expand Down Expand Up @@ -2222,7 +2235,7 @@ - (void)captureOutput:(AVCaptureOutput *)captureOutput
if (self.invertImageData) {
image = [RNImageUtils invertColors:image];
}

[self.barcodeDetector findBarcodesInFrame:image scaleX:scaleX scaleY:scaleY completed:^(NSArray * barcodes) {
NSDictionary *eventBarcode = @{@"type" : @"barcode", @"barcodes" : barcodes};
[self onBarcodesDetected:eventBarcode];
Expand Down

0 comments on commit 7109ac7

Please sign in to comment.