如何在AVCaptureSession中添加增加降低相机亮度的Slider

时间:2013-07-30 11:05:55

标签: iphone ios avcapturesession

到目前为止,我已经提出相机放一个滑块,现在变焦缩小相机,但现在我想放另一个滑块,增加和减少我搜索到的相机的亮度,我需要使用AVCaptureVideoDataOutput并采取单帧我可以使用委托方法并处理它并执行您想要对该框架执行的操作我将在下面发布我的代码并解释其中的其他内容

    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 
  }

2 个答案:

答案 0 :(得分:3)

为此,只需使用波纹管代码设置主屏幕的亮度..

[[UIScreen mainScreen] setBrightness:sliderValue];
滑块值设置值

从0到1,如下所示..

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

及其UIControlEventValueChanged调用任何类似bellow的方法并更改值,请参阅下面的示例..

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

答案 1 :(得分:1)

@Paras Joshi的答案很完美

我只是为快速用户更新答案

@IBOutlet weak var slider : UISlider!

将滑块的最小值设置为0,最大值为1,然后在视图中加载时,只需将滑块的当前值设置为主屏幕即可。

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

最终创建滑块值更改操作并将值提供给主屏幕

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

您都可以构建并运行项目