• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何在 IOS 相机中启用防抖和 HDR?

[复制链接]
菜鸟教程小白 发表于 2022-12-11 18:31:34 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我做了什么:-

我已尝试启用 stabilizationHDR,但它不起作用。我认为我的方法正确。当我尝试检查当前设备是否支持稳定时和 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
    }



Best Answer-推荐答案


在稳定问题上对我有用的是在委托(delegate)中对其进行测试。在我的项目中,我使用 AVCaptureVideoDataOutputSampleBufferDelegate 写入文件,因为我在决定写入之前测试像素缓冲区中的某些内容。这是我发现的一个地方,它会说允许稳定。无论如何,这是我为稳定问题所做的。希望对您有所帮助。

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
        }
    }
}

关于ios - 如何在 IOS 相机中启用防抖和 HDR?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45790345/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap