Skip to content

Commit 6659b82

Browse files
committed
update the iOS implementation to add the initLicense, initRuntimeSettingsFromString and decodeBase64 functions
1 parent cf90b9a commit 6659b82

File tree

3 files changed

+94
-38
lines changed

3 files changed

+94
-38
lines changed

ios/BarcodeReaderInitializer.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ import DynamsoftBarcodeReader
1010

1111
public class BarcodeReaderInitializer: NSObject, DBRLicenseVerificationListener {
1212

13-
func configurationDBR(license:String) -> DynamsoftBarcodeReader {
14-
var dbr:DynamsoftBarcodeReader
13+
func initLicense(license:String) {
1514
DynamsoftBarcodeReader.initLicense(license, verificationDelegate: self)
16-
dbr = DynamsoftBarcodeReader.init()
17-
return dbr
1815
}
1916

2017
public func dbrLicenseVerificationCallback(_ isSuccess: Bool, error: Error?) {

ios/VisionCameraDBRPlugin.swift

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,15 @@ import DynamsoftBarcodeReader
1111
@objc(VisionCameraDBRPlugin)
1212
public class VisionCameraDBRPlugin: NSObject, FrameProcessorPluginBase {
1313

14-
15-
private static var barcodeReader:DynamsoftBarcodeReader! = nil
1614
private static var mTemplate:String! = nil
15+
private static var mLicense:String! = nil
1716
private static let context = CIContext(options: nil)
1817
@objc
1918
public static func callback(_ frame: Frame!, withArgs args: [Any]!) -> Any! {
2019
let config = getConfig(withArgs: args)
21-
if barcodeReader == nil {
22-
initDBR(config: config)
23-
}
24-
20+
21+
//for compatibility
22+
initLicense(config: config)
2523
updateRuntimeSettingsWithTemplate(config: config)
2624

2725
guard let imageBuffer = CMSampleBufferGetImageBuffer(frame.buffer) else {
@@ -37,24 +35,30 @@ public class VisionCameraDBRPlugin: NSObject, FrameProcessorPluginBase {
3735
var returned_results: [Any] = []
3836
let image = UIImage(cgImage: cgImage)
3937
// code goes here
40-
let results = try? barcodeReader.decodeImage(image)
38+
let results = try? VisionCameraDynamsoftBarcodeReader.dbr.decodeImage(image)
4139
let count = results?.count ?? 0
4240
if count > 0 {
4341
for index in 0..<count {
4442
let tr = results![index]
45-
returned_results.append(wrapResult(result: tr))
43+
returned_results.append(VisionCameraDynamsoftBarcodeReader.wrapResult(result: tr))
4644
}
4745
print("Found barcodes")
4846
}
4947
return returned_results
5048
}
5149

52-
static func initDBR(config: [String:String]!) {
53-
var license = "";
54-
license = config?["license"] ?? "DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ=="
55-
let initializer = BarcodeReaderInitializer();
56-
barcodeReader = initializer.configurationDBR(license: license)
50+
static func initLicense(config: [String:String]!) {
51+
if config?["license"] != nil {
52+
let license = config?["license"] ?? "DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ=="
53+
if license != mLicense {
54+
mLicense = license
55+
let initializer = BarcodeReaderInitializer()
56+
initializer.initLicense(license: license)
57+
}
58+
}
5759
}
60+
61+
5862

5963

6064
static func getConfig(withArgs args: [Any]!) -> [String:String]!{
@@ -79,37 +83,19 @@ public class VisionCameraDBRPlugin: NSObject, FrameProcessorPluginBase {
7983
}
8084

8185
if shouldUpdate {
82-
try? barcodeReader.initRuntimeSettingsWithString(template, conflictMode: EnumConflictMode.overwrite)
86+
try? VisionCameraDynamsoftBarcodeReader.dbr.initRuntimeSettingsWithString(template, conflictMode: EnumConflictMode.overwrite)
8387
mTemplate = template
8488
}
8589

8690
} else {
8791
if mTemplate != nil {
88-
try? barcodeReader.resetRuntimeSettings()
92+
try? VisionCameraDynamsoftBarcodeReader.dbr.resetRuntimeSettings()
8993
mTemplate = nil
9094
}
9195
}
9296
}
9397

94-
static func wrapResult(result: iTextResult) -> Any {
95-
var map: [String: Any] = [:]
96-
97-
map["barcodeText"] = result.barcodeText
98-
map["barcodeFormat"] = result.barcodeFormatString
99-
map["barcodeBytesBase64"] = result.barcodeBytes?.base64EncodedString()
100-
101-
let points = result.localizationResult?.resultPoints as! [CGPoint]
102-
map["x1"] = points[0].x
103-
map["x2"] = points[1].x
104-
map["x3"] = points[2].x
105-
map["x4"] = points[3].x
106-
map["y1"] = points[0].y
107-
map["y2"] = points[1].y
108-
map["y3"] = points[2].y
109-
map["y4"] = points[3].y
110-
111-
return map
112-
}
98+
11399

114100

115101
}

ios/VisionCameraDynamsoftBarcodeReader.swift

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,76 @@
77
//
88

99
import Foundation
10+
import DynamsoftBarcodeReader
11+
12+
@objc(VisionCameraDynamsoftBarcodeReader)
13+
class VisionCameraDynamsoftBarcodeReader: NSObject {
14+
static var dbr:DynamsoftBarcodeReader = DynamsoftBarcodeReader()
15+
16+
@objc(initRuntimeSettingsFromString:withResolver:withRejecter:)
17+
func initRuntimeSettingsFromString(template:String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
18+
do {
19+
try VisionCameraDynamsoftBarcodeReader.dbr.initRuntimeSettingsWithString(template, conflictMode: EnumConflictMode.overwrite)
20+
resolve(true)
21+
}catch {
22+
print("Unexpected error: \(error).")
23+
resolve(false)
24+
}
25+
}
26+
27+
@objc(decodeBase64:withResolver:withRejecter:)
28+
func decodeBase64(base64:String,resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
29+
var returned_results: [Any] = []
30+
let image = VisionCameraDynamsoftBarcodeReader.convertBase64ToImage(base64)
31+
if image != nil {
32+
let results = try? VisionCameraDynamsoftBarcodeReader.dbr.decodeImage(image!)
33+
let count = results?.count ?? 0
34+
if count > 0 {
35+
for index in 0..<count {
36+
let tr = results![index]
37+
returned_results.append(VisionCameraDynamsoftBarcodeReader.wrapResult(result: tr))
38+
}
39+
}
40+
}
41+
resolve(returned_results)
42+
}
43+
44+
@objc(initLicense:withResolver:withRejecter:)
45+
func initLicense(license:String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
46+
let initializer = BarcodeReaderInitializer()
47+
initializer.initLicense(license: license)
48+
resolve(true)
49+
}
50+
51+
static func wrapResult(result: iTextResult) -> Any {
52+
var map: [String: Any] = [:]
53+
54+
map["barcodeText"] = result.barcodeText
55+
map["barcodeFormat"] = result.barcodeFormatString
56+
map["barcodeBytesBase64"] = result.barcodeBytes?.base64EncodedString()
57+
58+
let points = result.localizationResult?.resultPoints as! [CGPoint]
59+
map["x1"] = points[0].x
60+
map["x2"] = points[1].x
61+
map["x3"] = points[2].x
62+
map["x4"] = points[3].x
63+
map["y1"] = points[0].y
64+
map["y2"] = points[1].y
65+
map["y3"] = points[2].y
66+
map["y4"] = points[3].y
67+
68+
return map
69+
}
70+
71+
static public func convertBase64ToImage(_ imageStr:String) ->UIImage?{
72+
if let data: NSData = NSData(base64Encoded: imageStr, options:NSData.Base64DecodingOptions.ignoreUnknownCharacters)
73+
{
74+
if let image: UIImage = UIImage(data: data as Data)
75+
{
76+
return image
77+
}
78+
}
79+
return nil
80+
}
81+
82+
}

0 commit comments

Comments
 (0)