Search code examples
iosobjective-cavfoundationavcapturesession

AVCaptureSession Brightness


I am developing a mirror app using front-camera with AVFoundation. I have already completed to display camera screen to UIView. But How can I adjust brightness? The codes are like this:

-(void)AVCaptureInit {   
    mCameraAVSession = [[AVCaptureSession alloc] init];
    [mCameraAVSession setSessionPreset:AVCaptureSessionPresetPhoto];

    mCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    for (AVCaptureDevice *device in devices) {
        if ([device position] == AVCaptureDevicePositionFront) {
            mCaptureDevice = device;
            break;
        }
    }

    //if ([mCaptureDevice hasTorch] && [mCaptureDevice hasFlash])
    {
        [mCaptureDevice lockForConfiguration:nil];
        // [mCaptureDevice setTorchMode:AVCaptureTorchModeOn];
        //[mCaptureDevice setExposurePointOfInterest:0.5];

        [mCaptureDevice setExposureMode:AVCaptureExposureModeManual];

        [mCaptureDevice unlockForConfiguration];
    }



    // [inputDevice setTorchModeOnWithLevel:0.5 error:NULL];

    NSError *error;
    AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:mCaptureDevice error:&error];

    if ([mCameraAVSession canAddInput:deviceInput]) {
        [mCameraAVSession addInput:deviceInput];
    }

    AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:mCameraAVSession];
    [previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    CALayer *rootLayer = [mCameraView layer];
    [rootLayer setMasksToBounds:YES];
    CGRect frame = mCaptureView.frame;
    [previewLayer setFrame:frame];
    [rootLayer insertSublayer:previewLayer atIndex:0];

    mCameraImageOutput = [[AVCaptureStillImageOutput alloc] init];
    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG, AVVideoCodecKey, nil];
    [mCameraImageOutput setOutputSettings:outputSettings];


    [mCameraAVSession addOutput:mCameraImageOutput];

    [mCameraAVSession startRunning];

    [self setVisible:mCaptureImage IsVisible:NO];
}

Anybody said that it would be possible to adjust brightness with exposure, but I don't know how to use it. Especially, I want to adjust camera brightness when I pinch.


Solution

  • Manual (custom) camera exposure is described in WWDC2014 Session 508 and the Manual AVCam Sample Code.

    Try it for your self by running AVCamManual, tapping Exposure > Custom and dragging the Exposure slider.

    In code, it boils down to setting your AVCaptureDevice exposureMode to AVCaptureExposureModeCustom and calling

    if ([mCaptureDevice lockForConfiguration:&error]) {
        [mCaptureDevice setExposureModeCustomWithDuration:exposureDuration ISO:AVCaptureISOCurrent completionHandler:nil];
        [mCaptureDevice unlockForConfiguration];
    }
    

    p.s. I'm not sure where you're getting AVCaptureExposureModeManual from. It doesn't seem to exist.