Class: Motion::Capture

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

Constant Summary collapse

CAMERA_POSITIONS =
{ rear: AVCaptureDevicePositionBack, front: AVCaptureDevicePositionFront }
FLASH_MODES =
{ on: AVCaptureFlashModeOn, off: AVCaptureFlashModeOff, auto: AVCaptureFlashModeAuto }

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 = {})
  @options = options
end

Instance Attribute Details

#deviceObject (readonly)

Returns the value of attribute device.



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

def device
  @device
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#preview_layerObject (readonly)

Returns the value of attribute preview_layer.



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

def preview_layer
  @preview_layer
end

Instance Method Details

#attach(view, options = {}) ⇒ Object



86
87
88
89
90
# File 'lib/project/motion-capture.rb', line 86

def attach(view, options = {})
  @preview_layer = preview_layer_for_view(view, options)

  view.layer.addSublayer(preview_layer)
end

#can_set_preset?(preset) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/project/motion-capture.rb', line 118

def can_set_preset?(preset)
  session.canSetSessionPreset(preset)
end

#capture(&block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/project/motion-capture.rb', line 42

def capture(&block)
  still_image_output.captureStillImageAsynchronouslyFromConnection(still_image_connection, completionHandler: -> (buffer, error) {
    if error
      error_callback.call(error)
    else
      image_data = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer)

      block.call(image_data)
    end
  })
end

#capture_and_save(&block) ⇒ Object



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

def capture_and_save(&block)
  capture do |jpeg_data|
    save_data(jpeg_data) do |asset_url|
      block.call(jpeg_data, asset_url)
    end
  end
end

#capture_image(&block) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/project/motion-capture.rb', line 54

def capture_image(&block)
  capture do |jpeg_data|
    image = UIImage.imageWithData(jpeg_data)

    block.call(image)
  end
end

#capture_image_and_save(&block) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/project/motion-capture.rb', line 70

def capture_image_and_save(&block)
  capture do |jpeg_data|
    save_data(jpeg_data) do |asset_url|
      image = UIImage.imageWithData(jpeg_data)

      block.call(image, asset_url)
    end
  end
end

#flashObject



130
131
132
# File 'lib/project/motion-capture.rb', line 130

def flash
  device.flashMode if @device
end

#on_error(block) ⇒ Object



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

def on_error(block)
  @error_callback = block
end

#presetObject



126
127
128
# File 'lib/project/motion-capture.rb', line 126

def preset
  session.captureSession if @session
end

#running?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/project/motion-capture.rb', line 38

def running?
  @session && session.running?
end

#save_data(jpeg_data, &block) ⇒ Object



80
81
82
83
84
# File 'lib/project/motion-capture.rb', line 80

def save_data(jpeg_data, &block)
  assets_library.writeImageDataToSavedPhotosAlbum(jpeg_data, metadata: nil, completionBlock: -> (asset_url, error) {
    error ? error_callback.call(error) : block.call(asset_url)
  })
end

#set_flash(mode = :auto) ⇒ Object



134
135
136
137
138
# File 'lib/project/motion-capture.rb', line 134

def set_flash(mode = :auto)
  configure_with_lock do
    device.flashMode = FLASH_MODES[mode] if FLASH_MODES.keys.include? mode
  end
end

#set_preset(preset) ⇒ Object



122
123
124
# File 'lib/project/motion-capture.rb', line 122

def set_preset(preset)
  session.sessionPreset = preset if can_set_preset? preset
end

#start!(preset = AVCaptureSessionPresetPhoto) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/project/motion-capture.rb', line 15

def start!(preset = AVCaptureSessionPresetPhoto)
  use_camera(options.fetch(:device, :default))

  set_preset(preset)

  add_ouput(still_image_output)

  session.startRunning
end

#stop!Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/project/motion-capture.rb', line 25

def stop!
  session.stopRunning

  remove_outputs
  remove_inputs

  preview_layer.removeFromSuperlayer if preview_layer && preview_layer.superlayer

  @still_image_output = nil
  @session            = nil
  @preview_layer      = nil
end

#toggle_cameraObject



104
105
106
107
108
# File 'lib/project/motion-capture.rb', line 104

def toggle_camera
  target_camera = using_rear_camera? ? :front : :rear

  use_camera(target_camera)
end

#toggle_flashObject



110
111
112
113
114
115
116
# File 'lib/project/motion-capture.rb', line 110

def toggle_flash
  if device && device.hasFlash
    target_mode = flash_on? ? :off : :on

    set_flash(target_mode)
  end
end

#use_camera(target_camera = :default) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/project/motion-capture.rb', line 92

def use_camera(target_camera = :default)
  @device = camera_devices[target_camera]

  error_pointer = Pointer.new(:object)

  if input = AVCaptureDeviceInput.deviceInputWithDevice(device, error: error_pointer)
    set_input(input)
  else
    error_callback.call(error_pointer[0])
  end
end