Class: Motion::Capture

Inherits:
Object
  • Object
show all
Defined in:
lib/project/motion-capture.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{ device: :default }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Capture

Returns a new instance of Capture.



7
8
9
# File 'lib/project/motion-capture.rb', line 7

def initialize(options = {})
  self.options = options.merge(DEFAULT_OPTIONS)
end

Instance Attribute Details

#deviceObject

Returns the value of attribute device.



5
6
7
# File 'lib/project/motion-capture.rb', line 5

def device
  @device
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/project/motion-capture.rb', line 5

def options
  @options
end

Instance Method Details

#capture(&block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/project/motion-capture.rb', line 17

def capture(&block)
  still_image_connection = still_image_output.connections.first

  still_image_output.captureStillImageAsynchronouslyFromConnection(still_image_connection, completionHandler: lambda { |image_data_sample_buffer, error|
    if image_data_sample_buffer
      image_data = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(image_data_sample_buffer)

      image = UIImage.alloc.initWithData(image_data)

      block.call(image)
    else
      p "Error capturing image: #{error[0].description}"
    end
  })
end

#capture_preview_view(options) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/project/motion-capture.rb', line 33

def capture_preview_view(options)
  @_capture_preview_view ||= begin
    start!

    UIView.alloc.initWithFrame(options.fetch(:frame, CGRectZero)).tap do |view|
      view.backgroundColor = UIColor.whiteColor

      view.layer.addSublayer(preview_layer_for_view(view))
    end
  end
end

#start!Object



11
12
13
14
15
# File 'lib/project/motion-capture.rb', line 11

def start!
  use_camera(options[:device])

  add_ouput(still_image_output)
end

#toggle_cameraObject



60
61
62
63
64
65
66
# File 'lib/project/motion-capture.rb', line 60

def toggle_camera
  if using_rear_camera?
    use_camera(:front)
  else
    use_camera(:rear)
  end
end

#toggle_flashObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/project/motion-capture.rb', line 68

def toggle_flash
  if device && device.hasFlash
    error = Pointer.new(:object)
    if device.lockForConfiguration(error)
      if flash_on?
        turn_flash_off
      else
        turn_flash_on
      end

      device.unlockForConfiguration
    else
      p error[0].description
    end
  end
end

#use_camera(camera) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/project/motion-capture.rb', line 45

def use_camera(camera)
  device = camera_devices[camera]

  error = Pointer.new(:object)

  self.device = device
  input = AVCaptureDeviceInput.deviceInputWithDevice(device, error: error)

  if input
    set_input(input)
  else
    p error[0].description
  end
end