-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQupid.mm
143 lines (113 loc) · 5.31 KB
/
Qupid.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#import "Qupid.h"
@implementation Qupid
RCT_EXPORT_MODULE()
// Don't compile this code when we build for the old architecture.
#ifdef RCT_NEW_ARCH_ENABLED
- (NSString *)convertFeatureToJSON:(CIQRCodeFeature *)result {
NSDictionary *jsonDict = @{
@"data": result.messageString,
@"x": @(result.topLeft.x),
@"y": @(result.topLeft.y),
@"width": @(result.topRight.x - result.topLeft.x),
@"height": @(result.bottomLeft.y - result.topLeft.y)
};
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:NSJSONWritingPrettyPrinted error:&error];
if (error) {
NSLog(@"Error converting ZXIResult to JSON: %@", error);
return @"";
} else {
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
return jsonString;
}
}
- (NSString *)removeFilePrefixFromString:(NSString *)filePath {
if ([filePath hasPrefix:@"file://"]) {
// Remove "file://" prefix
NSString *cleanPath = [filePath substringFromIndex:7];
return cleanPath;
}
// If the prefix is not present, return the original path
return filePath;
}
-(NSArray *)detectQRCode:(CIImage *) image {
@autoreleasepool {
NSDictionary* options;
CIContext* context = [CIContext context];
// options = @{ CIDetectorAccuracy : CIDetectorAccuracyHigh }; // Slow but thorough
options = @{ CIDetectorAccuracy : CIDetectorAccuracyLow}; // Fast but superficial
CIDetector* qrDetector = [CIDetector detectorOfType:CIDetectorTypeQRCode
context:context
options:options];
if ([[image properties] valueForKey:(NSString*) kCGImagePropertyOrientation] == nil) {
options = @{ CIDetectorImageOrientation : @1};
} else {
options = @{ CIDetectorImageOrientation : [[image properties] valueForKey:(NSString*) kCGImagePropertyOrientation]};
}
NSArray * features = [qrDetector featuresInImage:image
options:options];
return features;
}
}
- (NSString *)processImage:(CIImage *)image {
NSArray* features = [self detectQRCode:image];
if (features != nil && features.count > 0) {
NSMutableString *combinedText = [NSMutableString string];
[combinedText appendString:@"["]; // Start of JSON array
for (CIQRCodeFeature* qrFeature in features) {
NSString *toJson = [self convertFeatureToJSON:qrFeature];
[combinedText appendString:toJson];
[combinedText appendString:@","]; // Add comma to separate JSON objects
}
// Remove the last comma
NSRange lastCommaRange = [combinedText rangeOfString:@"," options:NSBackwardsSearch];
if (lastCommaRange.location != NSNotFound) {
[combinedText deleteCharactersInRange:lastCommaRange];
}
[combinedText appendString:@"]"]; // End of JSON array
return [combinedText copy];
} else {
return @"[]";
}
}
- (void)readImage:(NSString *)filePath inputSubSample:(double)inputSubSample resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
NSString *cleanedPath = [self removeFilePrefixFromString:filePath];
NSData *data = [NSData dataWithContentsOfFile:cleanedPath];
CIImage *image = [CIImage imageWithData:data];
// Process the image and resolve or reject the promise based on the result
NSString *resultText = [self processImage:image];
if (resultText != nil) {
resolve(resultText);
} else {
NSError *error = [NSError errorWithDomain:@"BarcodeReadingErrorDomain" code:0 userInfo:@{NSLocalizedDescriptionKey: @"Error reading barcode"}];
reject(@"BarcodeReadingError", @"Error reading barcode", error);
}
}
- (void)readRaw:(NSArray<NSNumber *> *)data resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
// Convert NSArray of integers into NSData
NSMutableData *imageData = [NSMutableData dataWithCapacity:data.count];
for (NSNumber *number in data) {
uint8_t byte = [number unsignedCharValue];
[imageData appendBytes:&byte length:1];
}
CIImage *image = [CIImage imageWithData:imageData];
// Process the image and resolve or reject the promise based on the result
NSString *resultText = [self processImage:image];
if (resultText != nil) {
resolve(resultText);
} else {
NSError *error = [NSError errorWithDomain:@"BarcodeReadingErrorDomain" code:0 userInfo:@{NSLocalizedDescriptionKey: @"Error reading barcode"}];
reject(@"BarcodeReadingError", @"Error reading barcode", error);
}
}
- (void)readVideo:(NSString *)filePath inputSubSample:(double)inputSubSample everyNthFrame:(double)everyNthFrame resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
NSString *res = @"Hellooo";
resolve(res);
}
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params
{
return std::make_shared<facebook::react::NativeQupidSpecJSI>(params);
}
#endif
@end