Skip to content

Commit

Permalink
Merge pull request #9 from CameraKit/develop
Browse files Browse the repository at this point in the history
Release v1.1.0
  • Loading branch information
austinkettner committed Feb 19, 2019
2 parents a96f783 + 663c1a9 commit 4d3722f
Show file tree
Hide file tree
Showing 25 changed files with 890 additions and 131 deletions.
File renamed without changes.
43 changes: 43 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Contribute

## Introduction

First, thank you for considering contributing to CameraKit-iOS! It's people like you that make the open source community such a great community! 😊

We welcome any type of contribution, not only code. You can help with
- **QA**: file bug reports, the more details you can give the better (e.g. screenshots with the console open)
- **Marketing**: writing blog posts, howto's, printing stickers, ...
- **Community**: presenting the project at meetups, organizing a dedicated meetup for the local community, ...
- **Code**: take a look at the [open issues](issues). Even if you can't write code, commenting on them, showing that you care about a given issue matters. It helps us triage them.

## Reporting Issues

A great way to contribute to the project is to send a detailed issue when you encounter a problem. We always appreciate a well-written, thorough bug report.

Check that the project issues database
doesn't already include that problem or suggestion before submitting an issue.
If you find a match, add a quick "+1" or "I have this problem too."
Doing this helps prioritize the most common problems and requests.

When reporting issues, please include the following:

* The version of Xcode you're using
* The device and version of iOS you're targeting
* The full output of any stack trace or compiler error
* A code snippet that reproduces the described behavior, if applicable
* Any other details that would be useful in understanding the problem

This information will help us review and fix your issue faster.

## Submitting code

Any code change should be submitted as a pull request. The description should explain what the code does and give steps to execute it.

## Code review process

The bigger the pull request, the longer it will take to review and merge. Try to break down large pull requests in smaller chunks that are easier to review and merge.
It is also always helpful to have some context for your pull request. What was the purpose? Why does it matter to you?

## Questions

If you have any questions, create an [issue](issue) (protip: do a quick search first to see if someone else didn't ask the same question before!).
2 changes: 1 addition & 1 deletion CameraKit-iOS.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "CameraKit-iOS"
s.version = "1.0.0"
s.version = "1.1.0"
s.summary = "Camera library for iOS written in pure Swift."
s.description = "Easy to work with, camera library for iOS written in pure Swift."
s.homepage = "https://camerakit.io/"
Expand Down
5 changes: 4 additions & 1 deletion CameraKit.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions CameraKit/CameraKit/CKGridView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@

import UIKit

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

public var color: UIColor = UIColor.white.withAlphaComponent(0.5) {
@objc public var color: UIColor = UIColor.white.withAlphaComponent(0.5) {
didSet {
self.setNeedsDisplay()
}
}

public override init(frame: CGRect) {
@objc public override init(frame: CGRect) {
super.init(frame: frame)
self.setupView()
}

public required init?(coder aDecoder: NSCoder) {
@objc public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setupView()
}
Expand Down
102 changes: 46 additions & 56 deletions CameraKit/CameraKit/CKPhotoSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
// Copyright © 2019 Wonderkiln. All rights reserved.
//

import UIKit
import AVFoundation

extension CKPhotoSession.FlashMode {
extension CKSession.FlashMode {

var captureFlashMode: AVCaptureDevice.FlashMode {
switch self {
Expand All @@ -19,19 +20,13 @@ extension CKPhotoSession.FlashMode {
}
}

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

public enum CameraDetection {
@objc public enum CameraDetection: UInt {
case none, faces
}

public enum FlashMode {
case off, on, auto
}

public typealias CaptureCallback = (UIImage?, AVCaptureResolvedPhotoSettings?, CKError?) -> Void

public var cameraPosition = CameraPosition.back {
@objc public var cameraPosition = CameraPosition.back {
didSet {
do {
let deviceInput = try CKSession.captureDeviceInput(type: self.cameraPosition.deviceType)
Expand All @@ -42,7 +37,7 @@ public class CKPhotoSession: CKSession, AVCapturePhotoCaptureDelegate, AVCapture
}
}

public var cameraDetection = CameraDetection.none {
@objc public var cameraDetection = CameraDetection.none {
didSet {
if oldValue == self.cameraDetection { return }

Expand All @@ -67,7 +62,7 @@ public class CKPhotoSession: CKSession, AVCapturePhotoCaptureDelegate, AVCapture
}
}

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

var captureDeviceInput: AVCaptureDeviceInput? {
didSet {
Expand All @@ -86,10 +81,9 @@ public class CKPhotoSession: CKSession, AVCapturePhotoCaptureDelegate, AVCapture

let photoOutput = AVCapturePhotoOutput()

var captureCallback: CaptureCallback?
var faceDetectionBoxes: [UIView] = []

public init(position: CameraPosition = .back, detection: CameraDetection = .none) {
@objc public init(position: CameraPosition = .back, detection: CameraDetection = .none) {
super.init()

defer {
Expand All @@ -101,24 +95,28 @@ public class CKPhotoSession: CKSession, AVCapturePhotoCaptureDelegate, AVCapture
self.session.addOutput(self.photoOutput)
}

deinit {
@objc deinit {
self.faceDetectionBoxes.forEach({ $0.removeFromSuperview() })
}

public func capture(_ callback: @escaping CaptureCallback) {
var captureCallback: (UIImage, AVCaptureResolvedPhotoSettings) -> Void = { (_, _) in }
var errorCallback: (Error) -> Void = { (_) in }

@objc public func capture(_ callback: @escaping (UIImage, AVCaptureResolvedPhotoSettings) -> Void, _ error: @escaping (Error) -> Void) {
self.captureCallback = callback

self.errorCallback = error

let settings = AVCapturePhotoSettings()
settings.flashMode = self.flashMode.captureFlashMode

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

public func togglePosition() {
@objc public func togglePosition() {
self.cameraPosition = self.cameraPosition == .back ? .front : .back
}

public override var zoom: Double {
@objc public override var zoom: Double {
didSet {
guard let device = self.captureDeviceInput?.device else {
return
Expand All @@ -138,7 +136,7 @@ public class CKPhotoSession: CKSession, AVCapturePhotoCaptureDelegate, AVCapture
}
}

public var resolution: CGSize? {
@objc public var resolution = CGSize.zero {
didSet {
guard let deviceInput = self.captureDeviceInput else {
return
Expand All @@ -148,8 +146,8 @@ public class CKPhotoSession: CKSession, AVCapturePhotoCaptureDelegate, AVCapture
try deviceInput.device.lockForConfiguration()

if
let resolution = self.resolution,
let format = CKSession.deviceInputFormat(input: deviceInput, width: Int(resolution.width), height: Int(resolution.height))
self.resolution.width > 0, self.resolution.height > 0,
let format = CKSession.deviceInputFormat(input: deviceInput, width: Int(self.resolution.width), height: Int(self.resolution.height))
{
deviceInput.device.activeFormat = format
} else {
Expand All @@ -163,7 +161,7 @@ public class CKPhotoSession: CKSession, AVCapturePhotoCaptureDelegate, AVCapture
}
}

public override func focus(at point: CGPoint) {
@objc public override func focus(at point: CGPoint) {
if let device = self.captureDeviceInput?.device, device.isFocusPointOfInterestSupported {
do {
try device.lockForConfiguration()
Expand All @@ -178,69 +176,61 @@ public class CKPhotoSession: CKSession, AVCapturePhotoCaptureDelegate, AVCapture

@available(iOS 11.0, *)
public func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {

guard let captureCallback = self.captureCallback else {
return
defer {
self.captureCallback = { (_, _) in }
self.errorCallback = { (_) in }
}

defer { self.captureCallback = nil }


if let error = error {
captureCallback(nil, nil, CKError.error(error.localizedDescription))
self.errorCallback(error)
return
}

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

self.processPhotoData(data: data, resolvedSettings: photo.resolvedSettings)
}

public func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photoSampleBuffer: CMSampleBuffer?, previewPhoto previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {

guard let captureCallback = self.captureCallback else {
return
defer {
self.captureCallback = { (_, _) in }
self.errorCallback = { (_) in }
}

defer { self.captureCallback = nil }


if let error = error {
captureCallback(nil, nil, CKError.error(error.localizedDescription))
self.errorCallback(error)
return
}

guard
let photoSampleBuffer = photoSampleBuffer, let previewPhotoSampleBuffer = previewPhotoSampleBuffer,
let data = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoSampleBuffer, previewPhotoSampleBuffer: previewPhotoSampleBuffer) else
{
captureCallback(nil, nil, CKError.error("Cannot get photo file data representation"))
self.errorCallback(CKError.error("Cannot get photo file data representation"))
return
}

self.processPhotoData(data: data, resolvedSettings: resolvedSettings)
}

func processPhotoData(data: Data, resolvedSettings: AVCaptureResolvedPhotoSettings) {
guard let captureCallback = self.captureCallback else {
return
}

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

if
let resolution = self.resolution,
let transformedImage = CKUtils.cropAndScale(image, width: Int(resolution.width), height: Int(resolution.height))
self.resolution.width > 0, self.resolution.height > 0,
let transformedImage = CKUtils.cropAndScale(image, width: Int(self.resolution.width), height: Int(self.resolution.height))
{
captureCallback(transformedImage, resolvedSettings, nil)
self.captureCallback(transformedImage, resolvedSettings)
return
}
captureCallback(image, resolvedSettings, nil)

self.captureCallback(image, resolvedSettings)
}

public func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
Expand Down
14 changes: 7 additions & 7 deletions CameraKit/CameraKit/CKPreviewView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import UIKit
import AVFoundation

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

private var lastScale: CGFloat = 1.0

private(set) public var previewLayer: AVCaptureVideoPreviewLayer? {
@objc private(set) public var previewLayer: AVCaptureVideoPreviewLayer? {
didSet {
oldValue?.removeFromSuperlayer()

Expand All @@ -23,7 +23,7 @@ public class CKPreviewView: UIView {
}
}

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

Expand All @@ -36,7 +36,7 @@ public class CKPreviewView: UIView {
}
}

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

Expand All @@ -46,7 +46,7 @@ public class CKPreviewView: UIView {
}
}

public var showGrid: Bool = false {
@objc public var showGrid: Bool = false {
didSet {
if self.showGrid == oldValue {
return
Expand All @@ -60,12 +60,12 @@ public class CKPreviewView: UIView {
}
}

public override init(frame: CGRect) {
@objc public override init(frame: CGRect) {
super.init(frame: frame)
self.setupView()
}

public required init?(coder aDecoder: NSCoder) {
@objc public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setupView()
}
Expand Down
Loading

0 comments on commit 4d3722f

Please sign in to comment.