Skip to content

Commit

Permalink
exposing torch functions (#97)
Browse files Browse the repository at this point in the history
* exposing torch functions

* handling torch level > 1.0

* hastorch and isavailable
  • Loading branch information
jaimejiyepark authored Nov 21, 2019
1 parent 78a29a3 commit b2d0c52
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 35 deletions.
25 changes: 23 additions & 2 deletions CardScan/Classes/ScanBaseViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,28 @@ import Vision
@objc open func showCardNumber(_ number: String, expiry: String?) { }
@objc open func onCameraPermissionDenied(showedPrompt: Bool) { }

//MARK: -Torch Logic
public func toggleTorch() {
self.ocr.scanStats.torchOn = !self.ocr.scanStats.torchOn
self.videoFeed.toggleTorch()
}

public func isTorchOn() -> Bool{
return self.videoFeed.isTorchOn()
}

public func hasTorchAndIsAvailable() -> Bool {
return self.videoFeed.hasTorchAndIsAvailable()
}

public func setTorchLevel(level: Float) {
if 0.0...1.0 ~= level {
self.videoFeed.setTorchLevel(level: level)
} else {
print("Not a valid torch level")
}
}

@objc static public func configure(apiKey: String? = nil) {
if let apiKey = apiKey {
Api.apiKey = apiKey
Expand Down Expand Up @@ -172,7 +189,7 @@ import Vision
self.videoFeed.requestCameraAccess(permissionDelegate: self)
}

public func setupOnViewDidLoad(regionOfInterestLabel: UILabel, blurView: BlurView, previewView: PreviewView, cornerView: CornerView, debugImageView: UIImageView?) {
public func setupOnViewDidLoad(regionOfInterestLabel: UILabel, blurView: BlurView, previewView: PreviewView, cornerView: CornerView, debugImageView: UIImageView?, torchLevel: Float?) {

self.regionOfInterestLabel = regionOfInterestLabel
self.blurView = blurView
Expand All @@ -196,7 +213,11 @@ import Vision

self.videoFeed.pauseSession()
//Apple example app sets up in viewDidLoad: https://developer.apple.com/documentation/avfoundation/cameras_and_media_capture/avcam_building_a_camera_app
self.videoFeed.setup(captureDelegate: self, completion: { success in })
self.videoFeed.setup(captureDelegate: self, completion: { success in
if let level = torchLevel {
self.setTorchLevel(level: level)
}
})
}

override open var shouldAutorotate: Bool {
Expand Down
3 changes: 2 additions & 1 deletion CardScan/Classes/ScanViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ import UIKit
public weak var scanDelegate: ScanDelegate?
@objc public weak var stringDataSource: ScanStringsDataSource?
@objc public var allowSkip = false
public var torchLevel: Float?
public var scanQrCode = false
@objc public var hideBackButtonImage = false
@IBOutlet weak var backButtonImageToTextConstraint: NSLayoutConstraint!
Expand Down Expand Up @@ -272,7 +273,7 @@ import UIKit
}

let debugImageView = self.showDebugImageView ? self.debugImageView : nil
self.setupOnViewDidLoad(regionOfInterestLabel: self.regionOfInterestLabel, blurView: self.blurView, previewView: self.previewView, cornerView: self.cornerView, debugImageView: debugImageView)
self.setupOnViewDidLoad(regionOfInterestLabel: self.regionOfInterestLabel, blurView: self.blurView, previewView: self.previewView, cornerView: self.cornerView, debugImageView: debugImageView, torchLevel: self.torchLevel)
self.startCameraPreview()
}

Expand Down
35 changes: 3 additions & 32 deletions CardScan/Classes/Torch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ struct Torch {
let device: AVCaptureDevice?
var state: State
var lastStateChange: Date
var level: Float

init(device: AVCaptureDevice) {
self.state = .off
Expand All @@ -19,15 +20,15 @@ struct Torch {
} else {
self.device = nil
}
self.level = 1.0
}

mutating func toggle() {
self.state = self.state == .on ? .off : .on

do {
try self.device?.lockForConfiguration()
if self.state == .on {
do { try self.device?.setTorchModeOn(level: 0.5) } catch { print("could not set torch mode on") }
do { try self.device?.setTorchModeOn(level: self.level) } catch { print("could not set torch mode on") }
} else {
self.device?.torchMode = .off
}
Expand All @@ -37,34 +38,4 @@ struct Torch {
}
}

mutating func luma(_ value: Double) {
let duration: Double = self.lastStateChange.timeIntervalSinceNow * -1.0
var newState = self.state
switch (self.state, value, duration) {
case (.off, ..<0.4, 3.0...):
newState = .on
case (.on, _, 20.0...):
newState = .off
default:
newState = self.state
}

if self.state != newState {
self.lastStateChange = Date()
self.state = newState

do {
try self.device?.lockForConfiguration()
if self.state == .on {
do { try self.device?.setTorchModeOn(level: 0.005) } catch { print("could not set torch mode on") }
} else {
self.device?.torchMode = .off
}
self.device?.unlockForConfiguration()
} catch {
print("error setting torch level")
}
}
}

}
16 changes: 16 additions & 0 deletions CardScan/Classes/VideoFeed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,26 @@ class VideoFeed {
videoDevice.unlockForConfiguration()
}

//MARK: -Torch Logic
func toggleTorch() {
self.torch?.toggle()
}

func isTorchOn() -> Bool {
return self.torch?.state == Torch.State.on
}

func hasTorchAndIsAvailable() -> Bool {
let hasTorch = self.torch?.device?.hasTorch ?? false
let isTorchAvailable = self.torch?.device?.isTorchAvailable ?? false
return hasTorch && isTorchAvailable
}

func setTorchLevel(level: Float) {
self.torch?.level = level
}

//MARK: -VC Lifecycle Logic
func willAppear() {
sessionQueue.async {
switch self.setupResult {
Expand Down

0 comments on commit b2d0c52

Please sign in to comment.