为什么我在ios上使用相同的iso和曝光时间获得了不同的图像亮度?

时间:2019-07-10 07:04:26

标签: ios objective-c camera

我在ios上设置了iso和曝光时间,但是照片的亮度总是不同的。

我想在ios上创建一个自控相机APP,设置曝光时间和iso后,在蚂蚁自动曝光到不同场景后,照片亮度总是不同的。在iPhone上,除了iso和曝光时间会影响照片的亮度外,我可以控制的其他任何参数也会影响照片的亮度。

 if ( self.session )
    {
        //    AVCaptureSessionPresetHigh
        if ( [self.session canSetSessionPreset:AVCaptureSessionPreset3840x2160] ) {
            //设置为4K
            self.session.sessionPreset= AVCaptureSessionPreset3840x2160;
        }
        /* 获取相机设备 */
        if ( !self.device ) {
            self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        }

        /* 创建输入对象 */
        NSError *error;
        if (!self.input) //容错,如果input已存在则input关联的device配置关系就未丢失
        {
            //强制关联。device与input输出配置,如果监测到当前input失去生命
            self.input = [[AVCaptureDeviceInput alloc] initWithDevice:self.device error:&error];
        }

        /* 更新相机输出对象 */
        if ( self.outPut )
        {
            /* 视频格式 */
            switch (picture_format)
            {
                case PICTURE_FORMAT_RGB: //RGB格式
                    [self.outPut setVideoSettings:@{(id)kCVPixelBufferPixelFormatTypeKey : [NSNumber numberWithInt:kCVPixelFormatType_32BGRA]}];
                    break;
                case PICTURE_FORMAT_YUV: //YUV格式
                    [self.outPut setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
                    break;
                default:
                    [self.outPut setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
                    break;
            } // end of switch (pictureFormat)

            [self.outPut setAlwaysDiscardsLateVideoFrames:YES];

            /* 添加输入 */
            if ( [self.session canAddInput:self.input] ) {
                [self.session addInput:self.input];
            }

            /* 添加输出 */
            if ([self.session canAddOutput:self.outPut]) {
                [self.session addOutput:self.outPut];
            }

            /* 链接对象 */
            AVCaptureConnection *connection = [self.outPut connectionWithMediaType:AVMediaTypeVideo];
            /* 视频方向 */
            connection.videoOrientation = AVCaptureVideoOrientationPortrait;

            self.sesstion_connection = connection;

            if ( [self.session canAddConnection:connection] ) {
                [self.session addConnection:connection];
            }

            //kvo setting--->>>
            self.exp_status = NO;
            [self.input.device addObserver:self forKeyPath:@"exposureTargetOffset" options:NSKeyValueObservingOptionOld |NSKeyValueObservingOptionNew context:nil];
#if 0
            NSError *error = nil;
            if ( [self.input.device lockForConfiguration:&error] ) {
                if ([self.input.device isExposureModeSupported:AVCaptureExposureModeAutoExpose]) {
                    self.input.device.exposurePointOfInterest = CGPointMake(0, 0);
                    [self.input.device setExposureMode:AVCaptureExposureModeAutoExpose];
                }
                [self.input.device unlockForConfiguration];
            }
            else {
                NSLog( @"Could not lock device for configuration: %@", error );
            }
#endif

        //set the exposure target bias
        if ( [self.input.device lockForConfiguration:nil] ) {
            [self.input.device setExposureTargetBias:0 completionHandler:nil];
            [self.input.device unlockForConfiguration];
        }
        else {
            NSLog( @"Could not lock device for configuration: %@", error );
        }

            if ( [self.input.device lockForConfiguration:nil] ) {
                [self.input.device setAutomaticallyAdjustsVideoHDREnabled:NO];
                if ([self.input.device.activeFormat isVideoHDRSupported] ) {
                    [self.input.device setVideoHDREnabled:NO];
                }
            } else {
                NSLog( @"Could not lock device for configuration...");
            }
#if 0
            if ( [self.input.device lockForConfiguration:nil] ) {
                if ( [self.input.device isWhiteBalanceModeSupported:AVCaptureWhiteBalanceModeLocked] ) {
                AVCaptureWhiteBalanceGains gains = {CAM_WHITEBALACE_R_VALUE, CAM_WHITEBALACE_G_VALUE, CAM_WHITEBALACE_B_VALUE};
                [self.input.device setWhiteBalanceModeLockedWithDeviceWhiteBalanceGains:gains completionHandler:^(CMTime syncTime) {}];
                }
            } else {
                NSLog( @"Could not lock device for configuration...");
            }
#else
        if ( [self.input.device lockForConfiguration:nil] ) {
            if ( [self.input.device isWhiteBalanceModeSupported:AVCaptureWhiteBalanceModeAutoWhiteBalance] ) {
                [self.input.device setWhiteBalanceMode:AVCaptureWhiteBalanceModeAutoWhiteBalance];
            }
        } else {
            NSLog( @"Could not lock device for configuration...");
        }

#endif
        } // end of if (self.outPut)
    }

0 个答案:

没有答案