Class: Tgios::ScanQrController
- Inherits:
-
UIViewController
- Object
- UIViewController
- Tgios::ScanQrController
- Defined in:
- lib/tgios/scan_qr_controller.rb
Instance Attribute Summary collapse
-
#bottom_text ⇒ Object
Returns the value of attribute bottom_text.
-
#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
#bottom_text ⇒ Object
Returns the value of attribute bottom_text.
3 4 5 |
# File 'lib/tgios/scan_qr_controller.rb', line 3 def bottom_text @bottom_text end |
#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
147 148 149 150 151 152 153 154 155 |
# File 'lib/tgios/scan_qr_controller.rb', line 147 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
203 204 205 206 |
# File 'lib/tgios/scan_qr_controller.rb', line 203 def dealloc ap "#{self.class.name} dealloc" super end |
#fake_scan ⇒ Object
182 183 184 185 186 187 188 189 190 |
# File 'lib/tgios/scan_qr_controller.rb', line 182 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
197 198 199 200 201 |
# File 'lib/tgios/scan_qr_controller.rb', line 197 def init super @events={} self end |
#on(event, &block) ⇒ Object
192 193 194 195 |
# File 'lib/tgios/scan_qr_controller.rb', line 192 def on(event, &block) @events[event] = block.weak! self end |
#openQRCode(result) ⇒ Object
157 158 159 160 161 162 163 |
# File 'lib/tgios/scan_qr_controller.rb', line 157 def openQRCode(result) stop_scanning self.dismissViewControllerAnimated(true, completion: -> { @events[:result_scanned].call(result) }) 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# 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 if @bottom_text whole_lbl_h = 36 lbl_h = 18 t_frame = layerRect.dup t_frame.origin.y = t_frame.size.height - whole_lbl_h t_frame.size.height = whole_lbl_h bg_layer = Base.style(CALayer.layer, frame: t_frame, backgroundColor: :black.cgcolor(0.3), ) self.view.layer.addSublayer(bg_layer) t_frame.origin.y = (whole_lbl_h - lbl_h)/ 2.0 t_frame.size.height = lbl_h text_layer = Base.style(CATextLayer.layer, contentsScale: UIScreen.mainScreen.scale, frame: t_frame, foregroundColor: :white.cgcolor, alignmentMode: KCAAlignmentCenter, truncationMode: KCATruncationEnd, fontSize: 14, ) bg_layer.addSublayer(text_layer) text_layer.string = @bottom_text end 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
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/tgios/scan_qr_controller.rb', line 165 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 |