forked from Khan/Prototope
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Khan#43 from Khan/camera-layers
Camera layers
- Loading branch information
Showing
7 changed files
with
200 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
var cameraLayer = new CameraLayer() | ||
cameraLayer.width = Layer.root.width * 0.5 | ||
cameraLayer.height = Layer.root.height * 0.5 | ||
cameraLayer.cameraPosition = CameraPosition.Front | ||
cameraLayer.x = Layer.root.x | ||
cameraLayer.y = Layer.root.y | ||
|
||
var flipButton = new TextLayer() | ||
flipButton.fontName = "Futura" | ||
flipButton.fontSize = 30 | ||
flipButton.text = "Flip" | ||
flipButton.x = Layer.root.x | ||
flipButton.y = 100 | ||
flipButton.animators.alpha.springBounciness = 0 | ||
flipButton.animators.alpha.springSpeed = 20 | ||
|
||
Layer.root.touchBeganHandler = function() { | ||
flipButton.animators.alpha.target = 0.5 | ||
} | ||
Layer.root.touchEndedHandler = function(touchSequence) { | ||
cameraLayer.cameraPosition = (cameraLayer.cameraPosition === CameraPosition.Front) ? CameraPosition.Back : CameraPosition.Front | ||
flipButton.animators.alpha.target = 1.0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// | ||
// CameraLayer.swift | ||
// Prototope | ||
// | ||
// Created by Andy Matuschak on 3/5/15. | ||
// Copyright (c) 2015 Khan Academy. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import AVFoundation | ||
|
||
/** A layer that shows the output of one of the device's cameras. Defaults to using the back camera. */ | ||
public class CameraLayer: Layer { | ||
public enum CameraPosition: Printable { | ||
/** The device's front-facing camera. */ | ||
case Front | ||
|
||
/** The device's back-facing camera. */ | ||
case Back | ||
|
||
private var avCaptureDevicePosition: AVCaptureDevicePosition { | ||
switch self { | ||
case .Front: return .Front | ||
case .Back: return .Back | ||
} | ||
} | ||
|
||
public var description: String { | ||
switch self { | ||
case .Front: return "Front" | ||
case .Back: return "Back" | ||
} | ||
} | ||
} | ||
|
||
/** Selects which camera to use. */ | ||
public var cameraPosition: CameraPosition { | ||
didSet { updateSession() } | ||
} | ||
|
||
private var captureSession: AVCaptureSession? | ||
|
||
public init(parent: Layer? = Layer.root, name: String? = nil) { | ||
self.cameraPosition = .Back | ||
super.init(parent: parent, name: name, viewClass: CameraView.self) | ||
updateSession() | ||
} | ||
|
||
deinit { | ||
captureSession?.stopRunning() | ||
} | ||
|
||
private func updateSession() { | ||
// Find device matching camera setting | ||
let devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo) as! [AVCaptureDevice] | ||
if let device = filter(devices, { device in return device.position == self.cameraPosition.avCaptureDevicePosition }).first { | ||
var error: NSError? | ||
if let input = AVCaptureDeviceInput(device: device, error: &error) { | ||
captureSession?.stopRunning() | ||
|
||
captureSession = AVCaptureSession() | ||
captureSession!.addInput(input) | ||
captureSession!.startRunning() | ||
cameraLayer.session = captureSession! | ||
cameraLayer.videoGravity = AVLayerVideoGravityResizeAspectFill | ||
} else { | ||
Environment.currentEnvironment!.exceptionHandler("Couldn't create camera device: \(error)") | ||
} | ||
|
||
} else { | ||
Environment.currentEnvironment!.exceptionHandler("Could not find a \(cameraPosition.description.lowercaseString) camera on this device") | ||
} | ||
|
||
} | ||
|
||
private var cameraLayer: AVCaptureVideoPreviewLayer { | ||
return (self.view as! CameraView).layer as! AVCaptureVideoPreviewLayer | ||
} | ||
|
||
/** Underlying camera view class. */ | ||
private class CameraView: UIView { | ||
override class func layerClass() -> AnyClass { | ||
return AVCaptureVideoPreviewLayer.self | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// | ||
// CameraLayerBridge.swift | ||
// Prototope | ||
// | ||
// Created by Andy Matuschak on 2/15/15. | ||
// Copyright (c) 2015 Khan Academy. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import Prototope | ||
import JavaScriptCore | ||
|
||
@objc public protocol CameraLayerJSExport: JSExport { | ||
var cameraPosition: JSValue { get set } | ||
} | ||
|
||
@objc public class CameraLayerBridge: LayerBridge, CameraLayerJSExport, BridgeType { | ||
var cameraLayer: CameraLayer { return layer as! CameraLayer } | ||
|
||
public override class func addToContext(context: JSContext) { | ||
context.setObject(self, forKeyedSubscript: "CameraLayer") | ||
} | ||
|
||
public required init?(args: NSDictionary) { | ||
let parentLayer = (args["parent"] as! LayerBridge?)?.layer | ||
let cameraLayer = CameraLayer(parent: parentLayer, name: (args["name"] as! String?)) | ||
super.init(cameraLayer) | ||
} | ||
|
||
public var cameraPosition: JSValue { | ||
get { return CameraPositionBridge.encodeCameraPosition(cameraLayer.cameraPosition, inContext: JSContext.currentContext()) } | ||
set { cameraLayer.cameraPosition = CameraPositionBridge.decodeCameraPosition(newValue) } | ||
} | ||
|
||
} | ||
|
||
public class CameraPositionBridge: NSObject, BridgeType { | ||
enum RawCameraPosition: Int { | ||
case Front = 0 | ||
case Back = 1 | ||
} | ||
|
||
public class func addToContext(context: JSContext) { | ||
let alignmentObject = JSValue(newObjectInContext: context) | ||
alignmentObject.setObject(RawCameraPosition.Front.rawValue, forKeyedSubscript: "Front") | ||
alignmentObject.setObject(RawCameraPosition.Back.rawValue, forKeyedSubscript: "Back") | ||
context.setObject(alignmentObject, forKeyedSubscript: "CameraPosition") | ||
} | ||
|
||
public class func encodeCameraPosition(cameraPosition: Prototope.CameraLayer.CameraPosition, inContext context: JSContext) -> JSValue { | ||
var rawCameraPosition: RawCameraPosition | ||
switch cameraPosition { | ||
case .Front: rawCameraPosition = .Front | ||
case .Back: rawCameraPosition = .Back | ||
} | ||
return JSValue(int32: Int32(rawCameraPosition.rawValue), inContext: context) | ||
} | ||
|
||
public class func decodeCameraPosition(bridgedCameraPosition: JSValue) -> Prototope.CameraLayer.CameraPosition! { | ||
if let rawCameraPosition = RawCameraPosition(rawValue: Int(bridgedCameraPosition.toInt32())) { | ||
switch rawCameraPosition { | ||
case .Front: return .Front | ||
case .Back: return .Back | ||
} | ||
} else { | ||
Environment.currentEnvironment!.exceptionHandler("Unknown camera position: \(bridgedCameraPosition)") | ||
return nil | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters