集成自定义相机视图AVCaptureDevice

时间:2017-06-14 00:36:14

标签: ios swift camera avcapturedevice

我尝试集成自定义相机视图并遵循一些稍微过时的代码。我有几个错误,但我相信我已经修好了吧。

以下是目前的代码:

import Foundation
import AVFoundation
import UIKit

class setupView : UIViewController {

@IBOutlet var cameraView: UIView!
@IBOutlet var nameTextField: UITextField!

var captureSession = AVCaptureSession()
var stillImageOutput = AVCapturePhotoOutput()
var previewLayer = AVCaptureVideoPreviewLayer()

override func viewDidLoad() {

       let session = AVCaptureDeviceDiscoverySession.init(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: .back)
    if let device = session?.devices[0] {

        if device.position == AVCaptureDevicePosition.back {

            do {

                let input = try AVCaptureDeviceInput(device: device )
                if captureSession.canAddInput(input){
                    captureSession.addInput(input)
                    stillImageOutput.outputSettings = [AVVideoCodecKey : AVVideoCodecJPEG]

                    if captureSession.canAddOutput(stillImageOutput) {
                        captureSession.addOutput(stillImageOutput)
                        captureSession.startRunning()

                        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                        previewLayer.AVLayerVideoGravityResizeAspectFill
                        previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.portrait
                        cameraView.layer.addSublayer(previewLayer)

                        previewLayer.bounds = cameraView.frame
                        previewLayer.position = CGPoint(x: cameraView.frame.width / 2, y:cameraView.frame.height / 2)

                    }

                }

            }                catch {



            }
        }
    }

}

@IBAction func takePhoto(_ sender: Any) {
    }

@IBAction func submitAction(_ sender: Any) {

}
}

我目前收到2个错误:

  

" AVCapturePhotoOutput"类型的值没有会员" outputSettings"

     

"类型的价值" AVCaptureVideoPreviewLayer"没有会员   " AVLayerVideoGravityResizeAspectFill"

2 个答案:

答案 0 :(得分:1)

你快到了。问题是一些AVFoundation类已被弃用,现在有多种方法可以拍摄照片。以下是您的代码的问题。

  

" AVCapturePhotoOutput"类型的值没有会员" outputSettings"

这是因为实际上AVCapturePhotoOutput没有将任何成员定义为outputSettings。查看AVCapturePhotoOutput

的完整documentation

实际上outputSettingsAVCaptureStillImageOutput的成员,同样在iOS 10.0中已弃用

  

"类型的价值" AVCaptureVideoPreviewLayer"没有会员   " AVLayerVideoGravityResizeAspectFill"

同样的错误,根据你的代码,AVCaptureVideoPreviewLayer没有成员。如果您想设置视频预览图层,请设置如下。

  previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill

正如您所提到的,代码已过时,其使用已弃用AVCaptureStillImageOutput

如果真的想使用AVCapturePhotoOutput,那么您应该按照以下步骤操作。

这些是拍摄照片的步骤。

  • 创建AVCapturePhotoOutput对象。使用其属性确定支持的捕获设置并启用某些功能(例如,是否捕获实时照片)。
  • 创建并配置AVCapturePhotoSettings对象,以选择特定捕获的功能和设置(例如,是否启用图像稳定或闪存)。
  • 通过将照片设置对象传递给capturePhoto(使用:delegate :)方法以及实现AVCapturePhotoCaptureDelegate协议的委托对象来捕获图像。然后,照片捕获输出会呼叫您的代理人在捕获过程中通知您重大事件。

在您的clickCapture方法中使用以下代码,并且忘记在您的课程中确认并实施委派。

let settings = AVCapturePhotoSettings()
let previewPixelType = settings.availablePreviewPhotoPixelFormatTypes.first!
let previewFormat = [kCVPixelBufferPixelFormatTypeKey as String: previewPixelType,
                             kCVPixelBufferWidthKey as String: 160,
                             kCVPixelBufferHeightKey as String: 160,
                             ]
settings.previewPhotoFormat = previewFormat
self.cameraOutput.capturePhoto(with: settings, delegate: self)

如果你想知道从avfoundation捕捉照片的不同方式,请查看我以前的SO answer

此外,Apple文档对How to use AVCapturePhotoOutput

的解释非常明确

答案 1 :(得分:0)

 import AVFoundation
    import Foundation
    @IBOutlet weak var mainimage: UIImageView!
    let captureSession = AVCaptureSession()
    let stillImageOutput = AVCaptureStillImageOutput()
    var previewLayer : AVCaptureVideoPreviewLayer?
    var captureDevice : AVCaptureDevice?
        override func viewDidLoad() {
        super.viewDidLoad()
            captureSession.sessionPreset = AVCaptureSessionPresetHigh

            if let devices = AVCaptureDevice.devices() as? [AVCaptureDevice] {
                // Loop through all the capture devices on this phone
                for device in devices {
                    // Make sure this particular device supports video
                    if (device.hasMediaType(AVMediaTypeVideo)) {
                        // Finally check the position and confirm we've got the back camera
                        if(device.position == AVCaptureDevicePosition.front) {
                            captureDevice = device
                            if captureDevice != nil {
                                print("Capture device found")
                                beginSession()
                            }
                        }
                    }
                }
            }


    }



func beginSession() {

        do {
            try captureSession.addInput(AVCaptureDeviceInput(device: captureDevice))
            stillImageOutput.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]

            if captureSession.canAddOutput(stillImageOutput) {
                captureSession.addOutput(stillImageOutput)
            }

        }
        catch {
            print("error: \(error.localizedDescription)")
        }

        guard let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) else {
            print("no preview layer")
            return
        }

        self.view.layer.addSublayer(previewLayer)
        previewLayer.frame = self.view.layer.frame
        captureSession.startRunning()
        self.view.addSubview(mainimage)

    }

此代码在我的应用中运行