如何在IOS相机中启用稳定和HDR?

时间:2017-08-21 06:13:48

标签: ios swift3 avfoundation avcapturesession avcam

我做了什么: -

我尝试启用稳定 HDR ,但它无法正常工作。我想我走的是正确的道路。当我试图检查时当前设备支持稳定和HDR,因为我在所有设备中都只有假例。

如果在以下代码段中出现任何错误,请引导我。

提前致谢!!

我的代码段: -

func createAVSession() throws -> AVCaptureSession {
        AppLog.LogFunction(object: LOG_Start)

        // Start out with low quality
        let session = AVCaptureSession()
        session.sessionPreset = AVCaptureSessionPresetPhoto

        // Input from video camera
        let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
        let currentFormat = device?.activeFormat.isVideoHDRSupported
        try device?.lockForConfiguration()
        if device?.activeFormat.isVideoHDRSupported == true {
            device?.automaticallyAdjustsVideoHDREnabled = false
            device?.isVideoHDREnabled = true
            print("device?.isVideoHDREnabled\(device?.isVideoHDREnabled)")
        }

        if (device?.isFocusModeSupported(.continuousAutoFocus))! {
            device?.focusMode = AVCaptureFocusMode.continuousAutoFocus
            print("device?.focusMode\(device?.focusMode.rawValue)")
        }
        if (device?.isSmoothAutoFocusSupported)! {
            device?.isSmoothAutoFocusEnabled = true
            print("device?.isSmoothAutoFocusEnabled\(device?.isSmoothAutoFocusEnabled)")
        }

        if (device?.isExposureModeSupported(.continuousAutoExposure))! {
            device?.exposureMode = .continuousAutoExposure
            print("device?.exposureMode\(device?.exposureMode.rawValue)")
        }
        device?.unlockForConfiguration()

        let input = try AVCaptureDeviceInput(device: device)
        do {
            try input.device.lockForConfiguration()
            input.device.activeVideoMaxFrameDuration =  CMTimeMake(1, 30)
            input.device.activeVideoMinFrameDuration =  CMTimeMake(1, 30)
            input.device.unlockForConfiguration()
        }
        catch {
            print("Failed to set FPS")
        }

        // Output
        let videoOutput = AVCaptureVideoDataOutput()

        videoOutput.videoSettings = [ kCVPixelBufferPixelFormatTypeKey as AnyHashable: kCVPixelFormatType_32BGRA]
        videoOutput.alwaysDiscardsLateVideoFrames = true
        videoOutput.setSampleBufferDelegate(self, queue: sessionQueue)

        let stillImageOutput: AVCaptureStillImageOutput = AVCaptureStillImageOutput()
        stillImageOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]

        //stillImageOutput.isHighResolutionStillImageOutputEnabled = true

        if stillImageOutput.isStillImageStabilizationSupported {
            stillImageOutput.automaticallyEnablesStillImageStabilizationWhenAvailable = true
            print("stillImageOutput.isStillImageStabilizationActive\(stillImageOutput.isStillImageStabilizationActive)")
        }

        // Join it all together
        session.addInput(input)
        session.addOutput(videoOutput)

      if session.canAddOutput(stillImageOutput) {
            session.addOutput(stillImageOutput)
          self.stillImageOutput = stillImageOutput

      }



        if let connection = videoOutput.connection(withMediaType: AVMediaTypeVideo) {
            if connection.isVideoOrientationSupported {
                connection.videoOrientation = .portrait
            }
            if connection.isVideoStabilizationSupported {
                connection.preferredVideoStabilizationMode = .standard
                print("connection.activeVideoStabilizationMode\(connection.activeVideoStabilizationMode.rawValue)")
            }
        }


        AppLog.LogFunction(object: LOG_End)
        return session
    }

1 个答案:

答案 0 :(得分:0)

在稳定性问题上对我有用的是在代表中测试它。在我的项目中,我使用AVCaptureVideoDataOutputSampleBufferDelegate写入我的文件,因为我在决定写入之前测试pixelBuffer中的某些内容。这是我发现允许稳定的地方。无论如何,这是我如何为稳定问题做的。希望它有所帮助。

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!){


self.lockQueue.sync {

    if !self.isCapturing || self.isPaused {

        return
    }



    let isVideo = captureOutput is AVCaptureVideoDataOutput

    if(isVideo && self.videoWriter == nil){

        // testing to make sure dealing with video and not audio

        let connection = captureOutput.connection(withMediaType: AVMediaTypeVideo)


        if (connection?.isVideoStabilizationSupported)! {
            connection?.preferredVideoStabilizationMode = AVCaptureVideoStabilizationMode.cinematic
        }

        //other work excluded as irrelevant
    }
}

}