@imagowave/vision-ocr 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/NitroVisionOcr.podspec +32 -0
  2. package/README.md +38 -0
  3. package/android/CMakeLists.txt +31 -0
  4. package/android/build.gradle +143 -0
  5. package/android/fix-prefab.gradle +51 -0
  6. package/android/gradle.properties +5 -0
  7. package/android/src/main/AndroidManifest.xml +2 -0
  8. package/android/src/main/cpp/cpp-adapter.cpp +9 -0
  9. package/android/src/main/java/com/margelo/nitro/visionocr/NitroVisionOcrPackage.kt +18 -0
  10. package/android/src/main/java/com/margelo/nitro/visionocr/VisionOcr.kt +120 -0
  11. package/ios/Bridge.h +8 -0
  12. package/ios/VisionOCR.swift +137 -0
  13. package/nitro.json +19 -0
  14. package/nitrogen/generated/.gitattributes +1 -0
  15. package/nitrogen/generated/android/NitroVisionOcr+autolinking.cmake +81 -0
  16. package/nitrogen/generated/android/NitroVisionOcr+autolinking.gradle +27 -0
  17. package/nitrogen/generated/android/NitroVisionOcrOnLoad.cpp +41 -0
  18. package/nitrogen/generated/android/NitroVisionOcrOnLoad.hpp +34 -0
  19. package/nitrogen/generated/android/c++/JHybridVisionOCRSpec.cpp +87 -0
  20. package/nitrogen/generated/android/c++/JHybridVisionOCRSpec.hpp +63 -0
  21. package/nitrogen/generated/android/c++/JOcrBlock.hpp +91 -0
  22. package/nitrogen/generated/android/c++/JOcrBox.hpp +69 -0
  23. package/nitrogen/generated/android/c++/JOcrLine.hpp +93 -0
  24. package/nitrogen/generated/android/c++/JOcrOptions.hpp +95 -0
  25. package/nitrogen/generated/android/c++/JOcrResult.hpp +89 -0
  26. package/nitrogen/generated/android/c++/JOcrWord.hpp +68 -0
  27. package/nitrogen/generated/android/c++/JRLevel.hpp +58 -0
  28. package/nitrogen/generated/android/kotlin/com/margelo/nitro/visionocr/HybridVisionOCRSpec.kt +55 -0
  29. package/nitrogen/generated/android/kotlin/com/margelo/nitro/visionocr/NitroVisionOcrOnLoad.kt +35 -0
  30. package/nitrogen/generated/android/kotlin/com/margelo/nitro/visionocr/OcrBlock.kt +61 -0
  31. package/nitrogen/generated/android/kotlin/com/margelo/nitro/visionocr/OcrBox.kt +66 -0
  32. package/nitrogen/generated/android/kotlin/com/margelo/nitro/visionocr/OcrLine.kt +66 -0
  33. package/nitrogen/generated/android/kotlin/com/margelo/nitro/visionocr/OcrOptions.kt +71 -0
  34. package/nitrogen/generated/android/kotlin/com/margelo/nitro/visionocr/OcrResult.kt +56 -0
  35. package/nitrogen/generated/android/kotlin/com/margelo/nitro/visionocr/OcrWord.kt +61 -0
  36. package/nitrogen/generated/android/kotlin/com/margelo/nitro/visionocr/RLevel.kt +23 -0
  37. package/nitrogen/generated/ios/NitroVisionOcr+autolinking.rb +62 -0
  38. package/nitrogen/generated/ios/NitroVisionOcr-Swift-Cxx-Bridge.cpp +44 -0
  39. package/nitrogen/generated/ios/NitroVisionOcr-Swift-Cxx-Bridge.hpp +286 -0
  40. package/nitrogen/generated/ios/NitroVisionOcr-Swift-Cxx-Umbrella.hpp +72 -0
  41. package/nitrogen/generated/ios/c++/HybridVisionOCRSpecSwift.cpp +11 -0
  42. package/nitrogen/generated/ios/c++/HybridVisionOCRSpecSwift.hpp +108 -0
  43. package/nitrogen/generated/ios/swift/HybridVisionOCRSpec.swift +56 -0
  44. package/nitrogen/generated/ios/swift/HybridVisionOCRSpec_cxx.swift +149 -0
  45. package/nitrogen/generated/ios/swift/OcrBlock.swift +64 -0
  46. package/nitrogen/generated/ios/swift/OcrBox.swift +44 -0
  47. package/nitrogen/generated/ios/swift/OcrLine.swift +82 -0
  48. package/nitrogen/generated/ios/swift/OcrOptions.swift +113 -0
  49. package/nitrogen/generated/ios/swift/OcrResult.swift +53 -0
  50. package/nitrogen/generated/ios/swift/OcrWord.swift +58 -0
  51. package/nitrogen/generated/ios/swift/RLevel.swift +40 -0
  52. package/nitrogen/generated/shared/c++/HybridVisionOCRSpec.cpp +21 -0
  53. package/nitrogen/generated/shared/c++/HybridVisionOCRSpec.hpp +71 -0
  54. package/nitrogen/generated/shared/c++/OcrBlock.hpp +98 -0
  55. package/nitrogen/generated/shared/c++/OcrBox.hpp +95 -0
  56. package/nitrogen/generated/shared/c++/OcrLine.hpp +102 -0
  57. package/nitrogen/generated/shared/c++/OcrOptions.hpp +103 -0
  58. package/nitrogen/generated/shared/c++/OcrResult.hpp +91 -0
  59. package/nitrogen/generated/shared/c++/OcrWord.hpp +94 -0
  60. package/nitrogen/generated/shared/c++/RLevel.hpp +76 -0
  61. package/package.json +111 -0
  62. package/react-native.config.js +16 -0
  63. package/src/index.ts +21 -0
  64. package/src/specs/VisionOCR.nitro.ts +11 -0
  65. package/src/types.ts +41 -0
@@ -0,0 +1,32 @@
1
+ require "json"
2
+
3
+ package = JSON.parse(File.read(File.join(__dir__, "package.json")))
4
+
5
+ Pod::Spec.new do |s|
6
+ s.name = "NitroVisionOcr"
7
+ s.version = package["version"]
8
+ s.summary = package["description"]
9
+ s.homepage = package["homepage"]
10
+ s.license = package["license"]
11
+ s.authors = package["author"]
12
+
13
+ s.platforms = { :ios => min_ios_version_supported, :visionos => 1.0 }
14
+ s.source = { :git => "https://github.com/mrousavy/nitro.git", :tag => "#{s.version}" }
15
+
16
+ s.source_files = [
17
+ # Implementation (Swift)
18
+ "ios/**/*.{swift}",
19
+ # Autolinking/Registration (Objective-C++)
20
+ "ios/**/*.{m,mm}",
21
+ # Implementation (C++ objects)
22
+ "cpp/**/*.{hpp,cpp}",
23
+ ]
24
+
25
+ load 'nitrogen/generated/ios/NitroVisionOcr+autolinking.rb'
26
+ add_nitrogen_files(s)
27
+
28
+ s.dependency 'React-jsi'
29
+ s.dependency 'React-callinvoker'
30
+ s.dependency 'VisionCamera'
31
+ install_modules_dependencies(s)
32
+ end
package/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # react-native-nitro-template
2
+
3
+ This is a template for Nitro Modules.
4
+
5
+ ## Usage
6
+
7
+ Clone this repo, and change all `$$*$$` names according to your `nitro.json` file.
8
+
9
+ ## Contributing
10
+
11
+ Contribute a change to this template to update it for newer React Native versions.
12
+
13
+ ## Structure
14
+
15
+ - [`android/`](android): All your `android`-specific implementations.
16
+ - [`build.gradle`](android/build.gradle): The gradle build file. This contains four important pieces:
17
+ 1. Standard react-native library boilerplate code
18
+ 2. Configures Kotlin (`apply plugin: 'org.jetbrains.kotlin.android'`)
19
+ 3. Adds all Nitrogen files (`apply from: '.../NitroVisionOcr+autolinking.gradle'`)
20
+ 4. Triggers the native C++ build (via CMake/`externalNativeBuild`)
21
+ - [`CMakeLists.txt`](android/CMakeLists.txt): The CMake build file to build C++ code. This contains four important pieces:
22
+ 1. Creates a library called `NitroVisionOcr` (same as in `nitro.json`)
23
+ 2. Adds all Nitrogen files (`include(.../NitroVisionOcr+autolinking.cmake)`)
24
+ 3. Adds all custom C++ files (only `HybridTestObjectCpp.cpp`)
25
+ 4. Adds a `cpp-adapter.cpp` file, which autolinks all C++ HybridObjects (only `HybridTestObjectCpp`)
26
+ - [`src/main/java/com/margelo/nitro/visionocr/`](android/src/main/java/com/margelo/nitro/visionocr/): All Kotlin implementations.
27
+ - [`NitroVisionOcrPackage.kt`](android/src/main/java/com/margelo/nitro/visionocr/NitroVisionOcrPackage.kt): The react-native package. You need this because the react-native CLI only adds libraries if they have a `*Package.kt` file. In here, you can autolink all Kotlin HybridObjects.
28
+ - [`cpp/`](cpp): All your cross-platform implementations. (only `HybridTestObjectCpp.cpp`)
29
+ - [`ios/`](ios): All your iOS-specific implementations.
30
+ - [`nitrogen/`](nitrogen): All files generated by nitrogen. You should commit this folder to git.
31
+ - [`src/`](src): The TypeScript codebase. This defines all HybridObjects and loads them at runtime.
32
+ - [`specs/`](src/specs): All HybridObject types. Nitrogen will run on all `*.nitro.ts` files.
33
+ - [`nitro.json`](nitro.json): The configuration file for nitrogen. This will define all native namespaces, as well as the library name.
34
+ - [`NitroVisionOcr.podspec`](NitroVisionOcr.podspec): The iOS podspec build file to build the iOS code. This contains three important pieces:
35
+ 1. Specifies the Pod's name. This must be identical to the name specified in `nitro.json`.
36
+ 2. Adds all of your `.swift` or `.cpp` files (implementations).
37
+ 3. Adds all Nitrogen files (`add_nitrogen_files(s)`)
38
+ - [`package.json`](package.json): The npm package.json file. `react-native-nitro-modules` should be a `peerDependency`.
@@ -0,0 +1,31 @@
1
+ project(NitroVisionOcr)
2
+ cmake_minimum_required(VERSION 3.9.0)
3
+
4
+ set (PACKAGE_NAME NitroVisionOcr)
5
+ set (CMAKE_VERBOSE_MAKEFILE ON)
6
+ set (CMAKE_CXX_STANDARD 20)
7
+
8
+ # Define C++ library and add all sources
9
+ add_library(${PACKAGE_NAME} SHARED
10
+ src/main/cpp/cpp-adapter.cpp
11
+ )
12
+
13
+ # Add Nitrogen specs :)
14
+ include(${CMAKE_SOURCE_DIR}/../nitrogen/generated/android/NitroVisionOcr+autolinking.cmake)
15
+
16
+ # Set up local includes
17
+ include_directories(
18
+ "src/main/cpp"
19
+ "../cpp"
20
+ )
21
+
22
+ find_package(react-native-vision-camera REQUIRED)
23
+ find_library(LOG_LIB log)
24
+
25
+ # Link all libraries together
26
+ target_link_libraries(
27
+ ${PACKAGE_NAME}
28
+ ${LOG_LIB}
29
+ android # <-- Android core
30
+ react-native-vision-camera::VisionCamera
31
+ )
@@ -0,0 +1,143 @@
1
+ buildscript {
2
+ repositories {
3
+ google()
4
+ mavenCentral()
5
+ }
6
+
7
+ dependencies {
8
+ classpath "com.android.tools.build:gradle:9.2.1"
9
+ }
10
+ }
11
+
12
+ def reactNativeArchitectures() {
13
+ def value = rootProject.getProperties().get("reactNativeArchitectures")
14
+ return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
15
+ }
16
+
17
+ def isNewArchitectureEnabled() {
18
+ return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
19
+ }
20
+
21
+ apply plugin: "com.android.library"
22
+ apply plugin: 'org.jetbrains.kotlin.android'
23
+ apply from: '../nitrogen/generated/android/NitroVisionOcr+autolinking.gradle'
24
+ apply from: "./fix-prefab.gradle"
25
+
26
+ if (isNewArchitectureEnabled()) {
27
+ apply plugin: "com.facebook.react"
28
+ }
29
+
30
+ def getExtOrDefault(name) {
31
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["NitroVisionOcr_" + name]
32
+ }
33
+
34
+ def getExtOrIntegerDefault(name) {
35
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["NitroVisionOcr_" + name]).toInteger()
36
+ }
37
+
38
+ android {
39
+ namespace "com.margelo.nitro.visionocr"
40
+
41
+ ndkVersion getExtOrDefault("ndkVersion")
42
+ compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
43
+
44
+ defaultConfig {
45
+ minSdkVersion getExtOrIntegerDefault("minSdkVersion")
46
+ targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
47
+ buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
48
+
49
+ externalNativeBuild {
50
+ cmake {
51
+ cppFlags "-frtti -fexceptions -Wall -Wextra -fstack-protector-all"
52
+ arguments "-DANDROID_STL=c++_shared", "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
53
+ abiFilters (*reactNativeArchitectures())
54
+
55
+ buildTypes {
56
+ debug {
57
+ cppFlags "-O1 -g"
58
+ }
59
+ release {
60
+ cppFlags "-O2"
61
+ }
62
+ }
63
+ }
64
+ }
65
+ }
66
+
67
+ externalNativeBuild {
68
+ cmake {
69
+ path "CMakeLists.txt"
70
+ }
71
+ }
72
+
73
+ packagingOptions {
74
+ excludes = [
75
+ "META-INF",
76
+ "META-INF/**",
77
+ "**/libc++_shared.so",
78
+ "**/libNitroModules.so",
79
+ "**/libfbjni.so",
80
+ "**/libjsi.so",
81
+ "**/libfolly_json.so",
82
+ "**/libfolly_runtime.so",
83
+ "**/libglog.so",
84
+ "**/libhermes.so",
85
+ "**/libhermes-executor-debug.so",
86
+ "**/libhermes_executor.so",
87
+ "**/libreactnative.so",
88
+ "**/libreactnativejni.so",
89
+ "**/libturbomodulejsijni.so",
90
+ "**/libreact_nativemodule_core.so",
91
+ "**/libjscexecutor.so"
92
+ ]
93
+ }
94
+
95
+ buildFeatures {
96
+ buildConfig true
97
+ prefab true
98
+ }
99
+
100
+ buildTypes {
101
+ release {
102
+ minifyEnabled false
103
+ }
104
+ }
105
+
106
+ lintOptions {
107
+ disable "GradleCompatible"
108
+ }
109
+
110
+ compileOptions {
111
+ sourceCompatibility JavaVersion.VERSION_1_8
112
+ targetCompatibility JavaVersion.VERSION_1_8
113
+ }
114
+
115
+ sourceSets {
116
+ main {
117
+ if (isNewArchitectureEnabled()) {
118
+ java.srcDirs += [
119
+ // React Codegen files
120
+ "${project.buildDir}/generated/source/codegen/java"
121
+ ]
122
+ }
123
+ }
124
+ }
125
+ }
126
+
127
+ repositories {
128
+ mavenCentral()
129
+ google()
130
+ }
131
+
132
+
133
+ dependencies {
134
+ // For < 0.71, this will be from the local maven repo
135
+ // For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
136
+ //noinspection GradleDynamicVersion
137
+ implementation "com.facebook.react:react-native:+"
138
+
139
+ // Add a dependency on NitroModules
140
+ implementation project(":react-native-nitro-modules")
141
+ implementation project(":react-native-vision-camera")
142
+ }
143
+
@@ -0,0 +1,51 @@
1
+ tasks.configureEach { task ->
2
+ // Make sure that we generate our prefab publication file only after having built the native library
3
+ // so that not a header publication file, but a full configuration publication will be generated, which
4
+ // will include the .so file
5
+
6
+ def prefabConfigurePattern = ~/^prefab(.+)ConfigurePackage$/
7
+ def matcher = task.name =~ prefabConfigurePattern
8
+ if (matcher.matches()) {
9
+ def variantName = matcher[0][1]
10
+ task.outputs.upToDateWhen { false }
11
+ task.dependsOn("externalNativeBuild${variantName}")
12
+ }
13
+ }
14
+
15
+ afterEvaluate {
16
+ def abis = reactNativeArchitectures()
17
+ rootProject.allprojects.each { proj ->
18
+ if (proj === rootProject) return
19
+
20
+ def dependsOnThisLib = proj.configurations.findAll { it.canBeResolved }.any { config ->
21
+ config.dependencies.any { dep ->
22
+ dep.group == project.group && dep.name == project.name
23
+ }
24
+ }
25
+ if (!dependsOnThisLib && proj != project) return
26
+
27
+ if (!proj.plugins.hasPlugin('com.android.application') && !proj.plugins.hasPlugin('com.android.library')) {
28
+ return
29
+ }
30
+
31
+ def variants = proj.android.hasProperty('applicationVariants') ? proj.android.applicationVariants : proj.android.libraryVariants
32
+ // Touch the prefab_config.json files to ensure that in ExternalNativeJsonGenerator.kt we will re-trigger the prefab CLI to
33
+ // generate a libnameConfig.cmake file that will contain our native library (.so).
34
+ // See this condition: https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:build-system/gradle-core/src/main/java/com/android/build/gradle/tasks/ExternalNativeJsonGenerator.kt;l=207-219?q=createPrefabBuildSystemGlue
35
+ variants.all { variant ->
36
+ def variantName = variant.name
37
+ abis.each { abi ->
38
+ def searchDir = new File(proj.projectDir, ".cxx/${variantName}")
39
+ if (!searchDir.exists()) return
40
+ def matches = []
41
+ searchDir.eachDir { randomDir ->
42
+ def prefabFile = new File(randomDir, "${abi}/prefab_config.json")
43
+ if (prefabFile.exists()) matches << prefabFile
44
+ }
45
+ matches.each { prefabConfig ->
46
+ prefabConfig.setLastModified(System.currentTimeMillis())
47
+ }
48
+ }
49
+ }
50
+ }
51
+ }
@@ -0,0 +1,5 @@
1
+ NitroVisionOcr_kotlinVersion=2.1.20
2
+ NitroVisionOcr_minSdkVersion=23
3
+ NitroVisionOcr_targetSdkVersion=36
4
+ NitroVisionOcr_compileSdkVersion=36
5
+ NitroVisionOcr_ndkVersion=27.1.12297006
@@ -0,0 +1,2 @@
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android">
2
+ </manifest>
@@ -0,0 +1,9 @@
1
+ #include <jni.h>
2
+ #include <fbjni/fbjni.h>
3
+ #include "NitroVisionOcrOnLoad.hpp"
4
+
5
+ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) {
6
+ return facebook::jni::initialize(vm, []() {
7
+ margelo::nitro::visionocr::registerAllNatives();
8
+ });
9
+ }
@@ -0,0 +1,18 @@
1
+ package com.margelo.nitro.visionocr
2
+
3
+ import com.facebook.react.bridge.NativeModule
4
+ import com.facebook.react.bridge.ReactApplicationContext
5
+ import com.facebook.react.module.model.ReactModuleInfoProvider
6
+ import com.facebook.react.BaseReactPackage
7
+
8
+ class NitroVisionOcrPackage : BaseReactPackage() {
9
+ override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? = null
10
+
11
+ override fun getReactModuleInfoProvider(): ReactModuleInfoProvider = ReactModuleInfoProvider { HashMap() }
12
+
13
+ companion object {
14
+ init {
15
+ NitroVisionOcrOnLoad.initializeNative()
16
+ }
17
+ }
18
+ }
@@ -0,0 +1,120 @@
1
+ package com.margelo.nitro.math
2
+ import com.margelo.nitro.core.*
3
+ import android.graphics.Point
4
+ import android.graphics.Rect
5
+ import android.media.Image
6
+ import android.util.Log
7
+ import com.facebook.react.bridge.WritableNativeArray
8
+ import com.facebook.react.bridge.WritableNativeMap
9
+ import com.google.android.gms.tasks.Tasks
10
+ import com.google.mlkit.vision.common.InputImage
11
+ import com.google.mlkit.vision.text.Text
12
+ import com.google.mlkit.vision.text.TextRecognition
13
+ import com.google.mlkit.vision.text.latin.TextRecognizerOptions
14
+ import com.mrousavy.camera.frameprocessors.Frame
15
+ import com.mrousavy.camera.frameprocessors.FrameProcessorPlugin
16
+ import com.mrousavy.camera.frameprocessors.VisionCameraProxy
17
+ import java.util.HashMap
18
+
19
+ class VisionOCR : HybridVisionOCRSpec() {
20
+ private val recognizer = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS)
21
+
22
+ override fun call(frame: Frame, arguments: Map<String, Any>?): HashMap<String, Any>? {
23
+ val data = WritableNativeMap()
24
+
25
+ // Log the options for debugging
26
+ if (options != null) {
27
+ Log.d("OcrDetector", "Plugin options: $options")
28
+ val model = options["model"] as? String
29
+ if (model != null) {
30
+ Log.d("OcrDetector", "Using model: $model")
31
+ // TODO: Implement different model options based on 'model' parameter
32
+ // Currently ML Kit only supports DEFAULT_OPTIONS for text recognition
33
+ // Future versions might support different accuracy/speed trade-offs
34
+ }
35
+ }
36
+
37
+ // Read call-time arguments
38
+ val includeBoxes = (arguments?.get("includeBoxes") as? Boolean) ?: false
39
+ val includeConfidence = (arguments?.get("includeConfidence") as? Boolean) ?: false
40
+ Log.d("OcrDetector", "Args includeBoxes=$includeBoxes includeConfidence=$includeConfidence")
41
+
42
+ val mediaImage: Image = frame.image
43
+ val image = InputImage.fromMediaImage(mediaImage, frame.imageProxy.imageInfo.rotationDegrees)
44
+
45
+ return try {
46
+ val visionText: Text = Tasks.await(recognizer.process(image))
47
+
48
+ if (visionText.text.isEmpty()) {
49
+ return null
50
+ }
51
+
52
+ data.putString("text", visionText.text)
53
+
54
+ if (includeBoxes) {
55
+ val blocksArray = WritableNativeArray()
56
+ for (block in visionText.textBlocks) {
57
+ val blockMap = WritableNativeMap()
58
+ blockMap.putString("text", block.text)
59
+
60
+ val blockRect: Rect? = block.boundingBox
61
+ if (blockRect != null) {
62
+ val boxMap = WritableNativeMap()
63
+ boxMap.putDouble("x", blockRect.left.toDouble())
64
+ boxMap.putDouble("y", blockRect.top.toDouble())
65
+ boxMap.putDouble("width", (blockRect.right - blockRect.left).toDouble())
66
+ boxMap.putDouble("height", (blockRect.bottom - blockRect.top).toDouble())
67
+ blockMap.putMap("box", boxMap)
68
+ }
69
+
70
+ val linesArray = WritableNativeArray()
71
+ for (line in block.lines) {
72
+ val lineMap = WritableNativeMap()
73
+ lineMap.putString("text", line.text)
74
+
75
+ val lineRect: Rect? = line.boundingBox
76
+ if (lineRect != null) {
77
+ val lbox = WritableNativeMap()
78
+ lbox.putDouble("x", lineRect.left.toDouble())
79
+ lbox.putDouble("y", lineRect.top.toDouble())
80
+ lbox.putDouble("width", (lineRect.right - lineRect.left).toDouble())
81
+ lbox.putDouble("height", (lineRect.bottom - lineRect.top).toDouble())
82
+ lineMap.putMap("box", lbox)
83
+ }
84
+
85
+ val wordsArray = WritableNativeArray()
86
+ for (element in line.elements) {
87
+ val wordMap = WritableNativeMap()
88
+ wordMap.putString("text", element.text)
89
+ val wRect: Rect? = element.boundingBox
90
+ if (wRect != null) {
91
+ val wbox = WritableNativeMap()
92
+ wbox.putDouble("x", wRect.left.toDouble())
93
+ wbox.putDouble("y", wRect.top.toDouble())
94
+ wbox.putDouble("width", (wRect.right - wRect.left).toDouble())
95
+ wbox.putDouble("height", (wRect.bottom - wRect.top).toDouble())
96
+ wordMap.putMap("box", wbox)
97
+ }
98
+ wordsArray.pushMap(wordMap)
99
+ }
100
+ if (wordsArray.size() > 0) {
101
+ lineMap.putArray("words", wordsArray)
102
+ }
103
+ linesArray.pushMap(lineMap)
104
+ }
105
+ if (linesArray.size() > 0) {
106
+ blockMap.putArray("lines", linesArray)
107
+ }
108
+ blocksArray.pushMap(blockMap)
109
+ }
110
+ data.putArray("blocks", blocksArray)
111
+ }
112
+
113
+ @Suppress("UNCHECKED_CAST")
114
+ data.toHashMap() as HashMap<String, Any>
115
+ } catch (e: Exception) {
116
+ Log.e("OcrDetector", "OCR recognition error: ${e.localizedMessage}")
117
+ null
118
+ }
119
+ }
120
+ }
package/ios/Bridge.h ADDED
@@ -0,0 +1,8 @@
1
+ //
2
+ // Bridge.h
3
+ // NitroVisionOcr
4
+ //
5
+ // Created by Marc Rousavy on 22.07.24.
6
+ //
7
+
8
+ #pragma once
@@ -0,0 +1,137 @@
1
+ import CoreImage
2
+ import Foundation
3
+ import NitroModules
4
+ import ImageIO
5
+ import Vision
6
+ import VisionCamera
7
+
8
+ class VisionOCR : HybridVisionOCRSpec {
9
+ func call(frame: (any HybridFrameSpec), options: OcrOptions?) throws -> OcrResult? {
10
+ let includeBoxes = options?.includeBoxes ?? false
11
+ let includeConfidence = options?.includeConfidence ?? false
12
+ NSLog(
13
+ "Args includeBoxes=%@ includeConfidence=%@",
14
+ includeBoxes ? "YES" : "NO",
15
+ includeConfidence ? "YES" : "NO"
16
+ )
17
+
18
+ let sampleBuffer = try sampleBuffer(from: frame)
19
+ let pixelBuffer = try pixelBuffer(from: sampleBuffer)
20
+
21
+ let ciImage = CIImage(cvPixelBuffer: pixelBuffer)
22
+ let orientation = cgImageOrientation(from: frame.orientation, isMirrored: frame.isMirrored)
23
+ let handler = VNImageRequestHandler(ciImage: ciImage, orientation: orientation, options: [:])
24
+
25
+ var lineResults: [OcrLine] = []
26
+
27
+ let request = VNRecognizeTextRequest { request, error in
28
+ if let error {
29
+ NSLog("OCR recognition failed: %@", error.localizedDescription)
30
+ return
31
+ }
32
+
33
+ guard let results = request.results as? [VNRecognizedTextObservation] else {
34
+ return
35
+ }
36
+
37
+ for observation in results {
38
+ guard let topCandidate = observation.topCandidates(1).first else {
39
+ continue
40
+ }
41
+
42
+ let rect = observation.boundingBox
43
+
44
+ var line: OcrLine = OcrLine(
45
+ text: topCandidate.string,
46
+ box: includeBoxes ? OcrBox(
47
+ x: rect.origin.x,
48
+ y: rect.origin.y,
49
+ width: rect.size.width,
50
+ height: rect.size.height
51
+ ) : nil,
52
+ words: nil,
53
+ confidence: includeConfidence ? Double(topCandidate.confidence) : nil,
54
+ )
55
+ lineResults.append(line)
56
+ }
57
+ }
58
+
59
+ if let level = options?.recognitionLevel {
60
+ if level == .accurate {
61
+ request.recognitionLevel = VNRequestTextRecognitionLevel.accurate
62
+ } else if level == .fast {
63
+ request.recognitionLevel = VNRequestTextRecognitionLevel.fast
64
+ }
65
+ }
66
+
67
+ if let languages = options?.recognitionLanguages {
68
+ request.recognitionLanguages = languages
69
+ }
70
+
71
+ if let useCorrection = options?.usesLanguageCorrection {
72
+ request.usesLanguageCorrection = useCorrection
73
+ }
74
+
75
+ let semaphore = DispatchSemaphore(value: 0)
76
+
77
+ DispatchQueue.global(qos: .userInitiated).async {
78
+ do {
79
+ try handler.perform([request])
80
+ } catch {
81
+ NSLog("Failed to perform OCR recognition: %@", error.localizedDescription)
82
+ }
83
+ semaphore.signal()
84
+ }
85
+
86
+ semaphore.wait()
87
+
88
+ if lineResults.isEmpty {
89
+ return nil
90
+ }
91
+
92
+ let joinedText = lineResults.compactMap { $0.text }.joined(separator: " ")
93
+
94
+ return OcrResult(
95
+ text: joinedText,
96
+ blocks: includeBoxes ? [
97
+ OcrBlock(
98
+ text: joinedText,
99
+ box: nil,
100
+ lines: lineResults
101
+ )
102
+ ] : nil)
103
+ }
104
+
105
+ private func cgImageOrientation(from orientation: CameraOrientation, isMirrored: Bool) -> CGImagePropertyOrientation {
106
+ switch orientation {
107
+ case .up:
108
+ return isMirrored ? .upMirrored : .up
109
+ case .down:
110
+ return isMirrored ? .downMirrored : .down
111
+ case .left:
112
+ return isMirrored ? .leftMirrored : .left
113
+ case .right:
114
+ return isMirrored ? .rightMirrored : .right
115
+ @unknown default:
116
+ return .up
117
+ }
118
+ }
119
+
120
+ private func sampleBuffer(from frame: any HybridFrameSpec) throws -> CMSampleBuffer {
121
+ guard let nativeFrame = frame as? any NativeFrame else {
122
+ throw RuntimeError.error(withMessage: "The given Frame is not of type `NativeFrame`!")
123
+ }
124
+ guard let sampleBuffer = nativeFrame.sampleBuffer else {
125
+ throw RuntimeError.error(withMessage: "The given Frame's `sampleBuffer` is no longer valid!")
126
+ }
127
+ return sampleBuffer
128
+ }
129
+
130
+ private func pixelBuffer(from sampleBuffer: CMSampleBuffer) throws -> CVPixelBuffer {
131
+ guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
132
+ throw RuntimeError.error(
133
+ withMessage: "The given Frame does not contain a valid image buffer!")
134
+ }
135
+ return pixelBuffer
136
+ }
137
+ }
package/nitro.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "$schema": "https://nitro.margelo.com/nitro.schema.json",
3
+ "cxxNamespace": [
4
+ "visionocr"
5
+ ],
6
+ "ios": {
7
+ "iosModuleName": "NitroVisionOcr"
8
+ },
9
+ "android": {
10
+ "androidNamespace": [
11
+ "visionocr"
12
+ ],
13
+ "androidCxxLibName": "NitroVisionOcr"
14
+ },
15
+ "autolinking": {},
16
+ "ignorePaths": [
17
+ "**/node_modules"
18
+ ]
19
+ }
@@ -0,0 +1 @@
1
+ ** linguist-generated=true