Class: CameraController

Inherits:
AVClubController show all
Defined in:
app/camera_controller.rb

Instance Attribute Summary collapse

Attributes inherited from AVClubController

#viewFinderView

Instance Method Summary collapse

Methods inherited from AVClubController

#rotateCameraTo, #startInView

Instance Attribute Details

#record_buttonObject

Returns the value of attribute record_button.



2
3
4
# File 'app/camera_controller.rb', line 2

def record_button
  @record_button
end

#still_buttonObject

Returns the value of attribute still_button.



2
3
4
# File 'app/camera_controller.rb', line 2

def still_button
  @still_button
end

#toggle_buttonObject

Returns the value of attribute toggle_button.



2
3
4
# File 'app/camera_controller.rb', line 2

def toggle_button
  @toggle_button
end

Instance Method Details

#captureStillImage(sender) ⇒ Object



118
119
120
121
122
123
124
# File 'app/camera_controller.rb', line 118

def captureStillImage(sender)
  return unless still_button.isEnabled

  # Capture a still image
  still_button.setEnabled(false)
  club.captureStillImageAnimated(true)
end

#club(club, assetSavedToURL: image, error: error) ⇒ Object



159
160
161
162
163
164
165
# File 'app/camera_controller.rb', line 159

def club(club, stillImageCaptured:image, error:error)
  if image
    club.saveImageToLibrary(image)
  else
    update_button_states
  end
end

#clubDeviceConfigurationChanged(club) ⇒ Object



171
172
173
# File 'app/camera_controller.rb', line 171

def clubDeviceConfigurationChanged(club)
  update_button_states
end

#clubRecordingBegan(club) ⇒ Object



149
150
151
152
# File 'app/camera_controller.rb', line 149

def clubRecordingBegan(club)
  self.record_button.setTitle('Stop', forState:UIControlStateNormal)
  update_button_states
end

#clubRecordingFinished(club) ⇒ Object



154
155
156
157
# File 'app/camera_controller.rb', line 154

def clubRecordingFinished(club)
  self.record_button.setTitle('Record', forState:UIControlStateNormal)
  update_button_states
end

#tapToAutoFocus(gestureRecognizer) ⇒ Object

Auto focus at a particular point. The focus mode will change to locked once the auto focus happens.



80
81
82
83
84
85
86
87
88
# File 'app/camera_controller.rb', line 80

def tapToAutoFocus(gestureRecognizer)
  return unless club.videoInput

  if club.videoInput.device.isFocusPointOfInterestSupported
    tapPoint = gestureRecognizer.locationInView(viewFinderView)
    convertedFocusPoint = club.convertToPointOfInterestFromViewCoordinates(tapPoint)
    club.autoFocusAtPoint(convertedFocusPoint)
  end
end

#tapToContinouslyAutoFocus(gestureRecognizer) ⇒ Object

Change to continuous auto focus. The camera will constantly focus at the point choosen.



92
93
94
95
96
97
98
# File 'app/camera_controller.rb', line 92

def tapToContinouslyAutoFocus(gestureRecognizer)
  return unless club.videoInput

  if club.videoInput.device.isFocusPointOfInterestSupported
    club.continuousFocusAtPoint(CGPoint.new(0.5, 0.5))
  end
end

#toggleCamera(sender) ⇒ Object



100
101
102
103
104
105
106
# File 'app/camera_controller.rb', line 100

def toggleCamera(sender)
  # Toggle between cameras when there is more than one
  club.toggleCamera

  # Do an initial focus
  club.continuousFocusAtPoint(CGPoint.new(0.5, 0.5))
end

#toggleRecording(sender) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'app/camera_controller.rb', line 108

def toggleRecording(sender)
  # Start recording if there isn't a recording running. Stop recording if there is.
  record_button.setEnabled(false)
  unless club.recorder.isRecording
    club.startRecording
  else
    club.stopRecording
  end
end

#update_button_statesObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/camera_controller.rb', line 126

def update_button_states
  if club.cameraCount > 1
    self.toggle_button.enabled = true
    self.record_button.enabled = true
    self.still_button.enabled = true
  else
    self.toggle_button.enabled = false

    if club.cameraCount > 0
      self.record_button.enabled = true
      self.still_button.enabled = true
    else
      self.still_button.enabled = false

      if club.micCount > 0
        self.record_button = true
      else
        self.record_button = false
      end
    end
  end
end

#viewDidLoadObject



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
# File 'app/camera_controller.rb', line 4

def viewDidLoad
  super
  self.view.backgroundColor = UIColor.darkGrayColor

  self.viewFinderView = UIView.alloc.initWithFrame(UIEdgeInsetsInsetRect(self.view.bounds, [50, 20, 20, 20]))
  self.viewFinderView.autoresizingMask = UIViewAutoresizingFlexibleHeight |
                                           UIViewAutoresizingFlexibleWidth
  self.viewFinderView.backgroundColor = UIColor.lightGrayColor
  self.view.addSubview(self.viewFinderView)

  ### important ###
  startInView(self.viewFinderView)

  width = 0
  height = 0
  self.toggle_button = UIButton.buttonWithType(UIButtonTypeRoundedRect).tap do |button|
    button.setTitle('Camera', forState:UIControlStateNormal)
    button.sizeToFit
    width += CGRectGetWidth(button.frame)
    height = CGRectGetHeight(button.frame)

    button.addTarget(self, action: 'toggleCamera:', forControlEvents:UIControlEventTouchUpInside)
  end
  width += 10

  self.record_button = UIButton.buttonWithType(UIButtonTypeRoundedRect).tap do |button|
    button.setTitle('Record', forState:UIControlStateNormal)
    button.sizeToFit
    width += CGRectGetWidth(button.frame)

    button.addTarget(self, action: 'toggleRecording:', forControlEvents:UIControlEventTouchUpInside)
  end
  width += 10

  self.still_button = UIButton.buttonWithType(UIButtonTypeRoundedRect).tap do |button|
    button.setTitle('Photo', forState:UIControlStateNormal)
    button.sizeToFit
    width += CGRectGetWidth(button.frame)

    button.addTarget(self, action: 'captureStillImage:', forControlEvents:UIControlEventTouchUpInside)
  end

  left = (CGRectGetWidth(self.view.frame) - width) / 2.0
  top = 5
  buttons_view = UIView.alloc.initWithFrame([[left, top], [width, height]])
  buttons_view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |
                                  UIViewAutoresizingFlexibleTopMargin |
                                  UIViewAutoresizingFlexibleRightMargin |
                                  UIViewAutoresizingFlexibleBottomMargin

  left = 0
  top = 0
  self.toggle_button.frame = [[left, top], self.toggle_button.frame.size]
  left += CGRectGetWidth(self.toggle_button.frame) + 10
  self.record_button.frame = [[left, top], self.record_button.frame.size]
  left += CGRectGetWidth(self.record_button.frame) + 10
  self.still_button.frame = [[left, top], self.still_button.frame.size]
  left += CGRectGetWidth(self.still_button.frame) + 10

  buttons_view.addSubview(self.toggle_button)
  buttons_view.addSubview(self.record_button)
  buttons_view.addSubview(self.still_button)

  self.view.addSubview(buttons_view)

  self.update_button_states

  # Add a single tap gesture to focus on the point tapped, then lock focus
  singleTap = UITapGestureRecognizer.alloc.initWithTarget(self, action:'tapToAutoFocus:')
  singleTap.setDelegate(self)
  singleTap.setNumberOfTapsRequired(1)
  self.viewFinderView.addGestureRecognizer(singleTap)
end

#willRotateToInterfaceOrientation(toInterfaceOrientation, duration: duration) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'app/camera_controller.rb', line 175

def willRotateToInterfaceOrientation(toInterfaceOrientation, duration:duration)
  super

  case toInterfaceOrientation
  when UIInterfaceOrientationLandscapeLeft
    new_frame = CGRect.new([0, 0], [480, 320])
  when UIInterfaceOrientationLandscapeRight
    new_frame = CGRect.new([0, 0], [480, 320])
  when UIInterfaceOrientationPortrait
    new_frame = CGRect.new([0, 0], [320, 480])
  when UIInterfaceOrientationPortraitUpsideDown
    new_frame = CGRect.new([0, 0], [320, 480])
  end

  ### important ###
  rotateCameraTo(new_frame, orientation:toInterfaceOrientation, duration:duration)
end