美文网首页
Swift使用AVFoundation实现自定义相机

Swift使用AVFoundation实现自定义相机

作者: Sultan | 来源:发表于2019-08-05 11:14 被阅读0次

    1.什么是AVFoundation

    AV Foundation是在iOS,macOS,watchOS和tvOS上使用基于时间的视听媒体的全功能框架。例如,你可以用它来检查,创建,编辑或重新编码媒体文件。您也可以从设备得到输入流和在实时捕捉回放过程中操控视频。

    2.为什么使用AVFoundation

    通常情况下,如果需要播放视频,可以使用AVKit框架,如果需要拍照或者录制视频则可以使用UIKit中的UIImagePickerController实现,但是如果你想要更多自定义的功能时,例如,以编程方式更改硬件参数,或者操纵实时预览图,就需要使用到AVFoundation来实现,本文介绍如何通过AVFoundation来实现自定义相机。

    3.AVFoundation 相关类

    AVFoundation 框架基于以下几个类实现图像捕捉 ,通过这些类可以访问来自相机设备的原始数据并控制它的组件:
    AVCaptureDevice 是关于相机硬件的接口。它被用于控制硬件特性,诸如镜头的位置、曝光、闪光灯等。
    AVCaptureDeviceInput 提供来自设备的数据。
    AVCaptureOutput 是一个抽象类,描述 capture session 的结果。以下是三种关于静态图片捕捉的具体子类:
    AVCaptureStillImageOutput 用于捕捉静态图片
    AVCaptureMetadataOutput 启用检测人脸和二维码
    AVCaptureVideoOutput 为实时预览图提供原始帧
    AVCaptureSession 管理输入与输出之间的数据流,以及在出现问题时生成运行时错误。
    AVCaptureVideoPreviewLayer 是 CALayer 的子类,可被用于自动显示相机产生的实时图像。它还有几个工具性质的方法,可将 layer 上的坐标转化到设备上。它看起来像输出,但其实不是。另外,它拥有 session (outputs 被 session 所拥有)。

    4.代码实现

      1.初始化AVCaptureSession对象

    let session = AVCaptureSession()
    

      2.现在我们需要一个相机设备输入,我们可以选择后置摄像头或前置摄像头。那么我们必须先遍历所有能提供视频数据的设备(麦克风也属于AVCaptureDevice),并检查position属性:

     let session = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .unspecified)
     let availableCameraDevices = session.devices.compactMap { $0 }
     guard !availableCameraDevices.isEmpty else {throw CameraError.noCamerasAvailable}
     for camera in availableCameraDevices{
             if camera.position == .back {
                  backCamera = camera
              }else if camera.position == .front {
                    frontCamera = camera
              }
       }
    

    3.获得相关的AVCaptureDeviceInput对象,将它设置为session的输入:

     guard let captureSession = self.captureSession else { throw CameraError.captureSessionIsMissing }
            if let backCamera = self.backCamera {
                self.backCameraInput = try AVCaptureDeviceInput(device:backCamera)
                if(captureSession.canAddInput(self.backCameraInput!)){
                    captureSession.addInput(self.backCameraInput!)
                }
                self.currentCameraPosition = .back
            }else if let frontCamera = self.frontCamera{
                self.frontCameraInput = try AVCaptureDeviceInput(device:frontCamera)
                if(captureSession.canAddInput(self.frontCameraInput!)){
                    captureSession.addInput(self.frontCameraInput!)
                }
                self.currentCameraPosition = .front
            }else{throw CameraError.noCamerasAvailable}
    

    4.通过AVCapturePhotoOutput配置照片输出:

     guard let captureSession = self.captureSession else { throw CameraError.captureSessionIsMissing }
     self.photoOutput = AVCapturePhotoOutput()
     self.photoOutput!.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format: [AVVideoCodecKey : AVVideoCodecType.jpeg])], completionHandler: nil)
     if captureSession.canAddOutput(self.photoOutput!) { captureSession.addOutput(self.photoOutput!) }
    

    5.开始运行:

    guard let captureSession = self.captureSession else { throw CameraError.captureSessionIsMissing }
    captureSession.startRunning()
    

    6.显示来自相机的输出:

     guard let captureSession = self.captureSession, captureSession.isRunning else {
                throw CameraError.captureSessionIsMissing
            }
    self.previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    self.previewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
    self.previewLayer?.connection?.videoOrientation = .landscapeRight
    view.layer.insertSublayer(self.previewLayer!, at: 0)
    self.previewLayer?.frame = view.frame
    

    7.实现拍照:

    let settings = AVCapturePhotoSettings()
    settings.flashMode = .off
    self.photoOutput?.capturePhoto(with: settings, delegate: self)
    

    8.通过AVCapturePhotoCaptureDelegate获取图片:

    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
            let imageData = photo.fileDataRepresentation()
    }
    

    9.切换摄像头:

     guard let currentCameraPosition = currentCameraPosition, let captureSession = self.captureSession, captureSession.isRunning else { throw CameraError.captureSessionIsMissing }
            
            captureSession.beginConfiguration()
            
            func switchToFrontCamera() throws {
                
                guard let backCameraInput = self.backCameraInput, captureSession.inputs.contains(backCameraInput),
                    let frontCamera = self.frontCamera else { throw CameraError.invalidOperation }
                
                self.frontCameraInput = try AVCaptureDeviceInput(device: frontCamera)
                
                captureSession.removeInput(backCameraInput)
                
                if captureSession.canAddInput(self.frontCameraInput!) {
                    captureSession.addInput(self.frontCameraInput!)
                    
                    self.currentCameraPosition = .front
                }
                    
                else {
                    throw CameraError.invalidOperation
                }
            }
            
            func switchToRearCamera() throws {
                
                guard let frontCameraInput = self.frontCameraInput, captureSession.inputs.contains(frontCameraInput),
                    let backCamera = self.backCamera else { throw CameraError.invalidOperation }
                
                self.backCameraInput = try AVCaptureDeviceInput(device: backCamera)
                
                captureSession.removeInput(frontCameraInput)
                
                if captureSession.canAddInput(self.backCameraInput!) {
                    captureSession.addInput(self.backCameraInput!)
                    
                    self.currentCameraPosition = .back
                }
                    
                else { throw CameraError.invalidOperation }
            }
            
            switch currentCameraPosition {
            case .front:
                try switchToRearCamera()
            case .back:
                try switchToFrontCamera()
            }
            captureSession.commitConfiguration()
        }
    

    最后需要在info.plist添加访问相机的权限:


    camera.png

    相关文章

      网友评论

          本文标题:Swift使用AVFoundation实现自定义相机

          本文链接:https://www.haomeiwen.com/subject/rbbpdctx.html