我正在使用带AVFoundation
的前置摄像头开发镜像应用程序。我已完成将相机屏幕显示到UIView
。但是我如何调整亮度?
代码是这样的:
-(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];
}
任何人都说可以通过曝光调节亮度,但我不知道如何使用它。特别是,我想在捏时调整相机亮度。
答案 0 :(得分:2)
WWDC2014 Session 508和Manual AVCam Sample Code中描述了手动(自定义)相机曝光。
通过运行AVCamManual,点击Exposure>尝试自己。自定义并拖动“曝光”滑块。
在代码中,归结为将AVCaptureDevice
exposureMode
设置为AVCaptureExposureModeCustom
并调用
if ([mCaptureDevice lockForConfiguration:&error]) {
[mCaptureDevice setExposureModeCustomWithDuration:exposureDuration ISO:AVCaptureISOCurrent completionHandler:nil];
[mCaptureDevice unlockForConfiguration];
}
P.S。我不知道你从哪里获得AVCaptureExposureModeManual
。它似乎不存在。