diff --git a/packages/react-native/example/src/App.tsx b/packages/react-native/example/src/App.tsx index f88401a..aff1a76 100644 --- a/packages/react-native/example/src/App.tsx +++ b/packages/react-native/example/src/App.tsx @@ -9,7 +9,14 @@ import type { ImageDetails } from './types' async function main() { const ocr = await Ocr.create() - const result = await ocr.detect(`${FileSystem.bundleDirectory}/gutenye-ocr-react-native.bundle/cn-01.jpg`) + const result = await ocr.detect(`${FileSystem.bundleDirectory}/gutenye-ocr-react-native.bundle/cn-01.jpg`, { + number1: 1, + number2: 0, + float1: 1.1, + string: 'a', + boolean1: true, + boolean2: false, + }) console.log('js', result) return result } diff --git a/packages/react-native/ios/RNOcr.h b/packages/react-native/ios/RNOcr.h index 33ae8ba..92ef646 100644 --- a/packages/react-native/ios/RNOcr.h +++ b/packages/react-native/ios/RNOcr.h @@ -1,5 +1,6 @@ #ifdef __cplusplus #include +#include #endif #import @@ -18,7 +19,7 @@ std::string convertNSString(NSString *nsString); -using MapValue = std::variant; +using MapValue = std::variant; std::unordered_map convertNSDictionary(NSDictionary *nsDictionary); NSArray *convertStdVector(const std::vector &stdVector); \ No newline at end of file diff --git a/packages/react-native/ios/RNOcr.mm b/packages/react-native/ios/RNOcr.mm index 8364ab9..5e3a867 100644 --- a/packages/react-native/ios/RNOcr.mm +++ b/packages/react-native/ios/RNOcr.mm @@ -1,10 +1,7 @@ #import "RNOcr.h" #import -#import +#include #import -#import -#import -#import #import "native-ocr.h" @interface RNOcr () { @@ -37,6 +34,29 @@ @implementation RNOcr : (RCTPromiseRejectBlock)reject) { auto imagePath = convertNSString(rawImagePath); auto options = convertNSDictionary(rawOptions); + + NSLog(@"options: %@", rawOptions); + + std::cout << " string: " << std::get(options["string"]) << std::endl; + std::cout << " double: " << std::get(options["float1"]) << std::endl; + std::cout << " number: " << std::get(options["number1"]) << std::endl; + std::cout << " boolean: " << std::get(options["boolean1"]) << std::endl; + + // for (const auto &pair : options) { + // std::visit( + // [](auto &&arg) { + // using T = std::decay_t; + // if constexpr (std::is_same_v) { + // std::cout << "string: " << pair.first << ": " << arg << std::endl; + // } else if constexpr (std::is_same_v) { + // std::cout << "double: " << pair.first << ": " << arg << std::endl; + // } else if constexpr (std::is_same_v) { + // std::cout << "boolean: " << pair.first << ": " << arg << std::endl; + // } + // }, + // pair.second); + // } + auto lines = _ocr->Process(imagePath); NSArray *finalLines = convertStdVector(lines); resolve(finalLines); @@ -66,21 +86,26 @@ @implementation RNOcr if (nsDictionary == nil) { return stdMap; } - - for (NSString *rawKey in nsDictionary) { + for (id rawKey in nsDictionary) { + if (![rawKey isKindOfClass:[NSString class]]) { + continue; + } std::string key = [rawKey UTF8String]; id rawValue = [nsDictionary objectForKey:rawKey]; if ([rawValue isKindOfClass:[NSString class]]) { - std::string value = [((NSString *)rawValue) UTF8String]; - stdMap[key] = value; + stdMap[key] = std::string([rawValue UTF8String]); } else if ([rawValue isKindOfClass:[NSNumber class]]) { - // Use NSNumber to identify if it's a bool - if (strcmp([rawValue objCType], @encode(bool)) == 0) { - bool value = [((NSNumber *)rawValue) boolValue]; - stdMap[key] = value; + if (strcmp([rawValue objCType], @encode(char)) == 0) { + stdMap[key] = (bool)[rawValue boolValue]; } else { - double value = [((NSNumber *)rawValue) doubleValue]; - stdMap[key] = value; + id rawValueString = [rawValue stringValue]; + NSRange range = [rawValueString rangeOfString:@"."]; + NSRange exponentRangeE = [rawValueString rangeOfString:@"e" options:NSCaseInsensitiveSearch]; + if (range.location != NSNotFound || exponentRangeE.location != NSNotFound) { + stdMap[key] = [rawValue doubleValue]; + } else { + stdMap[key] = [rawValue intValue]; + } } } }