-
Notifications
You must be signed in to change notification settings - Fork 0
/
CameraInstructionPacket.swift
128 lines (111 loc) · 6.44 KB
/
CameraInstructionPacket.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// CameraInstructionPacket.swift
//
//
// Created by Nicholas Mosier on 5/25/17.
//
//
import Foundation
import AVFoundation
@objc(CameraInstruction)
// CameraInstruction: specifies type of instruction being sent to iPhone
enum CameraInstruction: Int {
case SetLensPosition // requires lensPosition parameter
case GetLensPosition
case LockLensPosition // lensPosition parameter optional
case SetPointOfFocus // requires pointOfFocus parameter
case ConfigureTorchMode
case ReadExposure, LockExposure, AutoExposure, SetExposure // sets camera exposure mode
case CaptureStillImage // capture single image; optional parameters: resolution
case CapturePhotoBracket // capture photo bracket; required parameters:
// photoBracketExposureDurations; optional parameters: resolution
case StartVideoCapture, EndVideoCapture
// the following instructions are used by captureWithStructuredLighting function in Mac app's
// ProgramControl.swift
case CaptureNormalInvertedPair, FinishCapturePair
case StartStructuredLightingCaptureFull, EndStructuredLightingCaptureFull
case EndCaptureSession // not yet implemented
case LockWhiteBalance, AutoWhiteBalance // sets white balance modes -- feature not used/relevant, though
}
@objc(CameraInstructionPacket) // IMPORTANT: this line ensures that both the Mac and iPhone
// targets consider this to be the same class during sending/receipt
class CameraInstructionPacket: NSObject, NSCoding {
// Parameters
//---IN USE---
var cameraInstruction: CameraInstruction!
var resolution: String = "max" // for setting photo resolution (use constants "AVCaptureSessionPreset...")
var photoBracketExposureDurations: [Double]? // values are in seconds
var photoBracketExposureISOs: [Double]?
var pointOfFocus: CGPoint? // point of focus: represents where to focus in field of view
var lensPosition: Float? // sets focus
// binary code parameters
var binaryCodeSystem: BinaryCodeSystem?
var binaryCodeDirection: Bool?
var binaryCodeBit: Int? // bit of binary code being displayed
var binaryCodeInverted: Bool? // if binary code is inverted
var torchMode: AVCaptureDevice.TorchMode?
var torchLevel: Float?
var flashMode: AVCaptureDevice.FlashMode?
//MARK: INITIALIZERS
// public initializer for CameraInstructionPacket
// NOTE: most values are optional, so they are safe to omit when calling this function unless
// you need to use them
convenience init(cameraInstruction: CameraInstruction, resolution: String = "max", photoBracketExposureDurations: [Double]? = nil, pointOfFocus: CGPoint? = nil, torchMode: AVCaptureDevice.TorchMode? = nil, torchLevel: Float? = nil, flashMode: AVCaptureDevice.FlashMode? = nil, lensPosition: Float? = nil, binaryCodeBit: Int? = nil, binaryCodeDirection: Bool? = nil, binaryCodeInverted: Bool? = nil, binaryCodeSystem: BinaryCodeSystem? = nil, photoBracketExposureISOs: [Double]? = nil) {
self.init()
self.cameraInstruction = cameraInstruction
self.resolution = resolution
self.photoBracketExposureDurations = photoBracketExposureDurations
self.photoBracketExposureISOs = photoBracketExposureISOs
self.pointOfFocus = pointOfFocus
self.torchMode = torchMode
self.torchLevel = torchLevel
self.lensPosition = lensPosition
self.binaryCodeBit = binaryCodeBit
self.binaryCodeDirection = binaryCodeDirection
self.binaryCodeInverted = binaryCodeInverted
self.binaryCodeSystem = binaryCodeSystem
self.flashMode = flashMode
}
// for decoding packet (never called by programmer)
required convenience init?(coder decoder: NSCoder) {
self.init()
self.cameraInstruction = CameraInstruction(rawValue: decoder.decodeInteger(forKey: "cameraInstruction"))
self.resolution = decoder.decodeObject(forKey: "resolution") as! String
self.photoBracketExposureDurations = decoder.decodeObject(forKey: "photoBracketExposureDurations") as! [Double]?
self.photoBracketExposureISOs = decoder.decodeObject(forKey: "photoBracketExposureISOs") as! [Double]?
self.pointOfFocus = decoder.decodeObject(forKey: "pointOfFocus") as! CGPoint?
if let torchModeRaw = decoder.decodeObject(forKey: "torchMode") as! Int? {
self.torchMode = AVCaptureDevice.TorchMode(rawValue: torchModeRaw)
} else {
self.torchMode = nil
}
self.torchLevel = decoder.decodeObject(forKey: "torchLevel") as! Float?
if let torchModeRawValue = decoder.decodeObject(forKey: "flashMode") as! Int? {
self.flashMode = AVCaptureDevice.FlashMode(rawValue: torchModeRawValue)
}
self.lensPosition = decoder.decodeObject(forKey: "lensPosition") as! Float?
if let binaryCodeSystemRaw = decoder.decodeObject(forKey: "binaryCodeSystem") as! Int? {
self.binaryCodeSystem = BinaryCodeSystem(rawValue: binaryCodeSystemRaw)
}
self.binaryCodeBit = decoder.decodeObject(forKey: "binaryCodeBit") as! Int?
self.binaryCodeInverted = decoder.decodeObject(forKey: "binaryCodeInverted") as! Bool?
self.binaryCodeDirection = decoder.decodeObject(forKey: "binaryCodeDirection") as! Bool?
}
// never called by programmer
// encodes packet before being sent
func encode(with coder: NSCoder) {
coder.encode(self.cameraInstruction.rawValue, forKey: "cameraInstruction")
coder.encode(self.resolution, forKey: "resolution")
coder.encode(self.photoBracketExposureDurations, forKey: "photoBracketExposureDurations")
coder.encode(self.photoBracketExposureISOs, forKey: "photoBracketExposureISOs")
coder.encode(self.pointOfFocus, forKey: "pointOfFocus")
coder.encode(self.torchMode?.rawValue, forKey: "torchMode")
coder.encode(self.torchLevel, forKey: "torchLevel")
coder.encode(self.flashMode?.rawValue, forKey: "flashMode")
coder.encode(self.lensPosition, forKey: "lensPosition")
coder.encode(self.binaryCodeSystem?.rawValue ?? nil, forKey: "binaryCodeSystem")
coder.encode(self.binaryCodeBit, forKey: "binaryCodeBit")
coder.encode(self.binaryCodeInverted, forKey: "binaryCodeInverted")
coder.encode(self.binaryCodeDirection, forKey: "binaryCodeDirection")
}
}