Class: Motion::Capture
- Inherits:
-
Object
- Object
- Motion::Capture
- 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
-
#device ⇒ Object
readonly
Returns the value of attribute device.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #attach(view, options = {}) ⇒ Object
- #capture(&block) ⇒ Object
- #capture_and_save(&block) ⇒ Object
- #capture_image(&block) ⇒ Object
- #capture_image_and_save(&block) ⇒ Object
-
#initialize(options = {}) ⇒ Capture
constructor
A new instance of Capture.
- #on_error(block) ⇒ Object
- #running? ⇒ Boolean
- #save_data(jpeg_data, &block) ⇒ Object
- #set_flash(mode = :auto) ⇒ Object
- #start! ⇒ Object
- #stop! ⇒ Object
- #toggle_camera ⇒ Object
- #toggle_flash ⇒ Object
- #use_camera(target_camera = :default) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Capture
Returns a new instance of Capture.
7 8 9 |
# File 'lib/project/motion-capture.rb', line 7 def initialize( = {}) = end |
Instance Attribute Details
#device ⇒ Object (readonly)
Returns the value of attribute device.
5 6 7 |
# File 'lib/project/motion-capture.rb', line 5 def device @device end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/project/motion-capture.rb', line 5 def end |
Instance Method Details
#attach(view, options = {}) ⇒ Object
80 81 82 83 84 |
# File 'lib/project/motion-capture.rb', line 80 def attach(view, = {}) preview_layer = preview_layer_for_view(view, ) view.layer.addSublayer(preview_layer) end |
#capture(&block) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/project/motion-capture.rb', line 36 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
56 57 58 59 60 61 62 |
# File 'lib/project/motion-capture.rb', line 56 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
48 49 50 51 52 53 54 |
# File 'lib/project/motion-capture.rb', line 48 def capture_image(&block) capture do |jpeg_data| image = UIImage.imageWithData(jpeg_data) block.call(image) end end |
#capture_image_and_save(&block) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/project/motion-capture.rb', line 64 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 |
#on_error(block) ⇒ Object
11 12 13 |
# File 'lib/project/motion-capture.rb', line 11 def on_error(block) @error_callback = block end |
#running? ⇒ Boolean
32 33 34 |
# File 'lib/project/motion-capture.rb', line 32 def running? session && session.running? end |
#save_data(jpeg_data, &block) ⇒ Object
74 75 76 77 78 |
# File 'lib/project/motion-capture.rb', line 74 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
112 113 114 115 116 |
# File 'lib/project/motion-capture.rb', line 112 def set_flash(mode = :auto) configure_with_lock do device.flashMode = FLASH_MODES[mode] if FLASH_MODES.keys.include? mode end end |
#start! ⇒ Object
15 16 17 18 19 |
# File 'lib/project/motion-capture.rb', line 15 def start! use_camera(.fetch(:device, :default)) add_ouput(still_image_output) end |
#stop! ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/project/motion-capture.rb', line 21 def stop! session.stopRunning remove_outputs remove_inputs @still_image_output = nil @session = nil @capture_preview_view = nil end |
#toggle_camera ⇒ Object
98 99 100 101 102 |
# File 'lib/project/motion-capture.rb', line 98 def toggle_camera target_camera = using_rear_camera? ? :front : :rear use_camera(target_camera) end |
#toggle_flash ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/project/motion-capture.rb', line 104 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
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/project/motion-capture.rb', line 86 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 |