Skip to content
This repository was archived by the owner on Jun 16, 2023. It is now read-only.

Commit dcbbbf4

Browse files
feat(ios): Return the image containing barcodes (iOS only) (#2864)
* Return the image containing barcodes * Update RNCamera.md Co-authored-by: Manish Kumar <mkumar@acvauctions.com>
1 parent c434051 commit dcbbbf4

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

docs/RNCamera.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@ Event contains the following fields
464464

465465
- `data` - a textual representation of the barcode, if available
466466
- `rawData` - The raw data encoded in the barcode, if available
467+
- `uri`: (iOS only) string representing the path to the image saved on your app's cache directory. Applicable only for `onGoogleVisionBarcodesDetected`.
467468
- `type` - the type of the barcode detected
468469
- `bounds` -
469470

ios/RN/BarcodeDetectorManagerMlkit.m

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#import "BarcodeDetectorManagerMlkit.h"
22
#import <React/RCTConvert.h>
3+
#import "RNFileSystem.h"
34
#if __has_include(<FirebaseMLVision/FirebaseMLVision.h>)
45

56
@interface BarcodeDetectorManagerMlkit ()
@@ -13,7 +14,7 @@ @interface BarcodeDetectorManagerMlkit ()
1314

1415
@implementation BarcodeDetectorManagerMlkit
1516

16-
- (instancetype)init
17+
- (instancetype)init
1718
{
1819
if (self = [super init]) {
1920
self.vision = [FIRVision vision];
@@ -22,7 +23,7 @@ - (instancetype)init
2223
return self;
2324
}
2425

25-
- (BOOL)isRealDetector
26+
- (BOOL)isRealDetector
2627
{
2728
return true;
2829
}
@@ -52,7 +53,7 @@ + (NSDictionary *)constants
5253
};
5354
}
5455

55-
- (void)setType:(id)json queue:(dispatch_queue_t)sessionQueue
56+
- (void)setType:(id)json queue:(dispatch_queue_t)sessionQueue
5657
{
5758
NSInteger requestedValue = [RCTConvert NSInteger:json];
5859
if (self.setOption != requestedValue) {
@@ -78,7 +79,7 @@ -(void)setMode:(id)json queue:(dispatch_queue_t)sessionQueue
7879
- (void)findBarcodesInFrame:(UIImage *)uiImage
7980
scaleX:(float)scaleX
8081
scaleY:(float)scaleY
81-
completed:(void (^)(NSArray *result))completed
82+
completed:(void (^)(NSArray *result))completed
8283
{
8384
self.scaleX = scaleX;
8485
self.scaleY = scaleY;
@@ -89,12 +90,12 @@ - (void)findBarcodesInFrame:(UIImage *)uiImage
8990
if (error != nil || barcodes == nil) {
9091
completed(emptyResult);
9192
} else {
92-
completed([self processBarcodes:barcodes]);
93+
completed([self processBarcodes:barcodes imageContainingBarcodes:uiImage]);
9394
}
9495
}];
9596
}
9697

97-
- (NSArray *)processBarcodes:(NSArray *)barcodes
98+
- (NSArray *)processBarcodes:(NSArray *)barcodes imageContainingBarcodes:(UIImage *)imageContainingBarcodes
9899
{
99100
NSMutableArray *result = [[NSMutableArray alloc] init];
100101
for (FIRVisionBarcode *barcode in barcodes) {
@@ -103,15 +104,20 @@ - (NSArray *)processBarcodes:(NSArray *)barcodes
103104
// Boundaries of a barcode in image
104105
NSDictionary *bounds = [self processBounds:barcode.frame];
105106
[resultDict setObject:bounds forKey:@"bounds"];
106-
107+
107108
// TODO send points to javascript - implement on android at the same time
108109
// Point[] corners = barcode.getCornerPoints();
109-
110+
110111
NSString *rawValue = barcode.rawValue;
111112
NSString *displayValue = barcode.displayValue;
112113
[resultDict setObject:rawValue forKey:@"dataRaw"];
113114
[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+
115121
FIRVisionBarcodeValueType valueType = barcode.valueType;
116122
[resultDict setObject:[self getType:barcode.valueType] forKey:@"type"];
117123

@@ -136,7 +142,6 @@ - (NSArray *)processBarcodes:(NSArray *)barcodes
136142
break;
137143
}
138144
[resultDict setObject:encryptionTypeString forKey:@"encryptionType"];
139-
140145
}
141146
break;
142147
case FIRVisionBarcodeValueTypeURL:
@@ -177,7 +182,6 @@ - (NSArray *)processBarcodes:(NSArray *)barcodes
177182
[phones addObject:[self processPhone:phone]];
178183
}
179184
[resultDict setObject:phones forKey:@"phones"];
180-
181185
}
182186
if(barcode.contactInfo.urls) {[resultDict setObject:barcode.contactInfo.urls forKey:@"urls"]; }
183187
if(barcode.contactInfo.organization) {[resultDict setObject:barcode.contactInfo.organization forKey:@"organization"]; }
@@ -370,7 +374,7 @@ - (NSString *)processDate:(NSDate *)date
370374
return [dateFormatter stringFromDate:date];
371375
}
372376

373-
- (NSDictionary *)processBounds:(CGRect)bounds
377+
- (NSDictionary *)processBounds:(CGRect)bounds
374378
{
375379
float width = bounds.size.width * _scaleX;
376380
float height = bounds.size.height * _scaleY;
@@ -384,12 +388,11 @@ - (NSDictionary *)processBounds:(CGRect)bounds
384388
}
385389

386390

387-
- (NSDictionary *)processPoint:(FIRVisionPoint *)point
391+
- (NSDictionary *)processPoint:(FIRVisionPoint *)point
388392
{
389393
float originX = [point.x floatValue] * _scaleX;
390394
float originY = [point.y floatValue] * _scaleY;
391395
NSDictionary *pointDict = @{
392-
393396
@"x" : @(originX),
394397
@"y" : @(originY)
395398
};
@@ -449,3 +452,4 @@ - (void)setType:(id)json queue:(dispatch_queue_t)sessionQueue
449452

450453
@end
451454
#endif
455+

0 commit comments

Comments
 (0)