1
1
#import " BarcodeDetectorManagerMlkit.h"
2
2
#import < React/RCTConvert.h>
3
+ #import " RNFileSystem.h"
3
4
#if __has_include(<FirebaseMLVision/FirebaseMLVision.h>)
4
5
5
6
@interface BarcodeDetectorManagerMlkit ()
@@ -13,7 +14,7 @@ @interface BarcodeDetectorManagerMlkit ()
13
14
14
15
@implementation BarcodeDetectorManagerMlkit
15
16
16
- - (instancetype )init
17
+ - (instancetype )init
17
18
{
18
19
if (self = [super init ]) {
19
20
self.vision = [FIRVision vision ];
@@ -22,7 +23,7 @@ - (instancetype)init
22
23
return self;
23
24
}
24
25
25
- - (BOOL )isRealDetector
26
+ - (BOOL )isRealDetector
26
27
{
27
28
return true ;
28
29
}
@@ -52,7 +53,7 @@ + (NSDictionary *)constants
52
53
};
53
54
}
54
55
55
- - (void )setType : (id )json queue : (dispatch_queue_t )sessionQueue
56
+ - (void )setType : (id )json queue : (dispatch_queue_t )sessionQueue
56
57
{
57
58
NSInteger requestedValue = [RCTConvert NSInteger: json];
58
59
if (self.setOption != requestedValue) {
@@ -78,7 +79,7 @@ -(void)setMode:(id)json queue:(dispatch_queue_t)sessionQueue
78
79
- (void )findBarcodesInFrame : (UIImage *)uiImage
79
80
scaleX : (float )scaleX
80
81
scaleY : (float )scaleY
81
- completed : (void (^)(NSArray *result))completed
82
+ completed : (void (^)(NSArray *result))completed
82
83
{
83
84
self.scaleX = scaleX;
84
85
self.scaleY = scaleY;
@@ -89,12 +90,12 @@ - (void)findBarcodesInFrame:(UIImage *)uiImage
89
90
if (error != nil || barcodes == nil ) {
90
91
completed (emptyResult);
91
92
} else {
92
- completed ([self processBarcodes: barcodes]);
93
+ completed ([self processBarcodes: barcodes imageContainingBarcodes: uiImage ]);
93
94
}
94
95
}];
95
96
}
96
97
97
- - (NSArray *)processBarcodes : (NSArray *)barcodes
98
+ - (NSArray *)processBarcodes : (NSArray *)barcodes imageContainingBarcodes : (UIImage *) imageContainingBarcodes
98
99
{
99
100
NSMutableArray *result = [[NSMutableArray alloc ] init ];
100
101
for (FIRVisionBarcode *barcode in barcodes) {
@@ -103,15 +104,20 @@ - (NSArray *)processBarcodes:(NSArray *)barcodes
103
104
// Boundaries of a barcode in image
104
105
NSDictionary *bounds = [self processBounds: barcode.frame];
105
106
[resultDict setObject: bounds forKey: @" bounds" ];
106
-
107
+
107
108
// TODO send points to javascript - implement on android at the same time
108
109
// Point[] corners = barcode.getCornerPoints();
109
-
110
+
110
111
NSString *rawValue = barcode.rawValue ;
111
112
NSString *displayValue = barcode.displayValue ;
112
113
[resultDict setObject: rawValue forKey: @" dataRaw" ];
113
114
[resultDict setObject: displayValue forKey: @" data" ];
114
-
115
+
116
+ // Store the image to app cache and return the uri
117
+ NSString *path = [RNFileSystem generatePathInDirectory: [[RNFileSystem cacheDirectoryPath ] stringByAppendingPathComponent: @" Camera" ] withExtension: @" .jpg" ];
118
+ [UIImageJPEGRepresentation (imageContainingBarcodes, 1.0 ) writeToFile: path atomically: YES ];
119
+ [resultDict setObject: path forKey: @" uri" ];
120
+
115
121
FIRVisionBarcodeValueType valueType = barcode.valueType ;
116
122
[resultDict setObject: [self getType: barcode.valueType] forKey: @" type" ];
117
123
@@ -136,7 +142,6 @@ - (NSArray *)processBarcodes:(NSArray *)barcodes
136
142
break ;
137
143
}
138
144
[resultDict setObject: encryptionTypeString forKey: @" encryptionType" ];
139
-
140
145
}
141
146
break ;
142
147
case FIRVisionBarcodeValueTypeURL:
@@ -177,7 +182,6 @@ - (NSArray *)processBarcodes:(NSArray *)barcodes
177
182
[phones addObject: [self processPhone: phone]];
178
183
}
179
184
[resultDict setObject: phones forKey: @" phones" ];
180
-
181
185
}
182
186
if (barcode.contactInfo .urls ) {[resultDict setObject: barcode.contactInfo.urls forKey: @" urls" ]; }
183
187
if (barcode.contactInfo .organization ) {[resultDict setObject: barcode.contactInfo.organization forKey: @" organization" ]; }
@@ -370,7 +374,7 @@ - (NSString *)processDate:(NSDate *)date
370
374
return [dateFormatter stringFromDate: date];
371
375
}
372
376
373
- - (NSDictionary *)processBounds : (CGRect)bounds
377
+ - (NSDictionary *)processBounds : (CGRect)bounds
374
378
{
375
379
float width = bounds.size .width * _scaleX;
376
380
float height = bounds.size .height * _scaleY;
@@ -384,12 +388,11 @@ - (NSDictionary *)processBounds:(CGRect)bounds
384
388
}
385
389
386
390
387
- - (NSDictionary *)processPoint : (FIRVisionPoint *)point
391
+ - (NSDictionary *)processPoint : (FIRVisionPoint *)point
388
392
{
389
393
float originX = [point.x floatValue ] * _scaleX;
390
394
float originY = [point.y floatValue ] * _scaleY;
391
395
NSDictionary *pointDict = @{
392
-
393
396
@" x" : @(originX),
394
397
@" y" : @(originY)
395
398
};
@@ -449,3 +452,4 @@ - (void)setType:(id)json queue:(dispatch_queue_t)sessionQueue
449
452
450
453
@end
451
454
#endif
455
+
0 commit comments