Skip to content

shakurocom/VideoCamera

Repository files navigation

Shakuro VideoCamera

VideoCamera

Version Platform License MIT

Wrapper around AVFoundation camera. Works with several data outputs, such as metadata, video data, still image capture.

Requirements

  • iOS 11.0+
  • Xcode 11.0+
  • Swift 5.0+

Installation

CocoaPods

To integrate VideoCamera into your Xcode project with CocoaPods, specify it in your Podfile:

pod 'Shakuro.VideoCamera'

Then, run the following command:

$ pod install

Manually

If you prefer not to use CocoaPods, you can integrate Shakuro.VideoCamera simply by copying it to your project.

Usage

Each VideoCamera must be provided with VideoCameraConfiguration structure. Go though it's extensive array of settings. For a simple setup of back-facing camera, that provides only video data preview without ability to get still images:

private var camera: VideoCamera?

override func viewDidLoad() {
    super.viewDidLoad()
    
    // ...
    
    var cameraConfig = VideoCameraConfiguration()
    cameraConfig.cameraDelegate = self
    cameraConfig.captureSessionPreset = .high
    cameraConfig.capturePhotoEnabled = false
    cameraConfig.videoFeedDelegate = self
    cameraConfig.simulatedImage = UIImage(named: "card_backside.png")?.cgImage
    let videoCamera = VideoCameraFactory.createCamera(configuration: cameraConfig)
    camera = videoCamera
    
    // ...
}

To display camera preview, one of the simplest ways is to prepare container view in storyboard and than add camera's preview to it:

@IBOutlet private var cameraPreviewContainerView: UIView!

override func viewDidLoad() {
    super.viewDidLoad()
    
    // setup camera (see above)

    let preview = videoCamera.previewView
    preview.translatesAutoresizingMaskIntoConstraints = true
    preview.autoresizingMask = [UIViewAutoresizing.flexibleHeight, UIViewAutoresizing.flexibleWidth]
    preview.frame = cameraPreviewContainerView.bounds
    cameraPreviewContainerView.insertSubview(preview, at: 0)
    
    // ...
}

Changes of camera's properties are observed via delegate:

extension MyViewController: VideoCameraDelegate {

    func videoCamera(_ videoCamera: VideoCamera, error: Error) {
        // display/process error
    }

    func videoCameraInitialized(_ videoCamera: VideoCamera, errors: [VideoCameraError]) {
        // update UI - such as flash button (with actual state of camera)
    }

    func videoCamera(_ videoCamera: VideoCamera, flashModeForPhotoDidChanged newValue: AVCaptureDevice.FlashMode) {
        // update flash button
    }
    
    // ... other delegate functions
    
}

Have a look at the VideoCamera_Example

License

Shakuro.VideoCamera is released under the MIT license. See LICENSE for details.

Give it a try and reach us

Star this tool if you like it, it will help us grow and add new useful things. Feel free to reach out and hire our team to develop a mobile or web project for you.