From 09cebdb1fef35fc1dfad088a58fb3566f87d6c45 Mon Sep 17 00:00:00 2001 From: souvick Date: Thu, 13 Jul 2017 20:01:32 +0530 Subject: [PATCH 1/3] Camera Class Updated --- LLSimpleCamera/LLSimpleCamera.h | 47 ++++++++++++ LLSimpleCamera/LLSimpleCamera.m | 129 +++++++++++++++++++++++++++++++- 2 files changed, 175 insertions(+), 1 deletion(-) diff --git a/LLSimpleCamera/LLSimpleCamera.h b/LLSimpleCamera/LLSimpleCamera.h index 4e9f9f5..d3df962 100644 --- a/LLSimpleCamera/LLSimpleCamera.h +++ b/LLSimpleCamera/LLSimpleCamera.h @@ -110,6 +110,53 @@ typedef enum : NSUInteger { */ @property (nonatomic) BOOL tapToFocus; +/** + * If user lock focus by calling lockFocus. Disabled by default. Tap to focus will not work if this is YES. + */ +@property (nonatomic, readonly) BOOL isFocusLockedByUser; + +/** + * Use this method to lock/unlock auto focus. This will set isFocusLockedByUser to YES/NO. + */ +- (void)lockFocus:(BOOL)shouldLockFocus; + +/** + * If user lock exposure by calling lockExposure. Disabled by default. + */ +@property (nonatomic, readonly) BOOL isExpouserLockedByUser; + +/** + * Use this method to lock/unlock exposure. This will set isExpouserLockedByUser to YES/NO. + */ +- (void)lockExposure:(BOOL)shouldLockExposure; + +/** + * Max frame rate for videoSupportedFrameRateRanges + */ +@property (nonatomic, readonly) float maxFrameRate; + +/** +* Min frame rate for videoSupportedFrameRateRanges +*/ +@property (nonatomic, readonly) float minFrameRate; + +/** + * Use this method to update frame rate within videoSupportedFrameRateRanges + */ +- (void)changeFrameRate:(float)frameRate; + +/** + * Use this method to update sound input level if available from device (6S mic don't have permission, + * using headphone will work though + * soundLavel range is 0 to 1. 0 is the minimum, 1 is the maximum + */ +- (void)changeInputSoundLavel:(float)soundLavel; + +/** + * Use this method to get current power level within value 0(min) to 1(max). + */ +- (float)getChannelSoundPowerLevel; + /** * Set YES if you your view controller does not allow autorotation, * however you want to take the device rotation into account no matter what. Disabled by default. diff --git a/LLSimpleCamera/LLSimpleCamera.m b/LLSimpleCamera/LLSimpleCamera.m index db75c9f..32d6ae6 100644 --- a/LLSimpleCamera/LLSimpleCamera.m +++ b/LLSimpleCamera/LLSimpleCamera.m @@ -19,6 +19,7 @@ @interface LLSimpleCamera () =frameRateRange.minFrameRate) { + if([device lockForConfiguration:&error]) { + [device setActiveVideoMinFrameDuration:CMTimeMake(1, frameRate)]; + [device setActiveVideoMaxFrameDuration:CMTimeMake(1, frameRate)]; + [device unlockForConfiguration]; + } else { + [self passError:error]; + } + } +} + +#pragma mark - Audio + +- (void)changeInputSoundLavel:(float)soundLavel { + if([[AVAudioSession sharedInstance] isInputGainSettable] && soundLavel>=0.0 && soundLavel<=1.0) { + NSError *error; + NSError *sessionError; + if(![AVAudioSession sharedInstance].category) + [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:&sessionError]; + [[AVAudioSession sharedInstance] setInputGain:soundLavel error:&error]; + + if(error) { + [self passError:error]; + } + + if(sessionError) { + [self passError:sessionError]; + } + } +} + +- (float)getChannelSoundPowerLevel { + if(_captureAudioDataOutput) { + NSArray *connections = _captureAudioDataOutput.connections; + if ([connections count] > 0) { + AVCaptureConnection *connection = [connections objectAtIndex:0]; + NSArray *audioChannels = connection.audioChannels; + float soundLevel = 0.0; + for (AVCaptureAudioChannel *channel in audioChannels) { + float avg = channel.averagePowerLevel;// value Range: 0:max -160:min + soundLevel = soundLevel+pow (10, avg / 20);// change to readable value Range: 0:min 1:max + } + return soundLevel/audioChannels.count; + } + } + + return 0.0; +} + + #pragma mark - UIViewController - (void)viewWillAppear:(BOOL)animated From 6366ad3b01f87a3c8d1a03cbe9138acba2f49756 Mon Sep 17 00:00:00 2001 From: souvick Date: Mon, 17 Jul 2017 11:28:14 +0530 Subject: [PATCH 2/3] View Controller Update --- LLSimpleCameraExample/HomeViewController.m | 143 ++++++++++++++++++++- 1 file changed, 141 insertions(+), 2 deletions(-) diff --git a/LLSimpleCameraExample/HomeViewController.m b/LLSimpleCameraExample/HomeViewController.m index 7ed78c1..718b10f 100644 --- a/LLSimpleCameraExample/HomeViewController.m +++ b/LLSimpleCameraExample/HomeViewController.m @@ -18,6 +18,14 @@ @interface HomeViewController () @property (strong, nonatomic) UIButton *switchButton; @property (strong, nonatomic) UIButton *flashButton; @property (strong, nonatomic) UISegmentedControl *segmentedControl; + +@property (strong, nonatomic) UIButton *lockFocusButton; +@property (strong, nonatomic) UIButton *lockExposureButton; +@property (strong, nonatomic) UISlider *frameRateSlider; +@property (strong, nonatomic) UILabel *frameRateLabel; +@property (strong, nonatomic) UIProgressView *soundLevelProgress; +@property (strong, nonatomic) UILabel *soundLevelProgressLabel; + @end @implementation HomeViewController @@ -25,7 +33,7 @@ @implementation HomeViewController - (void)viewDidLoad { [super viewDidLoad]; - + self.view.backgroundColor = [UIColor blackColor]; [self.navigationController setNavigationBarHidden:YES animated:NO]; @@ -65,6 +73,15 @@ - (void)viewDidLoad else { weakSelf.flashButton.hidden = YES; } + + if(weakSelf.frameRateSlider) { + weakSelf.frameRateSlider.value = weakSelf.camera.maxFrameRate; + weakSelf.frameRateLabel.text = [NSString stringWithFormat:@"Frame Rate \nMax:%.2f, Min:%.2f\nValue:%.2f",weakSelf.camera.minFrameRate,weakSelf.camera.maxFrameRate,weakSelf.camera.maxFrameRate]; + } + + [weakSelf updateLockFocusButtonTitle]; + [weakSelf updateLockExposureButtonTitle]; + }]; [self.camera setOnError:^(LLSimpleCamera *camera, NSError *error) { @@ -93,7 +110,7 @@ - (void)viewDidLoad } } }]; - + // ----- camera buttons -------- // // snap button to capture image @@ -135,6 +152,124 @@ - (void)viewDidLoad self.segmentedControl.tintColor = [UIColor whiteColor]; [self.segmentedControl addTarget:self action:@selector(segmentedControlValueChanged:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:self.segmentedControl]; + + // Button for focus lock + self.lockFocusButton = [UIButton buttonWithType:UIButtonTypeCustom]; + self.lockFocusButton.frame = CGRectMake(10.0, 10.0, 100.0, 50.0); + self.lockFocusButton.backgroundColor = [UIColor clearColor]; + self.lockFocusButton.layer.borderWidth = 1.0; + self.lockFocusButton.layer.borderColor = [[UIColor whiteColor] CGColor]; + [self.lockFocusButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; + self.lockFocusButton.titleLabel.font = [UIFont systemFontOfSize:12.0]; + [self.lockFocusButton addTarget:self action:@selector(lockFocusButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; + [self updateLockFocusButtonTitle]; + [self.view addSubview:self.lockFocusButton]; + + // Button for exposure lock + self.lockExposureButton = [UIButton buttonWithType:UIButtonTypeCustom]; + self.lockExposureButton.frame = CGRectMake(10.0, 70.0, 120.0, 50.0); + self.lockExposureButton.backgroundColor = [UIColor clearColor]; + self.lockExposureButton.layer.borderWidth = 1.0; + self.lockExposureButton.layer.borderColor = [[UIColor whiteColor] CGColor]; + [self.lockExposureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; + self.lockExposureButton.titleLabel.font = [UIFont systemFontOfSize:12.0]; + [self.lockExposureButton addTarget:self action:@selector(lockExposureButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; + [self updateLockExposureButtonTitle]; + [self.view addSubview:self.lockExposureButton]; + + self.frameRateLabel = [[UILabel alloc] initWithFrame:CGRectMake(5.0, 160.0, 130.0, 80.0)]; + self.frameRateLabel.text = @""; + self.frameRateLabel.font = [UIFont systemFontOfSize:12.0]; + self.frameRateLabel.numberOfLines = 0; + self.frameRateLabel.adjustsFontSizeToFitWidth = YES; + self.frameRateLabel.minimumScaleFactor = 10.0f/12.0f; + self.frameRateLabel.backgroundColor = [UIColor clearColor]; + self.frameRateLabel.textColor = [UIColor whiteColor]; + self.frameRateLabel.textAlignment = NSTextAlignmentCenter; + [self.view addSubview:self.frameRateLabel]; + + self.frameRateSlider = [[UISlider alloc] initWithFrame:CGRectMake(10.0, 130.0, 120.0, 50.0)]; + [self.frameRateSlider addTarget:self action:@selector(frameRateSliderValueChanged:) forControlEvents:UIControlEventValueChanged]; + [self.frameRateSlider setBackgroundColor:[UIColor clearColor]]; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + // Warning: Delaying 2 sec for camera to fully initialize, + // after start function is called from viewWillAppear + // You can call it from there after the camera initialization + self.frameRateSlider.minimumValue = self.camera.minFrameRate; + self.frameRateSlider.maximumValue = self.camera.maxFrameRate; + self.frameRateSlider.continuous = NO; + self.frameRateSlider.value = self.camera.maxFrameRate; + + self.frameRateLabel.text = [NSString stringWithFormat:@"Frame Rate \nMax:%.2f, Min:%.2f\nValue:%.2f",self.camera.minFrameRate,self.camera.maxFrameRate,self.camera.maxFrameRate]; + + [self.view addSubview:self.frameRateSlider]; + }); + + self.soundLevelProgress = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault]; + self.soundLevelProgress.progressTintColor = [UIColor colorWithRed:187.0/255 green:160.0/255 blue:209.0/255 alpha:1.0]; + [[self.soundLevelProgress layer]setFrame:CGRectMake(10.0, 250.0, 120, 20)]; + [[self.soundLevelProgress layer]setBorderColor:[UIColor whiteColor].CGColor]; + self.soundLevelProgress.trackTintColor = [UIColor clearColor]; + [self.soundLevelProgress setProgress:0.0 animated:YES]; + self.soundLevelProgress.progress=0.0; + [[self.soundLevelProgress layer]setCornerRadius:4.0]; + [[self.soundLevelProgress layer]setBorderWidth:1]; + [[self.soundLevelProgress layer]setMasksToBounds:TRUE]; + self.soundLevelProgress.clipsToBounds = YES; + + [self.view addSubview:self.soundLevelProgress]; + + [NSTimer scheduledTimerWithTimeInterval:1/30 repeats:YES block:^(NSTimer * _Nonnull timer) { + self.soundLevelProgress.progress = [self.camera getChannelSoundPowerLevel]; + }]; + self.soundLevelProgressLabel = [[UILabel alloc] initWithFrame:CGRectMake(5.0, 280.0, 130.0, 30.0)]; + self.soundLevelProgressLabel.text = @"Sound Power Level"; + self.soundLevelProgressLabel.font = [UIFont systemFontOfSize:12.0]; + self.soundLevelProgressLabel.numberOfLines = 0; + self.soundLevelProgressLabel.adjustsFontSizeToFitWidth = YES; + self.soundLevelProgressLabel.minimumScaleFactor = 10.0f/12.0f; + self.soundLevelProgressLabel.backgroundColor = [UIColor clearColor]; + self.soundLevelProgressLabel.textColor = [UIColor whiteColor]; + self.soundLevelProgressLabel.textAlignment = NSTextAlignmentCenter; + [self.view addSubview:self.soundLevelProgressLabel]; +} + +- (void)frameRateSliderValueChanged:(id)sender { + UISlider *slider = (UISlider*)sender; + [self.camera changeFrameRate:slider.value]; + self.frameRateLabel.text = [NSString stringWithFormat:@"Frame Rate \nMax:%.2f, Min:%.2f\nValue:%.2f",self.camera.minFrameRate,self.camera.maxFrameRate,slider.value]; +} + +- (IBAction)lockFocusButtonPressed:(id)sender { + if(!self.camera.isFocusLockedByUser) + [self.camera lockFocus:YES]; + else + [self.camera lockFocus:NO]; + [self updateLockFocusButtonTitle]; +} + +- (void)updateLockFocusButtonTitle { + if(self.camera.isFocusLockedByUser) + [self.lockFocusButton setTitle:@"Focus Lock: On" forState:UIControlStateNormal]; + else + [self.lockFocusButton setTitle:@"Focus Lock: Off" forState:UIControlStateNormal]; + +} + +- (IBAction)lockExposureButtonPressed:(id)sender { + if(!self.camera.isExpouserLockedByUser) + [self.camera lockExposure:YES]; + else + [self.camera lockExposure:NO]; + [self updateLockExposureButtonTitle]; +} + +- (void)updateLockExposureButtonTitle { + if(self.camera.isExpouserLockedByUser) + [self.lockExposureButton setTitle:@"Exposure Lock: On" forState:UIControlStateNormal]; + else + [self.lockExposureButton setTitle:@"Exposure Lock: Off" forState:UIControlStateNormal]; + } - (void)segmentedControlValueChanged:(UISegmentedControl *)control @@ -157,6 +292,10 @@ - (void)switchButtonPressed:(UIButton *)button [self.camera togglePosition]; } +- (void)callSOundLevel { + NSLog(@"%f",[self.camera getChannelSoundPowerLevel]); +} + - (NSURL *)applicationDocumentsDirectory { return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; From 573940c894acef16137a207ae738e56428a78e7b Mon Sep 17 00:00:00 2001 From: Souvick Ghosh Date: Mon, 17 Jul 2017 14:47:06 +0530 Subject: [PATCH 3/3] Update HomeViewController.m --- LLSimpleCameraExample/HomeViewController.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/LLSimpleCameraExample/HomeViewController.m b/LLSimpleCameraExample/HomeViewController.m index 718b10f..b31ff7a 100644 --- a/LLSimpleCameraExample/HomeViewController.m +++ b/LLSimpleCameraExample/HomeViewController.m @@ -76,7 +76,7 @@ - (void)viewDidLoad if(weakSelf.frameRateSlider) { weakSelf.frameRateSlider.value = weakSelf.camera.maxFrameRate; - weakSelf.frameRateLabel.text = [NSString stringWithFormat:@"Frame Rate \nMax:%.2f, Min:%.2f\nValue:%.2f",weakSelf.camera.minFrameRate,weakSelf.camera.maxFrameRate,weakSelf.camera.maxFrameRate]; + weakSelf.frameRateLabel.text = [NSString stringWithFormat:@"Frame Rate \nMin:%.2f, Max:%.2f\nValue:%.2f",weakSelf.camera.minFrameRate,weakSelf.camera.maxFrameRate,weakSelf.camera.maxFrameRate]; } [weakSelf updateLockFocusButtonTitle]; @@ -200,7 +200,7 @@ - (void)viewDidLoad self.frameRateSlider.continuous = NO; self.frameRateSlider.value = self.camera.maxFrameRate; - self.frameRateLabel.text = [NSString stringWithFormat:@"Frame Rate \nMax:%.2f, Min:%.2f\nValue:%.2f",self.camera.minFrameRate,self.camera.maxFrameRate,self.camera.maxFrameRate]; + self.frameRateLabel.text = [NSString stringWithFormat:@"Frame Rate \nMin:%.2f, Max:%.2f\nValue:%.2f",self.camera.minFrameRate,self.camera.maxFrameRate,self.camera.maxFrameRate]; [self.view addSubview:self.frameRateSlider]; }); @@ -237,7 +237,7 @@ - (void)viewDidLoad - (void)frameRateSliderValueChanged:(id)sender { UISlider *slider = (UISlider*)sender; [self.camera changeFrameRate:slider.value]; - self.frameRateLabel.text = [NSString stringWithFormat:@"Frame Rate \nMax:%.2f, Min:%.2f\nValue:%.2f",self.camera.minFrameRate,self.camera.maxFrameRate,slider.value]; + self.frameRateLabel.text = [NSString stringWithFormat:@"Frame Rate \nMin:%.2f, Max:%.2f\nValue:%.2f",self.camera.minFrameRate,self.camera.maxFrameRate,slider.value]; } - (IBAction)lockFocusButtonPressed:(id)sender {