iOS Native Camera AVFoundation亮度曝光差异

时间:2016-03-02 19:36:31

标签: ios swift

AVFoundation亮度/曝光与本机相机不同。是否可以使用AVFoundation框架复制本机相机设置?

2 个答案:

答案 0 :(得分:0)

如果您正在寻找自动曝光,请为" adjustExposure"设置键值观察器。并将曝光模式设置为AVCaptureExposureModeContinuousAutoExposure。如果您尝试实施手动曝光,请查看此WWDC session on camera controls

答案 1 :(得分:0)

在Swift 4.2中自动曝光

// MARK: Exposure Methods
    @objc
    func tapToExpose(recognizer: UIGestureRecognizer){
        if activeInput.device.isExposurePointOfInterestSupported{
            let point = recognizer.location(in: camPreview)
            // The tap location is converted from the `GestureRecognizer` to the preview's coordinates.

            let pointOfInterest = previewLayer.captureDevicePointConverted(fromLayerPoint: point)
            // Then , the second conversion is made for the coordinate space of the camera.

            showMarkerAtPoint(point: point, marker: exposureMarker)
            exposeAtPoint(pointOfInterest)
        }
    }



    func exposeAtPoint(_ point: CGPoint){
        let device = activeInput.device
        if device.isExposurePointOfInterestSupported, device.isFocusModeSupported(.continuousAutoFocus){
            do{
                try device.lockForConfiguration()
                device.exposurePointOfInterest = point
                device.exposureMode = .continuousAutoExposure
                if device.isFocusModeSupported(.locked){

                    //  Now let us add the illumination for the `observeValueForKeyPath` method,
                    device.addObserver(self, forKeyPath: kExposure, options: .new, context: &adjustingExposureContext)

                    device.unlockForConfiguration()
                }

            }
            catch{
                print("Error Exposing on POI: \(String(describing: error.localizedDescription))")
            }
        }
    }


    // MARK: KVO

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {

        //  First , check to make sure that the context matches the adjusting exposure context,
        //  Otherwise, pass the observation on to super.

        if context == &adjustingExposureContext {
            let device = object as! AVCaptureDevice

            // then determine if the camera has stopped adjusting exposure , and if the locked mode is supported.

            if !device.isAdjustingExposure , device.isExposureModeSupported(.locked){
                // remove self from the observer to stop subsequent notification,
                device.removeObserver(self, forKeyPath: kExposure, context: &adjustingExposureContext)
                DispatchQueue.main.async {
                    do{
                        try device.lockForConfiguration()
                        device.exposureMode = .locked
                        device.unlockForConfiguration()
                    }
                    catch{
                        print("Error exposing on POI: \(String(describing: error.localizedDescription))")
                    }

                }// DispatchQueue.main.async

            }// if !device.isAdjustingExposure , device.isExposureModeSupported(.locked)


        }// if context == &adjustingExposureContext {
        else{
            super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
        }
    }