📸 iOS Media Capture – features touch-to-record video, slow motion, and photography

Overview

PBJVision

PBJVision

PBJVision is a camera library for iOS that enables easy integration of special capture features and camera interface customizations in your iOS app. Next Level is the Swift counterpart.

Build Status Pod Version GitHub license

Features

  • touch-to-record video capture
  • slow motion capture (120 fps on supported hardware)
  • photo capture
  • customizable user interface and gestural interactions
  • ghosting (onion skinning) of last recorded segment
  • flash/torch support
  • white balance, focus, and exposure adjustment support
  • mirroring support

Capture is also possible without having to use the touch-to-record gesture interaction as the sample project provides.

About

This library was originally created at DIY as a fun means for kids to author video and share their skills. The touch-to-record interaction was pioneered by Vine and Instagram.

Thanks to everyone who has contributed and helped make this a fun project and community.

Quick Start

PBJVision is available and recommended for installation using the dependency manager CocoaPods.

To integrate, just add the following line to your Podfile:

pod 'PBJVision'

Usage

Import the header.

#import "PBJVision.h"

Setup the camera preview using [[PBJVision sharedInstance] previewLayer].

    // preview and AV layer
    _previewView = [[UIView alloc] initWithFrame:CGRectZero];
    _previewView.backgroundColor = [UIColor blackColor];
    CGRect previewFrame = CGRectMake(0, 60.0f, CGRectGetWidth(self.view.frame), CGRectGetWidth(self.view.frame));
    _previewView.frame = previewFrame;
    _previewLayer = [[PBJVision sharedInstance] previewLayer];
    _previewLayer.frame = _previewView.bounds;
    _previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [_previewView.layer addSublayer:_previewLayer];

If your view controller is managed by a Storyboard, keep the previewLayer updated for device sizes

- (void)viewDidLayoutSubviews
{
    _previewLayer.frame = _previewView.bounds;
}

Setup and configure the PBJVision controller, then start the camera preview.

- (void)_setup
{
    _longPressGestureRecognizer.enabled = YES;

    PBJVision *vision = [PBJVision sharedInstance];
    vision.delegate = self;
    vision.cameraMode = PBJCameraModeVideo;
    vision.cameraOrientation = PBJCameraOrientationPortrait;
    vision.focusMode = PBJFocusModeContinuousAutoFocus;
    vision.outputFormat = PBJOutputFormatSquare;

    [vision startPreview];
}

Start/pause/resume recording.

- (void)_handleLongPressGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
    switch (gestureRecognizer.state) {
      case UIGestureRecognizerStateBegan:
        {
            if (!_recording)
                [[PBJVision sharedInstance] startVideoCapture];
            else
                [[PBJVision sharedInstance] resumeVideoCapture];
            break;
        }
      case UIGestureRecognizerStateEnded:
      case UIGestureRecognizerStateCancelled:
      case UIGestureRecognizerStateFailed:
        {
            [[PBJVision sharedInstance] pauseVideoCapture];
            break;
        }
      default:
        break;
    }
}

End recording.

    [[PBJVision sharedInstance] endVideoCapture];

Handle the final video output or error accordingly.

- (void)vision:(PBJVision *)vision capturedVideo:(NSDictionary *)videoDict error:(NSError *)error
{   
    if (error && [error.domain isEqual:PBJVisionErrorDomain] && error.code == PBJVisionErrorCancelled) {
        NSLog(@"recording session cancelled");
        return;
    } else if (error) {
        NSLog(@"encounted an error in video capture (%@)", error);
        return;
    }

    _currentVideo = videoDict;
    
    NSString *videoPath = [_currentVideo  objectForKey:PBJVisionVideoPathKey];
    [_assetLibrary writeVideoAtPathToSavedPhotosAlbum:[NSURL URLWithString:videoPath] completionBlock:^(NSURL *assetURL, NSError *error1) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Video Saved!" message: @"Saved to the camera roll."
                                                       delegate:self
                                              cancelButtonTitle:nil
                                              otherButtonTitles:@"OK", nil];
        [alert show];
    }];
}

To specify an automatic end capture maximum duration, set the following property on the 'PBJVision' controller.

    [[PBJVision sharedInstance] setMaximumCaptureDuration:CMTimeMakeWithSeconds(5, 600)]; // ~ 5 seconds

To adjust the video quality and compression bit rate, modify the following properties on the PBJVision controller.

    @property (nonatomic, copy) NSString *captureSessionPreset;

    @property (nonatomic) CGFloat videoBitRate;
    @property (nonatomic) NSInteger audioBitRate;
    @property (nonatomic) NSDictionary *additionalCompressionProperties;

Community

Contributions and discussions are welcome!

Project

Related Projects

Resources

License

PBJVision is available under the MIT license, see the LICENSE file for more information.

Comments
  • Feature request: capture a frame from the live preview buffer

    Feature request: capture a frame from the live preview buffer

    Hey @piemonte I have a use case where i need to grab a frame from the live preview buffer in order to do some animation that doesn't require changing PBJVision modes. For example, lets say the user is in PBJCameraModePhoto and the user performs a gesture that requires immediate feedback/animation, in order to make animation smooth i basically need a freeze frame as a UIImage of the preview to make the animation fluid.

    I hacked my way into the project to get this working on my own fork, but i was wondering if you had any suggestions on how to clean this up, or if there is an easier route to take. https://github.com/rromanchuk/PBJVision/pull/3/files

    feature 
    opened by rromanchuk 23
  • slow motion - can not change activeVideoMinFrameDuration, activeVideoMaxFrameDuration and videoFrameRate since lookForConfiguration always returns 0

    slow motion - can not change activeVideoMinFrameDuration, activeVideoMaxFrameDuration and videoFrameRate since lookForConfiguration always returns 0

    I tried changing the fps to 60, permissible by iPhone 5 and I see that I cannot set the below since lockForConfiguration always returns 0.

    Therefore, the below attributes cannot be set - _currentDevice.activeVideoMinFrameDuration = fps; _currentDevice.activeVideoMaxFrameDuration = fps; _videoFrameRate = videoFrameRate;

    Calling lockForConfiguration, is not returning any errors. It always returns 0 or no for whether the lock was acquired or not. I don't know why it won't grant the lock. [device lockForConfiguration:nil];

    bug 
    opened by manans25 17
  • Change camera direction on orientation change

    Change camera direction on orientation change

    I manually set the camera orientation to begin with:

    [vision setCameraOrientation:PBJCameraOrientationPortrait];
    

    But I would like the orientation to change if the device rotates (regardless wether the rotation lock is active or not on the device)

    I tried getting orientation changes (which only works if the orientation lock is off):

    - (void)rotateCameraToInterfaceOrientation:(UIDeviceOrientation)orientation {
        switch (orientation) {
            case UIDeviceOrientationPortrait:
                [[PBJVision sharedInstance] setCameraOrientation:PBJCameraOrientationPortrait];
                break;
            case UIDeviceOrientationPortraitUpsideDown:
                [[PBJVision sharedInstance] setCameraOrientation:PBJCameraOrientationPortraitUpsideDown];
                break;
            case UIDeviceOrientationLandscapeLeft:
                [[PBJVision sharedInstance] setCameraOrientation:PBJCameraOrientationLandscapeLeft];
                break;
            case UIDeviceOrientationLandscapeRight:
                [[PBJVision sharedInstance] setCameraOrientation:PBJCameraOrientationLandscapeRight];
                break;
            default:
                [[PBJVision sharedInstance] setCameraOrientation:PBJCameraOrientationPortrait];
                break;
        }
    }
    

    But the preview layer rotates when I change the orientation. What I want is the metadata to update the orientation value, so when you put the image in the camera roll it ends up in the correct direction.

    How do I achieve this?

    feature 
    opened by n1mda 16
  • Capture front camera photo is vertically flipped after being saved

    Capture front camera photo is vertically flipped after being saved

    I'm trying to write a photo to the photo album and my front facing photos are always vertically flipped (until it is slightly zoomed). A GIF to help:

    img_7551

    I've tried saving the UIImage with the orientation set to image.imageOrientation like so:

    [assetsLibrary writeImageToSavedPhotosAlbum:currentImage.CGImage orientation:(ALAssetOrientation)currentImage.imageOrientation completionBlock:nil];
    

    I've also tried to save the PBJVisionPhotoJPEGKey data with the PBJVisionPhotoMetadataKey dictionary and am getting the same results:

    NSDictionary *currentMediaMetadata = currentMediaDict[PBJVisionPhotoMetadataKey];
    NSData *currentMediaData = currentMediaDict[PBJVisionPhotoJPEGKey];
    
    [assetsLibrary writeImageDataToSavedPhotosAlbum:currentMediaData metadata: currentMediaMetadata completionBlock:nil];
    

    vision.cameraOrientation is set to 0 and vision.mirroringMode is set to PBJMirroringAuto at the time if that helps.

    Any help would be appreciated!

    bug 
    opened by dannyhertz 14
  • asset writer is in an unknown state, wasn't recording

    asset writer is in an unknown state, wasn't recording

    The above error gets called in endCaptureVideo if the audio session is manually set to ambient (i do this to replay the video immediately after capture via: [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error:nil] )

    opened by ghost 14
  • add support for focus and exposure locking with delegate methods

    add support for focus and exposure locking with delegate methods

    So I was in the process of implementing subjectAreaChangeMonitoringEnabled and found the _setFocusLocked method - but I can't figure out where/when it's called?

    Also, the way it's set - wouldn't you want to set subjectAreaChangeMonitoringEnabled to the opposite value of focusLocked? Or am I completely misunderstanding what focusLocked means?

    feature 
    opened by jai 13
  • Impossible to use captureSessionPreset different from AVCaptureSessionPresetMedium

    Impossible to use captureSessionPreset different from AVCaptureSessionPresetMedium

    Once I set captureSessionPreset with a value different from AVCaptureSessionPresetMedium, it is impossible to record.

    I made many tests, and even when changing the default value of _captureSessionPreset in PBJVision::init, the problem remains.

    @piemonte, would you have an idea of a quickfix I could use for this ?

    Thanks by advance

    opened by takusen 12
  • Fixed Negative capturedVideoSeconds Bug

    Fixed Negative capturedVideoSeconds Bug

    In PBJVision.m, I added an if statement to handle situations where the method capturedVideoSeconds was returning a negative value. This situation had prevented my multi-view application from recognizing that new video was being captured on a return visit to a view that recorded video.

    opened by eliotw1 12
  • add better support for single core devices

    add better support for single core devices

    Hey - so a bit new to video on iOS - I'm trying to update a UIProgressView when recording but finding that it's stealing the main thread from the capture session. The shorter the update interval of the UIProgressView (either using NSTimer or GCD), the choppier the video is.

    Is AVCaptureSession not thread safe by default, or just this particular implementation? I notice other apps like Vine have no problem updating the UI while video is recording so either they're using something like CADisplayLink to update the UI or there's a way to record video off the main thread?

    opened by jai 12
  • unable to deal with very short record interval

    unable to deal with very short record interval

    In the sample project when you press for a very short time (more like a tap than a long press) the capturedVideoSeconds is calculated incorrectly.

    Part of the fix would be to not initiate _audioTimestamp and _videoTimestamp to kCMTimeZero but to the current value of _startTimestamp. This however does not fix the problem entirely. It seems that for a very short record time _videoTimestamp is not updated correctly but set to nan.

    Thanks for sharing your amazing camera engine!

    bug 
    opened by nilsymbol 12
  • captureOutput:didOuputSampleBuffer:fromConnection: not always being called on 64-bit devices

    captureOutput:didOuputSampleBuffer:fromConnection: not always being called on 64-bit devices

    While running on a 64-bit device, we've discovered that -[PBJVision captureOutput:didOuputSampleBuffer:fromConnection:] is not always called.

    In our app, by default, we always have the camera mode set to PBJCameraModePhoto. When the user goes to record a video, we change the camera mode to PBJCameraModeVideo. When the user is done recording a video, we reset the camera mode back to PBJCameraModePhoto to restore its default state. If the user quickly goes to record a video again, then the delegate method mentioned previously (-[PBJVision captureOutput:didOuputSampleBuffer:fromConnection:]) is not called.

    The way we came across this was by discovering that in this specific scenario, the PBJMediaWriter's _assetWriter had a status of AVAssetWriterStatusUnknown inside the method -[PBJMediaWriter finishWritingWithCompletionHandler:]. Further, we realized that the _assetWriter did not have any inputs. We realized both of these issues were because the method that sets up the inputs (-[PBJVision captureOutput:didOuputSampleBuffer:fromConnection:]) was not being called the second time recording a video.

    We noticed that this issue does _not_ occur when delaying several seconds between stopping recording, and beginning to record video again; and does _not_ occur on 32-bit devices. We verified this by testing on an iPhone 5, iPod Touch (5th gen), iPhone 5S, and iPad Air.

    bug 
    opened by jrmsklar 11
Releases(0.5.1)
  • v0.2.0(Apr 24, 2014)

    This release includes some needed bug fixes and a few minor features:

    • adjust focus only on point of interest
    • adjust exposure only point of interest
    • ability to end capture after a particular recorded duration, property maximumCaptureDuration
    • ability to reset the current recording session via "cancelVideoCapture"

    Please remember to update previous usage of focusAtPoint to:

    - (void)focusExposeAndAdjustWhiteBalanceAtAdjustedPoint:(CGPoint)adjustedPoint;
    
    Source code(tar.gz)
    Source code(zip)
  • v0.1.8(Mar 26, 2014)

    A couple delegate method names have changed with v0.1.8, please ensure you've updated:

    https://github.com/piemonte/PBJVision/pull/103

    https://github.com/piemonte/PBJVision/pull/105

    Source code(tar.gz)
    Source code(zip)
  • v0.1.3(Nov 7, 2013)

    many thanks to all the community contributions and suggestions!

    this update exposes some parameters to adjust compression and video quality.

    Source code(tar.gz)
    Source code(zip)
  • v0.1.2(Nov 4, 2013)

  • v0.1.1(Nov 4, 2013)

  • v0.1.0(Nov 4, 2013)

Owner
patrick piemonte
patrick piemonte
NextLevel is a Swift camera system designed for easy integration, customized media capture, and image streaming in iOS

NextLevel is a Swift camera system designed for easy integration, customized media capture, and image streaming in iOS. Integration can optionally leverage AVFoundation or ARKit.

NextLevel 2k Sep 8, 2022
IPadLiDARExperiment - Simple experiment to capture Depth data from the iPad Pro's LiDAR

iPad LiDAR Experiment Simple experiment to capture and display Depth data from t

Fabio 16 Jul 25, 2022
A camera designed in Swift for easily integrating CoreML models - as well as image streaming, QR/Barcode detection, and many other features

Would you like to use a fully-functional camera in an iOS application in seconds? Would you like to do CoreML image recognition in just a few more sec

David Okun 858 Sep 21, 2022
Video and photo camera for iOS

Features: Description Records video ?? takes photos ?? Flash on/off ⚡ Front / Back camera ↕️ Hold to record video ✊ Tap to take photo ?? Tap to focus

André J 186 Sep 12, 2022
A simple, customizable camera control - video recorder for iOS.

LLSimpleCamera: A simple customizable camera - video recorder control LLSimpleCamera is a library for creating a customized camera - video recorder sc

Ömer Faruk Gül 1.2k Sep 27, 2022
Easily take a photo or video or choose from library

FDTake Easily take a photo or video or choose from library ?? Author's tip jar: https://amazon.com/hz/wishlist/ls/EE78A23EEGQB Usage To run the exampl

William Entriken 320 Aug 18, 2022
1D and 2D barcodes reader and generators for iOS 8 with delightful controls. Now Swift.

RSBarcodes, now in Swift. RSBarcodes allows you to read 1D and 2D barcodes using the metadata scanning capabilities introduced with iOS 7 and generate

R0CKSTAR 684 Aug 15, 2022
BarcodeScanner is a simple and beautiful wrapper around the camera with barcode capturing functionality and a great user experience.

Description BarcodeScanner is a simple and beautiful wrapper around the camera with barcode capturing functionality and a great user experience. Barco

HyperRedink 1.6k Sep 22, 2022
Custom camera with AVFoundation. Beautiful, light and easy to integrate with iOS projects.

?? Warning This repository is DEPRECATED and not maintained anymore. Custom camera with AVFoundation. Beautiful, light and easy to integrate with iOS

Tudo Gostoso Internet 1.4k Aug 31, 2022
A fully customisable and modern camera implementation for iOS made with AVFoundation.

Features Extremely simple and easy to use Controls autofocus & exposure Customizable interface Code-made UI assets that do not lose resolution quality

Gabriel Alvarado 1.3k Sep 13, 2022
Fasttt and easy camera framework for iOS with customizable filters

FastttCamera is a wrapper around AVFoundation that allows you to build your own powerful custom camera app without all the headaches of using AVFounda

IFTTT 1.8k Sep 22, 2022
An iOS framework that uses the front camera, detects your face and takes a selfie.

TakeASelfie An iOS framework that uses the front camera, detects your face and takes a selfie. This api opens the front camera and draws an green oval

Abdullah Selek 36 Aug 18, 2022
ALCameraViewController - A camera view controller with custom image picker and image cropping.

ALCameraViewController A camera view controller with custom image picker and image cropping. Features Front facing and rear facing camera Simple and c

Alex Littlejohn 2k Sep 18, 2022
Instagram-like photo browser and a camera feature with a few line of code in Swift.

Fusuma is a Swift library that provides an Instagram-like photo browser with a camera feature using only a few lines of code.

Yuta Akizuki 2.4k Sep 23, 2022
This plugin defines a global navigator.camera object, which provides an API for taking pictures and for choosing images from the system's image library.

title description Camera Take pictures with the device camera. AppVeyor Travis CI cordova-plugin-camera This plugin defines a global navigator.camera

null 0 Nov 2, 2021
A PSP emulator for Android, Windows, Mac and Linux, written in C++.

PPSSPP - a fast and portable PSP emulator Created by Henrik Rydgård Additional code by many contributors, see the Credits screen Originally released u

Henrik Rydgård 7.8k Sep 23, 2022
Library for iOS Camera API. CameraKit helps you add reliable camera to your app quickly.

CameraKit helps you add reliable camera to your app quickly. Our open source camera platform provides consistent capture results, service that scales, and endless camera possibilities.

CameraKit 627 Sep 17, 2022
Horizon SDK for iOS

Horizon SDK for iOS Horizon SDK is a state of the art real-time video recording / photo shooting iOS library. Some of the features of Horizon SDK incl

Horizon Video Technologies 171 Aug 15, 2022
A light weight & simple & easy camera for iOS by Swift.

DKCamera Description A light weight & simple & easy camera for iOS by Swift. It uses CoreMotion framework to detect device orientation, so the screen-

Bannings 86 Aug 18, 2022