@@ -24,6 +24,41 @@ class MainViewController: UIViewController {
2424 private var sectionsData : [ CustomizationSection ] = [ ]
2525 private var pickerImages : [ UIImage ] = [ ]
2626
27+ var isCustomUILayerEnabled : Bool = false
28+ lazy var animationTimer = Timer . scheduledTimer ( timeInterval: 1.0 / 60 , target: self , selector: #selector( fireTimer) , userInfo: nil , repeats: true )
29+
30+ // JSON string for custom UI layer
31+ let customLayerJsonString =
32+ """
33+ {
34+ " objects " : [
35+ {
36+ " label " : {
37+ " text " : " Searching document... " ,
38+ " fontStyle " : " normal " ,
39+ " fontColor " : " #FF444444 " ,
40+ " fontSize " : 24,
41+ " alignment " : " center " ,
42+ " background " : " #BBDDDDDD " ,
43+ " borderRadius " : 10,
44+ " padding " : {
45+ " start " : 20,
46+ " end " : 20,
47+ " top " : 20,
48+ " bottom " : 20
49+ },
50+ " position " : {
51+ " v " : 1.0
52+ },
53+ " margin " : {
54+ " start " : 24,
55+ " end " : 24
56+ }
57+ }
58+ }]
59+ }
60+ """
61+
2762 override func viewDidLoad( ) {
2863 super. viewDidLoad ( )
2964
@@ -90,7 +125,12 @@ class MainViewController: UIViewController {
90125 defaultScanner. resetFunctionality = false
91126 let stillImage = CustomizationItem ( " Gallery (recognizeImages) " )
92127 stillImage. actionType = . gallery
93- let defaultSection = CustomizationSection ( " Default " , [ defaultScanner, stillImage] )
128+ let recognizeImageInput = CustomizationItem ( " Recognize images with light type " ) { [ weak self] in
129+ guard let self = self else { return }
130+ self . recognizeImagesWithImageInput ( )
131+ }
132+ recognizeImageInput. actionType = . custom
133+ let defaultSection = CustomizationSection ( " Default " , [ defaultScanner, stillImage, recognizeImageInput] )
94134 sectionsData. append ( defaultSection)
95135
96136 // 2. Custom modes
@@ -110,8 +150,8 @@ class MainViewController: UIViewController {
110150 }
111151 manualMultipageMode. resetFunctionality = false
112152 manualMultipageMode. actionType = . custom
113- let childModeSection = CustomizationSection ( " Custom " , [ childModeScanner, manualMultipageMode, onlineProcessing] )
114- sectionsData. append ( childModeSection )
153+ let customModedSection = CustomizationSection ( " Custom " , [ childModeScanner, manualMultipageMode, onlineProcessing] )
154+ sectionsData. append ( customModedSection )
115155
116156 // 3. Custom camera frame
117157 let customBorderWidth = CustomizationItem ( " Custom border width " ) { ( ) -> ( Void ) in
@@ -234,7 +274,17 @@ class MainViewController: UIViewController {
234274 DocReader . shared. customization. customStatusPositionMultiplier = 0.5
235275 }
236276
237- let freeCustomStatusItems = [ freeCustomTextAndPostion]
277+ let customUILayerModeStatic = CustomizationItem ( " Custom Status & Image " ) { [ weak self] in
278+ guard let self = self else { return }
279+ self . setupCustomUIFromFile ( )
280+ }
281+ let customUILayerModeAnimated = CustomizationItem ( " Custom Status Animated " ) { [ weak self] in
282+ guard let self = self else { return }
283+ self . isCustomUILayerEnabled = true
284+ self . animationTimer. fire ( )
285+ }
286+
287+ let freeCustomStatusItems = [ freeCustomTextAndPostion, customUILayerModeStatic, customUILayerModeAnimated]
238288 let freeCustomStatusSection = CustomizationSection ( " Free custom status " , freeCustomStatusItems)
239289 sectionsData. append ( freeCustomStatusSection)
240290
@@ -303,7 +353,7 @@ class MainViewController: UIViewController {
303353 case . cancel:
304354 print ( " Cancelled by user " )
305355 DocReader . shared. functionality. manualMultipageMode = false
306- case . complete:
356+ case . complete, . processTimeout :
307357 guard let results = result else {
308358 return
309359 }
@@ -328,6 +378,42 @@ class MainViewController: UIViewController {
328378 }
329379 }
330380
381+ private func recognizeImagesWithImageInput( ) {
382+ let whiteImage = UIImage ( named: " white.bmp " )
383+ let uvImage = UIImage ( named: " uv.bmp " )
384+ let irImage = UIImage ( named: " ir.bmp " )
385+
386+ let whiteInput = DocReader . ImageInput ( image: whiteImage!, light: . white, pageIndex: 0 )
387+ let uvInput = DocReader . ImageInput ( image: uvImage!, light: . UV, pageIndex: 0 )
388+ let irInput = DocReader . ImageInput ( image: irImage!, light: . infrared, pageIndex: 0 )
389+
390+ DocReader . shared. recognizeImages ( with: [ whiteInput, irInput, uvInput] ) { action, results, error in
391+ switch action {
392+ case . cancel:
393+ self . stopCustomUIChanges ( )
394+ print ( " Cancelled by user " )
395+ case . complete, . processTimeout:
396+ self . stopCustomUIChanges ( )
397+ guard let opticalResults = results else {
398+ return
399+ }
400+ self . showResultScreen ( opticalResults)
401+ case . error:
402+ self . stopCustomUIChanges ( )
403+ print ( " Error " )
404+ guard let error = error else { return }
405+ print ( " Error string: \( error) " )
406+ case . process:
407+ guard let result = results else { return }
408+ print ( " Scaning not finished. Result: \( result) " )
409+ case . morePagesAvailable:
410+ print ( " This status couldn't be here, it uses for -recognizeImage function " )
411+ default :
412+ break
413+ }
414+ }
415+ }
416+
331417 private func showAsChildViewController( ) {
332418 let mainStoryboard = UIStoryboard ( name: kMainStoryboardId, bundle: nil )
333419 guard let childViewController = mainStoryboard. instantiateViewController ( withIdentifier: kChildViewController) as? ChildViewController else {
@@ -448,10 +534,13 @@ class MainViewController: UIViewController {
448534 private func showCameraViewController( ) {
449535 DocReader . shared. showScanner ( self ) { [ weak self] ( action, result, error) in
450536 guard let self = self else { return }
537+
451538 switch action {
452539 case . cancel:
540+ self . stopCustomUIChanges ( )
453541 print ( " Cancelled by user " )
454- case . complete:
542+ case . complete, . processTimeout:
543+ self . stopCustomUIChanges ( )
455544 guard let opticalResults = result else {
456545 return
457546 }
@@ -461,6 +550,7 @@ class MainViewController: UIViewController {
461550 self . showResultScreen ( opticalResults)
462551 }
463552 case . error:
553+ self . stopCustomUIChanges ( )
464554 print ( " Error " )
465555 guard let error = error else { return }
466556 print ( " Error string: \( error) " )
@@ -636,6 +726,69 @@ class MainViewController: UIViewController {
636726
637727 return paCertificates
638728 }
729+
730+ private func setupCustomUIFromFile( ) {
731+ if let path = Bundle . main. path ( forResource: " layer " , ofType: " json " ) {
732+ do {
733+ let data = try Data ( contentsOf: URL ( fileURLWithPath: path) , options: . mappedIfSafe)
734+ let jsonDict = try JSONSerialization . jsonObject ( with: data, options: [ ] ) as? [ String : AnyObject ]
735+ DocReader . shared. customization. customUILayerJSON = jsonDict
736+ } catch {
737+
738+ }
739+ }
740+ }
741+
742+ @objc func fireTimer( ) {
743+ /*
744+ This example with Timer shows how you can change different properties of elements at runtime,
745+ such as text, position, color etc. Little bit overhead, but it show how it can changed over time.
746+ Also you can extend model with you properties such like ID, and access by it in runtime.
747+ If you don't need that kind of flexibility, you can just use a static JSON file.
748+ */
749+ guard isCustomUILayerEnabled else {
750+ return
751+ }
752+
753+ guard let jsonData = customLayerJsonString. data ( using: . utf8) else {
754+ return
755+ }
756+
757+ guard var model = try ? JSONDecoder ( ) . decode ( CustomUILayerModel . self, from: jsonData) else {
758+ return
759+ }
760+
761+ guard var object = model. objects. first else {
762+ return
763+ }
764+
765+ let date = Date ( )
766+ let dateFormatter = DateFormatter ( )
767+ dateFormatter. dateFormat = " HH:mm:ss "
768+ object. label. text = " Custom label that showing current time: \( dateFormatter. string ( from: date) ) "
769+
770+ let ct = CACurrentMediaTime ( )
771+ object. label. position. v = 1.0 + sin( ct) * 0.5 // Move vertically from 0.5 to 1.5
772+
773+ let encoder = JSONEncoder ( )
774+ encoder. outputFormatting = . prettyPrinted
775+
776+ model. objects = [ object]
777+
778+ do {
779+ let data = try encoder. encode ( model)
780+ if let jsonDict = try JSONSerialization . jsonObject ( with: data, options: [ ] ) as? [ String : Any ] {
781+ DocReader . shared. customization. customUILayerJSON = jsonDict
782+ }
783+ } catch {
784+
785+ }
786+ }
787+
788+ private func stopCustomUIChanges( ) {
789+ isCustomUILayerEnabled = false
790+ DocReader . shared. customization. customUILayerJSON = nil
791+ }
639792}
640793
641794// MARK: - UITableViewDataSource & UITableViewDelegate
0 commit comments