Class: Tgios::ScanQrController
- Inherits:
-
UIViewController
- Object
- UIViewController
- Tgios::ScanQrController
- Defined in:
- lib/tgios/scan_qr_controller.rb
Instance Attribute Summary collapse
-
#types ⇒ Object
Returns the value of attribute types.
Instance Method Summary collapse
- #captureOutput(captureOutput, didOutputMetadataObjects: metadataObjects, fromConnection: connection) ⇒ Object
- #check_permission ⇒ Object
- #dealloc ⇒ Object
- #fake_scan ⇒ Object
- #init ⇒ Object
- #on(event, &block) ⇒ Object
- #openQRCode(result) ⇒ Object
- #setupCapture ⇒ Object
- #startScanning ⇒ Object
- #stop_scanning ⇒ Object
- #viewDidLoad ⇒ Object
Instance Attribute Details
#types ⇒ Object
Returns the value of attribute types.
3 4 5 |
# File 'lib/tgios/scan_qr_controller.rb', line 3 def types @types end |
Instance Method Details
#captureOutput(captureOutput, didOutputMetadataObjects: metadataObjects, fromConnection: connection) ⇒ Object
121 122 123 124 125 126 127 128 129 |
# File 'lib/tgios/scan_qr_controller.rb', line 121 def captureOutput(captureOutput, didOutputMetadataObjects: , fromConnection: connection) = [0] if !@scanned && .present? @scanned = true self.performSelectorOnMainThread('openQRCode:', withObject: .stringValue, waitUntilDone: false) end end |
#check_permission ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/tgios/scan_qr_controller.rb', line 19 def status = AVCaptureDevice.(AVMediaTypeVideo) if status == AVAuthorizationStatusAuthorized self.performSelector('startScanning', withObject: nil, afterDelay: 0.5) elsif status == AVAuthorizationStatusNotDetermined Dispatch::Queue.new('ScanQrController').async do AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: ->(granted) { if granted Dispatch::Queue.main.async do startScanning end else Dispatch::Queue.main.async do @events[:permission_denied].call if @events[:permission_denied] end end }) end else @events[:permission_denied].call if @events[:permission_denied] end end |
#dealloc ⇒ Object
176 177 178 179 |
# File 'lib/tgios/scan_qr_controller.rb', line 176 def dealloc ap "#{self.class.name} dealloc" super end |
#fake_scan ⇒ Object
155 156 157 158 159 160 161 162 163 |
# File 'lib/tgios/scan_qr_controller.rb', line 155 def fake_scan @load_view.stop_loading @isScanning = false @events[:fake_result].call do |result| fake_result = result end unless @events[:fake_result].nil? fake_result ||= "#{rand(100)+1}" openQRCode(fake_result) end |
#init ⇒ Object
170 171 172 173 174 |
# File 'lib/tgios/scan_qr_controller.rb', line 170 def init super @events={} self end |
#on(event, &block) ⇒ Object
165 166 167 168 |
# File 'lib/tgios/scan_qr_controller.rb', line 165 def on(event, &block) @events[event] = block.weak! self end |
#openQRCode(result) ⇒ Object
131 132 133 134 135 136 |
# File 'lib/tgios/scan_qr_controller.rb', line 131 def openQRCode(result) stop_scanning @events[:result_scanned].call(result) self.dismissViewControllerAnimated(true, completion: nil) end |
#setupCapture ⇒ Object
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 |
# File 'lib/tgios/scan_qr_controller.rb', line 47 def setupCapture #NSLog "setCapture()" @session = AVCaptureSession.alloc.init @session.sessionPreset = AVCaptureSessionPresetHigh @device = AVCaptureDevice.defaultDeviceWithMediaType AVMediaTypeVideo if @device.lockForConfiguration(nil) if @device.isFocusModeSupported(AVCaptureFocusModeContinuousAutoFocus) @device.focusMode = AVCaptureFocusModeContinuousAutoFocus end if @device.isAutoFocusRangeRestrictionSupported @device.autoFocusRangeRestriction = AVCaptureAutoFocusRangeRestrictionNear end @device.unlockForConfiguration end @error = Pointer.new('@') @input = AVCaptureDeviceInput.deviceInputWithDevice @device, error: @error @previewLayer = AVCaptureVideoPreviewLayer.alloc.initWithSession(@session) @previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill layerRect = self.view.layer.bounds @previewLayer.bounds = layerRect @previewLayer.setPosition(CGPointMake(CGRectGetMidX(layerRect), CGRectGetMidY(layerRect))) self.view.layer.addSublayer(@previewLayer) @queue = Dispatch::Queue.new('camQueue') @output = AVCaptureMetadataOutput.alloc.init @output.setMetadataObjectsDelegate self, queue: @queue.dispatch_object camera_size = layerRect.size sq_size = 240 sq_border = 20 line_length = 60 border_color = :white.cgcolor(0.8) sq_x = (camera_size.width - sq_size) / 2 sq_y = (camera_size.height - sq_size) / 2 square = Base.style(CALayer.layer, {frame: [[sq_x, sq_y],[sq_size, sq_size]]}) top_left = Base.style(CALayer.layer, frame: [[0, 0], [line_length, sq_border]], backgroundColor: border_color) top_right = Base.style(CALayer.layer, frame: [[sq_size-line_length, 0], [line_length, sq_border]], backgroundColor: border_color) left_top = Base.style(CALayer.layer, frame: [[0, sq_border], [sq_border, line_length - sq_border]], backgroundColor: border_color) right_top = Base.style(CALayer.layer, frame: [[sq_size - sq_border, sq_border], [sq_border, line_length - sq_border]], backgroundColor: border_color) left_bottom = Base.style(CALayer.layer, frame: [[0, sq_size - line_length], [sq_border, line_length - sq_border]], backgroundColor: border_color) right_bottom = Base.style(CALayer.layer, frame: [[sq_size - sq_border, sq_size - line_length], [sq_border, line_length - sq_border]], backgroundColor: border_color) bottom_left = Base.style(CALayer.layer, frame: [[0, sq_size - sq_border], [line_length, sq_border]], backgroundColor: border_color) bottom_right = Base.style(CALayer.layer, frame: [[sq_size-line_length, sq_size - sq_border], [line_length, sq_border]], backgroundColor: border_color) square.addSublayer top_left square.addSublayer top_right square.addSublayer left_top square.addSublayer right_top square.addSublayer left_bottom square.addSublayer right_bottom square.addSublayer bottom_left square.addSublayer bottom_right self.view.layer.addSublayer square @output.rectOfInterest = [[sq_y / camera_size.height, sq_x / camera_size.width], [sq_size / camera_size.height, sq_size / camera_size.width]] @session.addInput @input @session.addOutput @output @output. = ( @types || [AVMetadataObjectTypeQRCode] ) @isScanning = true @session.startRunning true end |
#startScanning ⇒ Object
42 43 44 45 |
# File 'lib/tgios/scan_qr_controller.rb', line 42 def startScanning @load_view.stop_loading setupCapture end |
#stop_scanning ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/tgios/scan_qr_controller.rb', line 138 def stop_scanning NSObject.cancelPreviousPerformRequestsWithTarget(self) if @isScanning @isScanning = false @session.stopRunning @previewLayer.removeFromSuperlayer @previewLayer = nil @session = nil end self..leftBarButtonItem = nil end |
#viewDidLoad ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/tgios/scan_qr_controller.rb', line 5 def viewDidLoad super self.view.backgroundColor = :dark_gray.uicolor @load_view = Tgios::LoadingView.add_loading_view_to(self.view) @load_view.start_loading if NSProcessInfo.processInfo.environment[:IPHONE_SIMULATOR_DEVICE].present? self.performSelector('fake_scan', withObject: nil, afterDelay: 3) else # self.performSelector('startScanning', withObject: nil, afterDelay: 0.5) end end |