java
java

Reputation: 239

How to Put Slider that increase decrease brightness of camera in AVCaptureSession

so far i have presented camera put one slider in that which zoom zoom out camera now but now I want to Put another Slider which increase and decrease brightness of camera I have searched on this and i need to use AVCaptureVideoDataOutput and taking single frame which i can get on delegate method and process it and do what you want to do with that frame I am posting my code below and explain other things in it

    AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
   // AVCaptureVideoDataOutput
    if (videoDevice) {
        NSError *error;
        AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
        if (!error) {
            if ([[self captureSession] canAddInput:videoIn])
                [[self captureSession] addInput:videoIn];
            else
                NSLog(@"Couldn't add video input");     
        }
        else
            NSLog(@"Couldn't create video input");
    }
    else
        NSLog(@"Couldn't create video capture device");

    AVCaptureVideoDataOutput *videoOut = [[AVCaptureVideoDataOutput alloc] init];
    /*
     RosyWriter prefers to discard late video frames early in the capture pipeline, since its
     processing can take longer than real-time on some platforms (such as iPhone 3GS).
     Clients whose image processing is faster than real-time should consider setting AVCaptureVideoDataOutput's
     alwaysDiscardsLateVideoFrames property to NO.
     */
    [videoOut setAlwaysDiscardsLateVideoFrames:YES];
    [videoOut setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
    dispatch_queue_t videoCaptureQueue = dispatch_queue_create("Video Capture Queue", DISPATCH_QUEUE_SERIAL);
    [videoOut setSampleBufferDelegate:self queue:videoCaptureQueue];
    dispatch_release(videoCaptureQueue);
    if ([captureSession canAddOutput:videoOut])
        [captureSession addOutput:videoOut];
}

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{

      I HAVE READ THAT NEED TO PROCESS FRAME AND THEN SEND IT BACK TO SESSION BUT HOW I DONT KNOW 
        THIS IS THE DELEGATE METHOD WHERE I GET FRAME NOW I DONT UNDERSTAND HOW TO PROCESS THIS 
FRAME WHAT SHOULD I DO SO I CAN INCREASE AND DECREASE BRIGHTNESS OF CAMERA PLEASE HELP ME IF YOU KNOW 
  }

Upvotes: 3

Views: 1871

Answers (2)

Naqeeb
Naqeeb

Reputation: 1421

@Paras Joshi answer is working perfect

i am just updating answer for swift user

@IBOutlet weak var slider : UISlider!

set slider minimum value to 0 and maximum to 1 and then in view did load just set the current value of slider to main screen.

UIScreen.main.brightness = CGFloat(slider.value)

finally create action of slider value change and give value to main screen

@IBAction func slider_value_changed(_ sender: UISlider) {
    UIScreen.main.brightness = CGFloat(sender.value)
}

you are all set build and run your project

Upvotes: 1

Paras Joshi
Paras Joshi

Reputation: 20541

For that just set Brightness of mainScreen with bellow code..

[[UIScreen mainScreen] setBrightness:sliderValue];

in slider value set value from 0 to 1 like bellow..

yourSlider.minimumValue = 0.f;
yourSlider.maximumValue = 1.0f;

and its UIControlEventValueChanged call any method like bellow and change the value , see bellow Example..

- (void)Brightness_Changed:(UISlider *)sender
{
    [[UIScreen mainScreen] setBrightness:sender.value];
}

Upvotes: 4

Related Questions