You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过AVCaptureOutput获取实时相机画面的白平衡(开尔文值)

实时获取相机画面的白平衡开尔文值

你当前的代码仅在相机初始化阶段获取了一次白平衡增益对应的开尔文值,此时相机还未开始实时采集画面,因此返回的是默认固定值。要实现实时获取,需在相机运行过程中动态读取当前的白平衡参数。

核心步骤

  • 先为相机开启自动白平衡模式:确认设备支持该模式后,将whiteBalanceMode设置为.continuousAutoWhiteBalance
  • 在视频帧回调方法中,实时读取当前相机的白平衡增益,再转换为开尔文值
  • 操作相机配置时必须加锁,避免线程冲突

修改后的示例代码

import AVFoundation

class CameraManager: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate {
    private let captureSession = AVCaptureSession()
    
    func setupCamera() {
        guard let videoDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back) else {
            return
        }
        
        // 配置自动白平衡
        do {
            try videoDevice.lockForConfiguration()
            if videoDevice.isWhiteBalanceModeSupported(.continuousAutoWhiteBalance) {
                videoDevice.whiteBalanceMode = .continuousAutoWhiteBalance
            }
            videoDevice.unlockForConfiguration()
        } catch {
            print("相机配置失败: \(error.localizedDescription)")
            return
        }
        
        guard let videoDeviceInput = try? AVCaptureDeviceInput(device: videoDevice),
              captureSession.canAddInput(videoDeviceInput) else {
            print("设备相机异常")
            return
        }
        
        captureSession.addInput(videoDeviceInput)
        
        let captureOutput = AVCaptureVideoDataOutput()
        captureOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_32BGRA)]
        captureOutput.setSampleBufferDelegate(self, queue: DispatchQueue.global(qos: .default))
        
        if captureSession.canAddOutput(captureOutput) {
            captureSession.addOutput(captureOutput)
        }
        
        // 启动采集会话
        captureSession.startRunning()
    }
    
    func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
        guard let videoDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back) else {
            return
        }
        
        // 实时读取白平衡开尔文值
        do {
            try videoDevice.lockForConfiguration()
            let currentGains = videoDevice.deviceWhiteBalanceGains
            let kelvinData = videoDevice.temperatureAndTintValues(for: currentGains)
            print("实时开尔文温度: \(kelvinData.temperature)")
            print("实时色调值: \(kelvinData.tint)")
            videoDevice.unlockForConfiguration()
        } catch {
            print("获取白平衡数据失败: \(error.localizedDescription)")
        }
    }
}

关键说明

  • 必须启动captureSession让相机进入实时采集状态,白平衡增益才会随画面变化
  • 只有在视频帧回调中读取的增益值,才是当前画面对应的实时参数
  • 所有修改或读取相机配置的操作,都需要用lockForConfiguration()和unlockForConfiguration()包裹,保证线程安全

内容的提问来源于stack exchange,提问作者Paul S.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 01:11:31