Skip to content

Commit

Permalink
convertNSDictionary work
Browse files Browse the repository at this point in the history
  • Loading branch information
gutenye committed May 12, 2024
1 parent 15f6e60 commit 2e897f6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
9 changes: 8 additions & 1 deletion packages/react-native/example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion packages/react-native/ios/RNOcr.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#ifdef __cplusplus
#include <unordered_map>
#include <variant>
#endif

#import <memory>
Expand All @@ -18,7 +19,7 @@

std::string convertNSString(NSString *nsString);

using MapValue = std::variant<std::string, double, bool>;
using MapValue = std::variant<bool, int, double, std::string>;
std::unordered_map<std::string, MapValue> convertNSDictionary(NSDictionary *nsDictionary);

NSArray<NSString *> *convertStdVector(const std::vector<std::string> &stdVector);
53 changes: 39 additions & 14 deletions packages/react-native/ios/RNOcr.mm
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#import "RNOcr.h"
#import <Foundation/Foundation.h>
#import <onnxruntime_cxx_api.h>
#include <cstring>
#import <iostream>
#import <opencv2/opencv.hpp>
#import <string>
#import <vector>
#import "native-ocr.h"

@interface RNOcr () {
Expand Down Expand Up @@ -37,6 +34,29 @@ @implementation RNOcr
: (RCTPromiseRejectBlock)reject) {
auto imagePath = convertNSString(rawImagePath);
auto options = convertNSDictionary(rawOptions);

NSLog(@"options: %@", rawOptions);

std::cout << " string: " << std::get<std::string>(options["string"]) << std::endl;
std::cout << " double: " << std::get<double>(options["float1"]) << std::endl;
std::cout << " number: " << std::get<int>(options["number1"]) << std::endl;
std::cout << " boolean: " << std::get<bool>(options["boolean1"]) << std::endl;

// for (const auto &pair : options) {
// std::visit(
// [](auto &&arg) {
// using T = std::decay_t<decltype(arg)>;
// if constexpr (std::is_same_v<T, std::string>) {
// std::cout << "string: " << pair.first << ": " << arg << std::endl;
// } else if constexpr (std::is_same_v<T, double>) {
// std::cout << "double: " << pair.first << ": " << arg << std::endl;
// } else if constexpr (std::is_same_v<T, bool>) {
// std::cout << "boolean: " << pair.first << ": " << arg << std::endl;
// }
// },
// pair.second);
// }

auto lines = _ocr->Process(imagePath);
NSArray<NSString *> *finalLines = convertStdVector(lines);
resolve(finalLines);
Expand Down Expand Up @@ -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];
}
}
}
}
Expand Down

0 comments on commit 2e897f6

Please sign in to comment.