美文网首页
AV-自定义相机

AV-自定义相机

作者: 流年易逝_李 | 来源:发表于2018-04-06 10:23 被阅读104次

最近重新梳理AVFoundation框架,遂将之前的oc版本自定义相机重构为swift版本

使用到的类主要有:

AVCaptureDevice 设备类,有许多设置,比如位置Position,聚焦模式FocusMode,曝光模式ExposureMode,闪光灯模式FlashMode。。。。。。

AVCaptureDeviceInput 输入类,用于配置相机,相对应的有AVCaptureOutput输出类

AVCaptureStillImageOutput 照片输出类

AVCaptureSession 会话类,开启和停止相机

AVCaptureVideoPreviewLayer 预览层

文后有链接

使用:

1.相机搭建

1)懒加载各种相机配置

    lazy var captureSession :AVCaptureSession= {

        let captureSessionTmp =AVCaptureSession()

        if captureSessionTmp.canSetSessionPreset(AVCaptureSession.Preset.photo) {

            captureSessionTmp.sessionPreset = AVCaptureSession.Preset.photo

        }

        return captureSessionTmp

    }()

    lazy var captureDeviceInput :AVCaptureDeviceInput? = {

        let captureDevice =getCameraDeviceWithPosition(position:AVCaptureDevice.Position.back)

        do{

            let captureDeviceInputTmp =try AVCaptureDeviceInput.init(device: captureDevice!)

            return captureDeviceInputTmp

        }catch{

            print(error)

        }

        return nil

    }()

    lazy var captureStillImageOutput :AVCaptureStillImageOutput= {

        let captureStillImageOutputTmp =AVCaptureStillImageOutput()

        captureStillImageOutputTmp.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]

        return captureStillImageOutputTmp

    }()

    lazy var captureVideoPreviewLayer :AVCaptureVideoPreviewLayer= {

        let captureVideoPreviewLayerTmp =AVCaptureVideoPreviewLayer.init(session:captureSession)

        return captureVideoPreviewLayerTmp

    }()

2)配置相机

  if captureSession.canAddInput(captureDeviceInput!) {

            captureSession.addInput(captureDeviceInput!)

    }

  if captureSession.canAddOutput(captureStillImageOutput) {

            captureSession.addOutput(captureStillImageOutput)

    }

    let layer =ViewContainer.layer

    layer.masksToBounds=true

    captureVideoPreviewLayer.frame = layer.bounds

    captureVideoPreviewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill

    layer.insertSublayer(captureVideoPreviewLayer, below: focusCursor.layer)

3)添加手势监听,闪光灯按钮状态

    private func addNotificationToCaptureDevice(captureDevice :AVCaptureDevice) {

        changeDeviceProperty{ (captureDevice)in

            captureDevice.isSubjectAreaChangeMonitoringEnabled = true

        }

        NotificationCenter.default.addObserver(self, selector: #selector(areaChange(noti:)), name: Notification.Name.AVCaptureDeviceSubjectAreaDidChange, object: captureDevice)

    }

注:func changeDeviceProperty(propertyChange :PropertyChangeBlock)方法为设备加锁,改变相机属性必须,为自定义方法

private func addGenstureRecognizer() {

        let tap =UITapGestureRecognizer.init(target:self, action:#selector(tapScreen(tapGesture:)))

        ViewContainer.addGestureRecognizer(tap)

}

2.拍照

        let captureConnection =captureStillImageOutput.connection(with:AVMediaType.video)

        captureStillImageOutput.captureStillImageAsynchronously(from: captureConnection!) { (imageDataSampleBuffer, error)in

            if imageDataSampleBuffer !=nil{

                let imageData =AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer!)

                let image =UIImage.init(data: imageData!)

                UIImageWriteToSavedPhotosAlbum(image!,self,#selector(self.imageDidFinishSavingWithError(image:error:contextInfo:)),nil)

            }

        }

注:imageDidFinishSavingWithError(image:error:contextInfo:)为自定义方法,为保存照片回调函数,使用toast提示用户是否成功

3.前置/后置摄像头切换

    @IBAction func toggleButtonClick(_sender:UIButton) {

        let currentDevice =captureDeviceInput?.device

        let currentPosition = currentDevice?.position

        removeNotificationFromCaptureDevice(captureDevice: currentDevice!)

        var toChangeDevice :AVCaptureDevice?

        var toChangePosition =AVCaptureDevice.Position.front

        if currentPosition ==AVCaptureDevice.Position.unspecified|| currentPosition ==AVCaptureDevice.Position.front{

            toChangePosition =AVCaptureDevice.Position.back

        }

        toChangeDevice =getCameraDeviceWithPosition(position: toChangePosition)

        addNotificationToCaptureDevice(captureDevice: toChangeDevice!)

        do{

            let toChangeDeviceInput =tryAVCaptureDeviceInput.init(device: toChangeDevice!)

            captureSession.beginConfiguration()

            captureSession.removeInput(captureDeviceInput!)

            if captureSession.canAddInput(toChangeDeviceInput) {

                captureSession.addInput(toChangeDeviceInput)

                captureDeviceInput= toChangeDeviceInput

            }

            captureSession.commitConfiguration()

            setFlashModeButtonStatus()

        }catch{

            print(error)

        }

    }

注:需要先移除设备监听,再重新添加

4.闪光灯设置

    @IBAction func flashOffClick(_sender:UIButton) {

        setFlashMode(flashMode: AVCaptureDevice.FlashMode.off)

        setFlashModeButtonStatus()

    }

    @IBAction func flashOnClick(_sender:UIButton) {

        setFlashMode(flashMode: AVCaptureDevice.FlashMode.on)

        setFlashModeButtonStatus()

    }

    @IBAction func flashAutoClick(_sender:UIButton) {

        setFlashMode(flashMode: AVCaptureDevice.FlashMode.auto)

        setFlashModeButtonStatus()

    }

    private func setFlashMode(flashMode :AVCaptureDevice.FlashMode) {

        changeDeviceProperty{ (captureDevice)in

            if captureDevice.isFlashModeSupported(flashMode) {

                captureDevice.flashMode= flashMode

            }

        }

    }

5.聚焦模式,曝光模式

private func setFocusMode(focusMode :AVCaptureDevice.FocusMode) {

        changeDeviceProperty{ (captureDevice)in

            if captureDevice.isFocusModeSupported(focusMode) {

                captureDevice.focusMode= focusMode

            }

        }

    }

    private func setExposureMode(exposureMode :AVCaptureDevice.ExposureMode) {

        changeDeviceProperty{ (captureDevice)in

            if captureDevice.isExposureModeSupported(exposureMode) {

                captureDevice.exposureMode= exposureMode

            }

        }

    }

6.点击屏幕时,对焦的动画

    @objc private func tapScreen(tapGesture :UITapGestureRecognizer) {

        let pointTap = tapGesture.location(in:ViewContainer)

        let pointCamera =captureVideoPreviewLayer.captureDevicePointConverted(fromLayerPoint: pointTap)

        setFocusCursorWithPoint(point: pointTap)

        focusWithMode(focusMode:AVCaptureDevice.FocusMode.autoFocus, exposureMode:AVCaptureDevice.ExposureMode.autoExpose, point: pointCamera)

    }

demo 链接:https://github.com/licl19/CustomCameraDemo.git

相关文章

  • AV-自定义相机

    最近重新梳理AVFoundation框架,遂将之前的oc版本自定义相机重构为swift版本 使用到的类主要有: A...

  • AV-自定义相机 录制视频

    1.懒加载各对象 lazy var captureSession : AVCaptureSession = { ...

  • Android自定义相机

    CustomCamera android自定义相机 功能描述: 主要可自定义相机的各类按钮布局 相机拍照缩放功能 ...

  • iOS 之自定义相机

    项目最近要用到相机,但是界面自己设计了,所以系统相机就使不上了。所以研究了下自定义相机。 因为自定义相机的API在...

  • 自己写一个Android照相机应用(2)

    自定义相机 上一篇讲的是调用系统相机拍照然后显示在屏幕上,自定义一个相机就是自己相机的activity。1,首先是...

  • 相机小白自定义Camera实践

    背景 机缘巧合,需要自定义相机,几日折腾下来,对相机开发有了一定认识,做个小结。 既然是自定义相机,在设想里,相机...

  • AV-自定义播放器

    改编自oc版本的播放器 使用到的类主要有: AVPlayerItem: AVPlayer:播放器 AVPlayer...

  • Three.js笔记(八)相机(2)

    自定义控件 回到PerspectiveCamera透视相机上来。注释OrthographicCamera正交相机,...

  • Android CameraX结合LibYUV和GPUImage

    目录 前言 之前使用Camera实现了一个自定义相机滤镜(Android自定义相机滤镜[https://www.j...

  • Android自定义相机,添加水印

    很多app都要求自定义一个相机,类似违章查询拍照,美图相机之类的应用都要求自定义相机,网上的例子大多数我也看过,很...

网友评论

      本文标题:AV-自定义相机

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