1- import DynamsoftCaptureVisionRouter
2-
3- import DynamsoftCore
4-
5- import DynamsoftDocumentNormalizer
6-
7- import DynamsoftLicense
8-
9- import DynamsoftUtility
1+ import DynamsoftCaptureVisionBundle
102
113import Flutter
124
@@ -26,96 +18,101 @@ public class SwiftFlutterDocumentScanSdkPlugin: NSObject, FlutterPlugin, License
2618
2719 public func createNormalizedImage( _ result: CapturedResult ) -> NSMutableDictionary {
2820 let dictionary = NSMutableDictionary ( )
29-
30- if let item = result. items? . first, item. type == . normalizedImage {
31- let imageItem : NormalizedImageResultItem = item as! NormalizedImageResultItem
32- let imageData = imageItem. imageData
33- let width = imageData!. width
34- let height = imageData!. height
35- let stride = imageData!. stride
36- let format = imageData!. format
37- let data = imageData!. bytes
38- let length = data. count
39- let orientation = imageData!. orientation
40-
41- dictionary. setObject ( width, forKey: " width " as NSCopying )
42- dictionary. setObject ( height, forKey: " height " as NSCopying )
43- dictionary. setObject ( stride, forKey: " stride " as NSCopying )
44- dictionary. setObject ( format. rawValue, forKey: " format " as NSCopying )
45- dictionary. setObject ( orientation, forKey: " orientation " as NSCopying )
46- dictionary. setObject ( length, forKey: " length " as NSCopying )
47-
48- var rgba : [ UInt8 ] = [ UInt8] ( repeating: 0 , count: Int ( width * height) * 4 )
49-
50- if format == ImagePixelFormat . RGB888 {
51- var dataIndex = 0
52- for i in 0 ..< height {
53- for j in 0 ..< width {
54- let index = i * width + j
55- rgba [ Int ( index) * 4 ] = data [ dataIndex] // red
56- rgba [ Int ( index) * 4 + 1 ] = data [ dataIndex + 1 ] // green
57- rgba [ Int ( index) * 4 + 2 ] = data [ dataIndex + 2 ] // blue
58- rgba [ Int ( index) * 4 + 3 ] = 255 // alpha
59- dataIndex += 3
60- }
61- }
62- } else if format == ImagePixelFormat . grayScaled || format == ImagePixelFormat . binaryInverted || format == ImagePixelFormat . binary8 {
63- var dataIndex = 0
64- for i in 0 ..< height {
65- for j in 0 ..< width {
66- let index = i * width + j
67- rgba [ Int ( index) * 4 ] = data [ dataIndex]
68- rgba [ Int ( index) * 4 + 1 ] = data [ dataIndex]
69- rgba [ Int ( index) * 4 + 2 ] = data [ dataIndex]
70- rgba [ Int ( index) * 4 + 3 ] = 255
71- dataIndex += 1
72- }
73- }
74- } else if format == ImagePixelFormat . binary {
75- var grayscale : [ UInt8 ] = [ UInt8] ( repeating: 0 , count: Int ( width * height) )
76-
77- var index = 0
78- let skip = stride * 8 - width
79- var shift = 0
80- var n = 1
81-
82- for i in 0 ..< length {
83- let b = data [ i]
84- var byteCount = 7
85- while byteCount >= 0 {
86- let tmp = ( b & ( 1 << byteCount) ) >> byteCount
87-
88- if shift < stride * 8 * UInt( n) - skip {
89- if tmp == 1 {
90- grayscale [ index] = 255
91- } else {
92- grayscale [ index] = 0
21+
22+ if let items = result. items {
23+ for item in items {
24+ if item. type == . enhancedImage {
25+ let imageItem : EnhancedImageResultItem = item as! EnhancedImageResultItem
26+ let imageData = imageItem. imageData
27+ let width = imageData!. width
28+ let height = imageData!. height
29+ let stride = imageData!. stride
30+ let format = imageData!. format
31+ let data = imageData!. bytes
32+ let length = data. count
33+ let orientation = imageData!. orientation
34+
35+ dictionary. setObject ( width, forKey: " width " as NSCopying )
36+ dictionary. setObject ( height, forKey: " height " as NSCopying )
37+ dictionary. setObject ( stride, forKey: " stride " as NSCopying )
38+ dictionary. setObject ( format. rawValue, forKey: " format " as NSCopying )
39+ dictionary. setObject ( orientation, forKey: " orientation " as NSCopying )
40+ dictionary. setObject ( length, forKey: " length " as NSCopying )
41+
42+ var rgba : [ UInt8 ] = [ UInt8] ( repeating: 0 , count: Int ( width * height) * 4 )
43+
44+ if format == ImagePixelFormat . RGB888 {
45+ var dataIndex = 0
46+ for i in 0 ..< height {
47+ for j in 0 ..< width {
48+ let index = i * width + j
49+ rgba [ Int ( index) * 4 ] = data [ dataIndex] // red
50+ rgba [ Int ( index) * 4 + 1 ] = data [ dataIndex + 1 ] // green
51+ rgba [ Int ( index) * 4 + 2 ] = data [ dataIndex + 2 ] // blue
52+ rgba [ Int ( index) * 4 + 3 ] = 255 // alpha
53+ dataIndex += 3
9354 }
94- index += 1
9555 }
56+ } else if format == ImagePixelFormat . grayScaled || format == ImagePixelFormat . binaryInverted || format == ImagePixelFormat . binary8 {
57+ var dataIndex = 0
58+ for i in 0 ..< height {
59+ for j in 0 ..< width {
60+ let index = i * width + j
61+ rgba [ Int ( index) * 4 ] = data [ dataIndex]
62+ rgba [ Int ( index) * 4 + 1 ] = data [ dataIndex]
63+ rgba [ Int ( index) * 4 + 2 ] = data [ dataIndex]
64+ rgba [ Int ( index) * 4 + 3 ] = 255
65+ dataIndex += 1
66+ }
67+ }
68+ } else if format == ImagePixelFormat . binary {
69+ var grayscale : [ UInt8 ] = [ UInt8] ( repeating: 0 , count: Int ( width * height) )
70+
71+ var index = 0
72+ let skip = stride * 8 - width
73+ var shift = 0
74+ var n = 1
75+
76+ for i in 0 ..< length {
77+ let b = data [ i]
78+ var byteCount = 7
79+ while byteCount >= 0 {
80+ let tmp = ( b & ( 1 << byteCount) ) >> byteCount
81+
82+ if shift < stride * 8 * UInt( n) - skip {
83+ if tmp == 1 {
84+ grayscale [ index] = 255
85+ } else {
86+ grayscale [ index] = 0
87+ }
88+ index += 1
89+ }
90+
91+ byteCount -= 1
92+ shift += 1
93+ }
9694
97- byteCount -= 1
98- shift += 1
99- }
100-
101- if shift == Int ( stride) * 8 * n {
102- n += 1
103- }
104- }
95+ if shift == Int ( stride) * 8 * n {
96+ n += 1
97+ }
98+ }
10599
106- var dataIndex = 0
107- for i in 0 ..< height {
108- for j in 0 ..< width {
109- let index = i * width + j
110- rgba [ Int ( index) * 4 ] = grayscale [ dataIndex]
111- rgba [ Int ( index) * 4 + 1 ] = grayscale [ dataIndex]
112- rgba [ Int ( index) * 4 + 2 ] = grayscale [ dataIndex]
113- rgba [ Int ( index) * 4 + 3 ] = 255
114- dataIndex += 1
100+ var dataIndex = 0
101+ for i in 0 ..< height {
102+ for j in 0 ..< width {
103+ let index = i * width + j
104+ rgba [ Int ( index) * 4 ] = grayscale [ dataIndex]
105+ rgba [ Int ( index) * 4 + 1 ] = grayscale [ dataIndex]
106+ rgba [ Int ( index) * 4 + 2 ] = grayscale [ dataIndex]
107+ rgba [ Int ( index) * 4 + 3 ] = 255
108+ dataIndex += 1
109+ }
110+ }
115111 }
112+ dictionary. setObject ( rgba, forKey: " data " as NSCopying )
116113 }
114+
117115 }
118- dictionary. setObject ( rgba, forKey: " data " as NSCopying )
119116 }
120117
121118 return dictionary
@@ -161,7 +158,7 @@ public class SwiftFlutterDocumentScanSdkPlugin: NSObject, FlutterPlugin, License
161158 result ( ret)
162159
163160 case " getParameters " :
164- result ( try ? cvr. outputSettings ( " " ) )
161+ result ( try ? cvr. outputSettings ( " " , includeDefaultValues : false ) )
165162 case " detectFile " :
166163 let arguments : NSDictionary = call. arguments as! NSDictionary
167164 DispatchQueue . global ( ) . async {
@@ -226,10 +223,12 @@ public class SwiftFlutterDocumentScanSdkPlugin: NSObject, FlutterPlugin, License
226223 imageData. orientation = rotation
227224
228225 let points = [
229- CGPoint ( x: x1, y: y1) , CGPoint ( x: x2, y: y2) , CGPoint ( x : x3 , y : y3 ) ,
230- CGPoint ( x: x4, y: y4) ,
226+ CGPoint ( x: x1, y: y1) , CGPoint ( x: x2, y: y2) ,
227+ CGPoint ( x: x3 , y : y3 ) , CGPoint ( x : x4, y: y4)
231228 ]
232- let quad = Quadrilateral . init ( pointArray: points)
229+
230+ let nsPoints = points. map { NSValue ( cgPoint: $0) }
231+ let quad = Quadrilateral ( pointArray: nsPoints)
233232
234233 var mode = ImageColourMode . colour
235234
@@ -268,11 +267,13 @@ public class SwiftFlutterDocumentScanSdkPlugin: NSObject, FlutterPlugin, License
268267 let colorMode : Int = arguments. value ( forKey: " color " ) as! Int
269268
270269 let points = [
271- CGPoint ( x: x1, y: y1) , CGPoint ( x: x2, y: y2) , CGPoint ( x : x3 , y : y3 ) ,
272- CGPoint ( x: x4, y: y4) ,
270+ CGPoint ( x: x1, y: y1) , CGPoint ( x: x2, y: y2) ,
271+ CGPoint ( x: x3 , y : y3 ) , CGPoint ( x : x4, y: y4)
273272 ]
274- let quad = Quadrilateral . init ( pointArray: points)
275273
274+ let nsPoints = points. map { NSValue ( cgPoint: $0) }
275+ let quad = Quadrilateral ( pointArray: nsPoints)
276+
276277 var mode = ImageColourMode . colour
277278
278279 switch colorMode {
0 commit comments