Skip to content

Commit

Permalink
Merge pull request #13 from CameraKit/develop
Browse files Browse the repository at this point in the history
Release v1.2.0
  • Loading branch information
Adrian Mateoaea authored Feb 27, 2019
2 parents 4d3722f + b785bbc commit 5b37efc
Show file tree
Hide file tree
Showing 15 changed files with 260 additions and 102 deletions.
88 changes: 87 additions & 1 deletion .repo/gh-readme-expensify-logo.svg
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 28 additions & 28 deletions CameraKit/CameraKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
objects = {

/* Begin PBXBuildFile section */
3A47096321F21173004F4B8D /* CKGridView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A47096221F21173004F4B8D /* CKGridView.swift */; };
3A4E8BCF21F8E8D600CAC6F2 /* CKUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A4E8BCE21F8E8D600CAC6F2 /* CKUtils.swift */; };
3A76E33D21E549B800C8461D /* CKSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A76E33C21E549B800C8461D /* CKSession.swift */; };
3A76E33F21E54AC000C8461D /* CKPhotoSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A76E33E21E54AC000C8461D /* CKPhotoSession.swift */; };
3AC2453521E62AD600785FB2 /* CKVideoSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AC2453421E62AD600785FB2 /* CKVideoSession.swift */; };
3A47096321F21173004F4B8D /* CKFGridView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A47096221F21173004F4B8D /* CKFGridView.swift */; };
3A4E8BCF21F8E8D600CAC6F2 /* CKFUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A4E8BCE21F8E8D600CAC6F2 /* CKFUtils.swift */; };
3A76E33D21E549B800C8461D /* CKFSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A76E33C21E549B800C8461D /* CKFSession.swift */; };
3A76E33F21E54AC000C8461D /* CKFPhotoSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A76E33E21E54AC000C8461D /* CKFPhotoSession.swift */; };
3AC2453521E62AD600785FB2 /* CKFVideoSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AC2453421E62AD600785FB2 /* CKFVideoSession.swift */; };
3AD9372B21E53B97006A8C86 /* CameraKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3AD9372121E53B97006A8C86 /* CameraKit.framework */; };
3AD9373021E53B97006A8C86 /* CameraKitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AD9372F21E53B97006A8C86 /* CameraKitTests.swift */; };
3AD9373221E53B97006A8C86 /* CameraKit.h in Headers */ = {isa = PBXBuildFile; fileRef = 3AD9372421E53B97006A8C86 /* CameraKit.h */; settings = {ATTRIBUTES = (Public, ); }; };
3AD9375F21E53F81006A8C86 /* CKPreviewView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AD9375C21E53C61006A8C86 /* CKPreviewView.swift */; };
3ADDEE7921E75A4E00EEF063 /* CKError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3ADDEE7821E75A4E00EEF063 /* CKError.swift */; };
3AD9375F21E53F81006A8C86 /* CKFPreviewView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AD9375C21E53C61006A8C86 /* CKFPreviewView.swift */; };
3ADDEE7921E75A4E00EEF063 /* CKFError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3ADDEE7821E75A4E00EEF063 /* CKFError.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -30,19 +30,19 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
3A47096221F21173004F4B8D /* CKGridView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CKGridView.swift; sourceTree = "<group>"; };
3A4E8BCE21F8E8D600CAC6F2 /* CKUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CKUtils.swift; sourceTree = "<group>"; };
3A76E33C21E549B800C8461D /* CKSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CKSession.swift; sourceTree = "<group>"; };
3A76E33E21E54AC000C8461D /* CKPhotoSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CKPhotoSession.swift; sourceTree = "<group>"; };
3AC2453421E62AD600785FB2 /* CKVideoSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CKVideoSession.swift; sourceTree = "<group>"; };
3A47096221F21173004F4B8D /* CKFGridView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CKFGridView.swift; sourceTree = "<group>"; };
3A4E8BCE21F8E8D600CAC6F2 /* CKFUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CKFUtils.swift; sourceTree = "<group>"; };
3A76E33C21E549B800C8461D /* CKFSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CKFSession.swift; sourceTree = "<group>"; };
3A76E33E21E54AC000C8461D /* CKFPhotoSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CKFPhotoSession.swift; sourceTree = "<group>"; };
3AC2453421E62AD600785FB2 /* CKFVideoSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CKFVideoSession.swift; sourceTree = "<group>"; };
3AD9372121E53B97006A8C86 /* CameraKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = CameraKit.framework; sourceTree = BUILT_PRODUCTS_DIR; };
3AD9372421E53B97006A8C86 /* CameraKit.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CameraKit.h; sourceTree = "<group>"; };
3AD9372521E53B97006A8C86 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3AD9372A21E53B97006A8C86 /* CameraKitTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CameraKitTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
3AD9372F21E53B97006A8C86 /* CameraKitTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CameraKitTests.swift; sourceTree = "<group>"; };
3AD9373121E53B97006A8C86 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3AD9375C21E53C61006A8C86 /* CKPreviewView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CKPreviewView.swift; sourceTree = "<group>"; };
3ADDEE7821E75A4E00EEF063 /* CKError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CKError.swift; sourceTree = "<group>"; };
3AD9375C21E53C61006A8C86 /* CKFPreviewView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CKFPreviewView.swift; sourceTree = "<group>"; };
3ADDEE7821E75A4E00EEF063 /* CKFError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CKFError.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -85,13 +85,13 @@
3AD9372321E53B97006A8C86 /* CameraKit */ = {
isa = PBXGroup;
children = (
3AD9375C21E53C61006A8C86 /* CKPreviewView.swift */,
3A47096221F21173004F4B8D /* CKGridView.swift */,
3A76E33C21E549B800C8461D /* CKSession.swift */,
3A76E33E21E54AC000C8461D /* CKPhotoSession.swift */,
3AC2453421E62AD600785FB2 /* CKVideoSession.swift */,
3ADDEE7821E75A4E00EEF063 /* CKError.swift */,
3A4E8BCE21F8E8D600CAC6F2 /* CKUtils.swift */,
3AD9375C21E53C61006A8C86 /* CKFPreviewView.swift */,
3A47096221F21173004F4B8D /* CKFGridView.swift */,
3A76E33C21E549B800C8461D /* CKFSession.swift */,
3A76E33E21E54AC000C8461D /* CKFPhotoSession.swift */,
3AC2453421E62AD600785FB2 /* CKFVideoSession.swift */,
3ADDEE7821E75A4E00EEF063 /* CKFError.swift */,
3A4E8BCE21F8E8D600CAC6F2 /* CKFUtils.swift */,
3AD9372421E53B97006A8C86 /* CameraKit.h */,
3AD9372521E53B97006A8C86 /* Info.plist */,
);
Expand Down Expand Up @@ -216,13 +216,13 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
3AC2453521E62AD600785FB2 /* CKVideoSession.swift in Sources */,
3ADDEE7921E75A4E00EEF063 /* CKError.swift in Sources */,
3A76E33D21E549B800C8461D /* CKSession.swift in Sources */,
3AD9375F21E53F81006A8C86 /* CKPreviewView.swift in Sources */,
3A47096321F21173004F4B8D /* CKGridView.swift in Sources */,
3A4E8BCF21F8E8D600CAC6F2 /* CKUtils.swift in Sources */,
3A76E33F21E54AC000C8461D /* CKPhotoSession.swift in Sources */,
3AC2453521E62AD600785FB2 /* CKFVideoSession.swift in Sources */,
3ADDEE7921E75A4E00EEF063 /* CKFError.swift in Sources */,
3A76E33D21E549B800C8461D /* CKFSession.swift in Sources */,
3AD9375F21E53F81006A8C86 /* CKFPreviewView.swift in Sources */,
3A47096321F21173004F4B8D /* CKFGridView.swift in Sources */,
3A4E8BCF21F8E8D600CAC6F2 /* CKFUtils.swift in Sources */,
3A76E33F21E54AC000C8461D /* CKFPhotoSession.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Foundation

public enum CKError: Error {
public enum CKFError: Error {
case captureDeviceNotFound
case error(String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import UIKit

@objc public class CKGridView: UIView {
@objc public class CKFGridView: UIView {

@objc public var color: UIColor = UIColor.white.withAlphaComponent(0.5) {
didSet {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import UIKit
import AVFoundation

extension CKSession.FlashMode {
extension CKFSession.FlashMode {

var captureFlashMode: AVCaptureDevice.FlashMode {
switch self {
Expand All @@ -20,7 +20,7 @@ extension CKSession.FlashMode {
}
}

@objc public class CKPhotoSession: CKSession, AVCapturePhotoCaptureDelegate, AVCaptureMetadataOutputObjectsDelegate {
@objc public class CKFPhotoSession: CKFSession, AVCapturePhotoCaptureDelegate, AVCaptureMetadataOutputObjectsDelegate {

@objc public enum CameraDetection: UInt {
case none, faces
Expand All @@ -29,7 +29,7 @@ extension CKSession.FlashMode {
@objc public var cameraPosition = CameraPosition.back {
didSet {
do {
let deviceInput = try CKSession.captureDeviceInput(type: self.cameraPosition.deviceType)
let deviceInput = try CKFSession.captureDeviceInput(type: self.cameraPosition.deviceType)
self.captureDeviceInput = deviceInput
} catch let error {
print(error.localizedDescription)
Expand Down Expand Up @@ -62,7 +62,7 @@ extension CKSession.FlashMode {
}
}

@objc public var flashMode = CKSession.FlashMode.off
@objc public var flashMode = CKFSession.FlashMode.off

var captureDeviceInput: AVCaptureDeviceInput? {
didSet {
Expand Down Expand Up @@ -109,6 +109,14 @@ extension CKSession.FlashMode {
let settings = AVCapturePhotoSettings()
settings.flashMode = self.flashMode.captureFlashMode

if let connection = self.photoOutput.connection(with: .video) {
if self.resolution.width > 0, self.resolution.height > 0 {
connection.videoOrientation = .portrait
} else {
connection.videoOrientation = UIDevice.current.orientation.videoOrientation
}
}

self.photoOutput.capturePhoto(with: settings, delegate: self)
}

Expand Down Expand Up @@ -147,7 +155,7 @@ extension CKSession.FlashMode {

if
self.resolution.width > 0, self.resolution.height > 0,
let format = CKSession.deviceInputFormat(input: deviceInput, width: Int(self.resolution.width), height: Int(self.resolution.height))
let format = CKFSession.deviceInputFormat(input: deviceInput, width: Int(self.resolution.width), height: Int(self.resolution.height))
{
deviceInput.device.activeFormat = format
} else {
Expand Down Expand Up @@ -187,7 +195,7 @@ extension CKSession.FlashMode {
}

guard let data = photo.fileDataRepresentation() else {
self.errorCallback(CKError.error("Cannot get photo file data representation"))
self.errorCallback(CKFError.error("Cannot get photo file data representation"))
return
}

Expand All @@ -209,7 +217,7 @@ extension CKSession.FlashMode {
let photoSampleBuffer = photoSampleBuffer, let previewPhotoSampleBuffer = previewPhotoSampleBuffer,
let data = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoSampleBuffer, previewPhotoSampleBuffer: previewPhotoSampleBuffer) else
{
self.errorCallback(CKError.error("Cannot get photo file data representation"))
self.errorCallback(CKFError.error("Cannot get photo file data representation"))
return
}

Expand All @@ -218,13 +226,13 @@ extension CKSession.FlashMode {

private func processPhotoData(data: Data, resolvedSettings: AVCaptureResolvedPhotoSettings) {
guard let image = UIImage(data: data) else {
self.errorCallback(CKError.error("Cannot get photo"))
self.errorCallback(CKFError.error("Cannot get photo"))
return
}

if
self.resolution.width > 0, self.resolution.height > 0,
let transformedImage = CKUtils.cropAndScale(image, width: Int(self.resolution.width), height: Int(self.resolution.height))
let transformedImage = CKUtils.cropAndScale(image, width: Int(self.resolution.width), height: Int(self.resolution.height), orientation: UIDevice.current.orientation, mirrored: self.cameraPosition == .front)
{
self.captureCallback(transformedImage, resolvedSettings)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import UIKit
import AVFoundation

@objc public class CKPreviewView: UIView {
@objc public class CKFPreviewView: UIView {

private var lastScale: CGFloat = 1.0

Expand All @@ -23,7 +23,7 @@ import AVFoundation
}
}

@objc public var session: CKSession? {
@objc public var session: CKFSession? {
didSet {
oldValue?.stop()

Expand All @@ -36,7 +36,7 @@ import AVFoundation
}
}

@objc private(set) public var gridView: CKGridView? {
@objc private(set) public var gridView: CKFGridView? {
didSet {
oldValue?.removeFromSuperview()

Expand All @@ -53,13 +53,21 @@ import AVFoundation
}

if self.showGrid {
self.gridView = CKGridView(frame: self.bounds)
self.gridView = CKFGridView(frame: self.bounds)
} else {
self.gridView = nil
}
}
}

@objc public var autorotate: Bool = false {
didSet {
if !self.autorotate {
self.previewLayer?.connection?.videoOrientation = .portrait
}
}
}

@objc public override init(frame: CGRect) {
super.init(frame: frame)
self.setupView()
Expand Down Expand Up @@ -102,5 +110,9 @@ import AVFoundation
super.layoutSubviews()
self.previewLayer?.frame = self.bounds
self.gridView?.frame = self.bounds

if self.autorotate {
self.previewLayer?.connection?.videoOrientation = UIDevice.current.orientation.videoOrientation
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,23 @@

import AVFoundation

private extension CKSession.DeviceType {
public extension UIDeviceOrientation {

var videoOrientation: AVCaptureVideoOrientation {
switch UIDevice.current.orientation {
case .portraitUpsideDown:
return .portraitUpsideDown
case .landscapeLeft:
return .landscapeRight
case .landscapeRight:
return .landscapeLeft
default:
return .portrait
}
}
}

private extension CKFSession.DeviceType {

var captureDeviceType: AVCaptureDevice.DeviceType {
switch self {
Expand Down Expand Up @@ -40,8 +56,8 @@ private extension CKSession.DeviceType {
}
}

extension CKSession.CameraPosition {
var deviceType: CKSession.DeviceType {
extension CKFSession.CameraPosition {
var deviceType: CKFSession.DeviceType {
switch self {
case .back:
return .backCamera
Expand All @@ -51,11 +67,11 @@ extension CKSession.CameraPosition {
}
}

@objc public protocol CKSessionDelegate: class {
@objc func didChangeValue(session: CKSession, value: Any, key: String)
@objc public protocol CKFSessionDelegate: class {
@objc func didChangeValue(session: CKFSession, value: Any, key: String)
}

@objc public class CKSession: NSObject {
@objc public class CKFSession: NSObject {

@objc public enum DeviceType: UInt {
case frontCamera, backCamera, microphone
Expand All @@ -76,7 +92,7 @@ extension CKSession.CameraPosition {

@objc public var zoom = 1.0

@objc public weak var delegate: CKSessionDelegate?
@objc public weak var delegate: CKFSessionDelegate?

@objc override init() {
self.session = AVCaptureSession()
Expand Down Expand Up @@ -105,7 +121,7 @@ extension CKSession.CameraPosition {
position: type.capturePosition)

guard let captureDevice = captureDevices.devices.first else {
throw CKError.captureDeviceNotFound
throw CKFError.captureDeviceNotFound
}

return try AVCaptureDeviceInput(device: captureDevice)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,37 @@

import UIKit

private extension UIDeviceOrientation {
var imageOrientation: UIImageOrientation {
switch self {
case .portraitUpsideDown:
return .left
case .landscapeLeft:
return .up
case .landscapeRight:
return .down
default:
return .right
}
}

var imageOrientationMirrored: UIImageOrientation {
switch self {
case .portraitUpsideDown:
return .left
case .landscapeLeft:
return .down
case .landscapeRight:
return .up
default:
return .right
}
}
}

@objc public class CKUtils: NSObject {

@objc public static func cropAndScale(_ image: UIImage, width: Int, height: Int) -> UIImage? {
@objc public static func cropAndScale(_ image: UIImage, width: Int, height: Int, orientation: UIDeviceOrientation, mirrored: Bool) -> UIImage? {
let fromRect = CGRect(x: 0, y: 0, width: image.size.height, height: image.size.width)
var toRect = CGRect(x: 0, y: 0, width: height, height: width)

Expand Down Expand Up @@ -40,7 +68,8 @@ import UIKit
guard let finalCgImage = context.makeImage() else {
return nil
}

return UIImage(cgImage: finalCgImage, scale: 1.0, orientation: .right)

let orientation = mirrored ? orientation.imageOrientationMirrored : orientation.imageOrientation
return UIImage(cgImage: finalCgImage, scale: 1.0, orientation: orientation)
}
}
Loading

0 comments on commit 5b37efc

Please sign in to comment.