@saveeye/saveeye-sdk-reactnative-meter-recognition 0.0.0-stage → 0.1.0

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 (80) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +112 -2
  3. package/android/build.gradle +123 -0
  4. package/android/generated/java/com/saveeyemeterrecognition/NativeSaveeyeMeterRecognitionSpec.java +38 -0
  5. package/android/generated/jni/CMakeLists.txt +36 -0
  6. package/android/generated/jni/RNSaveeyeMeterRecognitionSpec-generated.cpp +32 -0
  7. package/android/generated/jni/RNSaveeyeMeterRecognitionSpec.h +31 -0
  8. package/android/generated/jni/react/renderer/components/RNSaveeyeMeterRecognitionSpec/RNSaveeyeMeterRecognitionSpecJSI-generated.cpp +30 -0
  9. package/android/generated/jni/react/renderer/components/RNSaveeyeMeterRecognitionSpec/RNSaveeyeMeterRecognitionSpecJSI.h +114 -0
  10. package/android/gradle.properties +5 -0
  11. package/android/src/main/AndroidManifest.xml +3 -0
  12. package/android/src/main/AndroidManifestNew.xml +2 -0
  13. package/android/src/main/java/com/saveeyemeterrecognition/ImageResizer.kt +113 -0
  14. package/android/src/main/java/com/saveeyemeterrecognition/SaveeyeMeterRecognitionModule.kt +42 -0
  15. package/android/src/main/java/com/saveeyemeterrecognition/SaveeyeMeterRecognitionPackage.kt +34 -0
  16. package/ios/SaveeyeMeterRecognition.h +6 -0
  17. package/ios/SaveeyeMeterRecognition.mm +82 -0
  18. package/ios/generated/RNSaveeyeMeterRecognitionSpec/RNSaveeyeMeterRecognitionSpec-generated.mm +39 -0
  19. package/ios/generated/RNSaveeyeMeterRecognitionSpec/RNSaveeyeMeterRecognitionSpec.h +64 -0
  20. package/ios/generated/RNSaveeyeMeterRecognitionSpecJSI-generated.cpp +30 -0
  21. package/ios/generated/RNSaveeyeMeterRecognitionSpecJSI.h +114 -0
  22. package/lib/commonjs/ImageUtil.js +83 -0
  23. package/lib/commonjs/ImageUtil.js.map +1 -0
  24. package/lib/commonjs/MeterImageFilter.js +103 -0
  25. package/lib/commonjs/MeterImageFilter.js.map +1 -0
  26. package/lib/commonjs/NativeSaveeyeMeterRecognition.js +11 -0
  27. package/lib/commonjs/NativeSaveeyeMeterRecognition.js.map +1 -0
  28. package/lib/commonjs/index.js +51 -0
  29. package/lib/commonjs/index.js.map +1 -0
  30. package/lib/commonjs/recognizeMeter.js +84 -0
  31. package/lib/commonjs/recognizeMeter.js.map +1 -0
  32. package/lib/commonjs/types.js +21 -0
  33. package/lib/commonjs/types.js.map +1 -0
  34. package/lib/module/ImageUtil.js +77 -0
  35. package/lib/module/ImageUtil.js.map +1 -0
  36. package/lib/module/MeterImageFilter.js +96 -0
  37. package/lib/module/MeterImageFilter.js.map +1 -0
  38. package/lib/module/NativeSaveeyeMeterRecognition.js +7 -0
  39. package/lib/module/NativeSaveeyeMeterRecognition.js.map +1 -0
  40. package/lib/module/index.js +6 -0
  41. package/lib/module/index.js.map +1 -0
  42. package/lib/module/recognizeMeter.js +79 -0
  43. package/lib/module/recognizeMeter.js.map +1 -0
  44. package/lib/module/types.js +17 -0
  45. package/lib/module/types.js.map +1 -0
  46. package/lib/typescript/commonjs/package.json +1 -0
  47. package/lib/typescript/commonjs/src/ImageUtil.d.ts +10 -0
  48. package/lib/typescript/commonjs/src/ImageUtil.d.ts.map +1 -0
  49. package/lib/typescript/commonjs/src/MeterImageFilter.d.ts +32 -0
  50. package/lib/typescript/commonjs/src/MeterImageFilter.d.ts.map +1 -0
  51. package/lib/typescript/commonjs/src/NativeSaveeyeMeterRecognition.d.ts +17 -0
  52. package/lib/typescript/commonjs/src/NativeSaveeyeMeterRecognition.d.ts.map +1 -0
  53. package/lib/typescript/commonjs/src/index.d.ts +4 -0
  54. package/lib/typescript/commonjs/src/index.d.ts.map +1 -0
  55. package/lib/typescript/commonjs/src/recognizeMeter.d.ts +18 -0
  56. package/lib/typescript/commonjs/src/recognizeMeter.d.ts.map +1 -0
  57. package/lib/typescript/commonjs/src/types.d.ts +50 -0
  58. package/lib/typescript/commonjs/src/types.d.ts.map +1 -0
  59. package/lib/typescript/module/package.json +1 -0
  60. package/lib/typescript/module/src/ImageUtil.d.ts +10 -0
  61. package/lib/typescript/module/src/ImageUtil.d.ts.map +1 -0
  62. package/lib/typescript/module/src/MeterImageFilter.d.ts +32 -0
  63. package/lib/typescript/module/src/MeterImageFilter.d.ts.map +1 -0
  64. package/lib/typescript/module/src/NativeSaveeyeMeterRecognition.d.ts +17 -0
  65. package/lib/typescript/module/src/NativeSaveeyeMeterRecognition.d.ts.map +1 -0
  66. package/lib/typescript/module/src/index.d.ts +4 -0
  67. package/lib/typescript/module/src/index.d.ts.map +1 -0
  68. package/lib/typescript/module/src/recognizeMeter.d.ts +18 -0
  69. package/lib/typescript/module/src/recognizeMeter.d.ts.map +1 -0
  70. package/lib/typescript/module/src/types.d.ts +50 -0
  71. package/lib/typescript/module/src/types.d.ts.map +1 -0
  72. package/package.json +170 -4
  73. package/react-native.config.js +12 -0
  74. package/saveeye-sdk-meter-recognition.podspec +41 -0
  75. package/src/ImageUtil.ts +113 -0
  76. package/src/MeterImageFilter.ts +154 -0
  77. package/src/NativeSaveeyeMeterRecognition.ts +26 -0
  78. package/src/index.ts +22 -0
  79. package/src/recognizeMeter.ts +109 -0
  80. package/src/types.ts +55 -0
package/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 SaveEye ApS
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all
12
+ copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,113 @@
1
- # Temporary Holding Version
1
+ # @saveeye/saveeye-sdk-reactnative-meter-recognition
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Recognize electricity meters from photos with the SaveEye SDK. Before uploading, the photo is checked on-device with Google ML Kit, so photos that clearly aren't a power meter (pets, people, feet, food...) or that show a SaveEye device instead of the meter are rejected without a network round-trip.
4
+
5
+ Built on top of [`@saveeye/saveeye-sdk-reactnative`](https://www.npmjs.com/package/@saveeye/saveeye-sdk-reactnative) (2.0+), which provides authentication. It is a separate package so apps that only use device provisioning don't need ML Kit or native image handling. Moving from `SaveeyeSdk.recognizeMeter()` in core SDK 1.x? See the core SDK's CHANGELOG for migration steps.
6
+
7
+ ## Installation
8
+
9
+ ```sh
10
+ yarn add @saveeye/saveeye-sdk-reactnative-meter-recognition \
11
+ @saveeye/saveeye-sdk-reactnative \
12
+ @react-native-ml-kit/image-labeling \
13
+ @react-native-ml-kit/text-recognition \
14
+ @react-native-ml-kit/barcode-scanning
15
+ cd ios && pod install
16
+ ```
17
+
18
+ Requires iOS 15.5+ (ML Kit). Expo apps need a development build (`expo prebuild` / dev client); Expo Go is not supported.
19
+
20
+ Initialize the core SDK as usual before calling `recognizeMeter`.
21
+
22
+ ## Usage
23
+
24
+ ```typescript
25
+ import { recognizeMeter } from '@saveeye/saveeye-sdk-reactnative-meter-recognition';
26
+
27
+ const result = await recognizeMeter({
28
+ uri: photo.uri, // file:// URI, JPEG/PNG/HEIC
29
+ type: 'image/jpeg',
30
+ name: 'meter.jpg',
31
+ });
32
+
33
+ if (result.rejectedLocally) {
34
+ if (result.localCheck?.reason === 'saveeye_device') {
35
+ // Photo shows a SaveEye device: ask the user to photograph the power meter
36
+ } else {
37
+ // No power meter found in the photo: ask for a new photo
38
+ }
39
+ } else if (!result.success) {
40
+ // Uploaded, but the backend didn't match a known meter
41
+ } else if (result.hasAccess) {
42
+ // result.powerMeter, result.compatibleProducts
43
+ }
44
+ ```
45
+
46
+ ### recognizeMeter
47
+
48
+ ```typescript
49
+ recognizeMeter(
50
+ image: RecognitionImageFile,
51
+ countryId?: string,
52
+ options?: { skipLocalCheck?: boolean }
53
+ ): Promise<MeterRecognitionResult>
54
+ ```
55
+
56
+ Runs `checkMeterImage` and, if the photo plausibly shows a meter, uploads it for recognition. HEIC/HEIF photos (the iOS camera default) are converted to JPEG and images over 10 MB are resized natively before uploading, so you don't need to handle either yourself. Returns a `RecognizedMeter`:
57
+
58
+ ```typescript
59
+ type RecognizedMeter = {
60
+ hasAccess: boolean; // Whether this app/partner may access recognition details
61
+ success: boolean; // Whether the image matched a known electricity meter
62
+ powerMeter?: { manufacturer: string; model: string } | null;
63
+ compatibleProducts: Array<{
64
+ id: string; // Product identifier, usable when creating an order line
65
+ name: string;
66
+ sku: string;
67
+ meterInterfaceType: MeterInterfaceType; // P1, ECHELON, HAN_RJ45, IR, KAMSTRUP, BASE
68
+ accessories: Array<{
69
+ countryId?: string | null; // Set when the accessory is country-specific
70
+ requirement: AccessoryRequirement; // REQUIRED or OPTIONAL
71
+ description?: string | null;
72
+ isAvailable: boolean;
73
+ product: { id: string; name: string; sku: string };
74
+ }>;
75
+ }>;
76
+ };
77
+ ```
78
+
79
+ `powerMeter` / `compatibleProducts` are only populated when `hasAccess` is `true`. The result also includes:
80
+
81
+ ```typescript
82
+ type MeterRecognitionResult = RecognizedMeter & {
83
+ rejectedLocally: boolean; // true → nothing was uploaded, see localCheck
84
+ localCheck?: MeterImageCheck;
85
+ };
86
+ ```
87
+
88
+ Pass `skipLocalCheck: true` to always upload.
89
+
90
+ ### checkMeterImage
91
+
92
+ ```typescript
93
+ checkMeterImage(uri: string): Promise<MeterImageCheck>
94
+ ```
95
+
96
+ Runs only the on-device check, without uploading. Never throws.
97
+
98
+ ```typescript
99
+ type MeterImageCheck = {
100
+ isLikelyMeter: boolean;
101
+ reason:
102
+ | 'meter_text' // OCR found kWh/kW/MID or 4+ digit numbers → accepted
103
+ | 'meter_label' // ML Kit labeled it as a gadget/machine/gauge... → accepted
104
+ | 'rejected_label' // pet, person, foot, food... → rejected
105
+ | 'no_signal' // nothing meter-like found → rejected
106
+ | 'saveeye_device' // QR code containing "saveeye" → rejected
107
+ | 'check_unavailable'; // ML Kit failed → accepted
108
+ topLabel?: string; // Highest-confidence ML Kit label
109
+ digitRuns: number; // Number of 4+ digit sequences found by OCR
110
+ };
111
+ ```
112
+
113
+ The check is biased towards accepting: the backend still rejects anything that slips through, while a false rejection blocks a real user.
@@ -0,0 +1,123 @@
1
+ buildscript {
2
+ // Buildscript is evaluated before everything else so we can't use getExtOrDefault
3
+ def kotlin_version = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["SaveeyeMeterRecognition_kotlinVersion"]
4
+
5
+ repositories {
6
+ google()
7
+ mavenCentral()
8
+ }
9
+
10
+ dependencies {
11
+ classpath "com.android.tools.build:gradle:7.2.1"
12
+ // noinspection DifferentKotlinGradleVersion
13
+ classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
14
+ }
15
+ }
16
+
17
+ def reactNativeArchitectures() {
18
+ def value = rootProject.getProperties().get("reactNativeArchitectures")
19
+ return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
20
+ }
21
+
22
+ def isNewArchitectureEnabled() {
23
+ return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
24
+ }
25
+
26
+ apply plugin: "com.android.library"
27
+ apply plugin: "kotlin-android"
28
+
29
+ if (isNewArchitectureEnabled()) {
30
+ apply plugin: "com.facebook.react"
31
+ }
32
+
33
+ def getExtOrDefault(name) {
34
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["SaveeyeMeterRecognition_" + name]
35
+ }
36
+
37
+ def getExtOrIntegerDefault(name) {
38
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["SaveeyeMeterRecognition_" + name]).toInteger()
39
+ }
40
+
41
+ def supportsNamespace() {
42
+ def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
43
+ def major = parsed[0].toInteger()
44
+ def minor = parsed[1].toInteger()
45
+
46
+ // Namespace support was added in 7.3.0
47
+ return (major == 7 && minor >= 3) || major >= 8
48
+ }
49
+
50
+ android {
51
+ if (supportsNamespace()) {
52
+ namespace "com.saveeyemeterrecognition"
53
+
54
+ sourceSets {
55
+ main {
56
+ manifest.srcFile "src/main/AndroidManifestNew.xml"
57
+ }
58
+ }
59
+ }
60
+
61
+ compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
62
+
63
+ defaultConfig {
64
+ minSdkVersion getExtOrIntegerDefault("minSdkVersion")
65
+ targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
66
+ buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
67
+
68
+ }
69
+
70
+ buildFeatures {
71
+ buildConfig true
72
+ }
73
+
74
+ buildTypes {
75
+ release {
76
+ minifyEnabled false
77
+ }
78
+ }
79
+
80
+ lintOptions {
81
+ disable "GradleCompatible"
82
+ }
83
+
84
+ compileOptions {
85
+ sourceCompatibility JavaVersion.VERSION_1_8
86
+ targetCompatibility JavaVersion.VERSION_1_8
87
+ }
88
+
89
+ sourceSets {
90
+ main {
91
+ if (isNewArchitectureEnabled()) {
92
+ java.srcDirs += [
93
+ "generated/java",
94
+ "generated/jni"
95
+ ]
96
+ }
97
+ }
98
+ }
99
+ }
100
+
101
+ repositories {
102
+ mavenCentral()
103
+ google()
104
+ }
105
+
106
+ def kotlin_version = getExtOrDefault("kotlinVersion")
107
+
108
+ dependencies {
109
+ // For < 0.71, this will be from the local maven repo
110
+ // For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
111
+ //noinspection GradleDynamicVersion
112
+ implementation "com.facebook.react:react-native:+"
113
+ implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
114
+ implementation "androidx.exifinterface:exifinterface:1.3.7"
115
+ }
116
+
117
+ if (isNewArchitectureEnabled()) {
118
+ react {
119
+ jsRootDir = file("../src/")
120
+ libraryName = "SaveeyeMeterRecognition"
121
+ codegenJavaPackageName = "com.saveeyemeterrecognition"
122
+ }
123
+ }
@@ -0,0 +1,38 @@
1
+
2
+ /**
3
+ * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
4
+ *
5
+ * Do not edit this file as changes may cause incorrect behavior and will be lost
6
+ * once the code is regenerated.
7
+ *
8
+ * @generated by codegen project: GenerateModuleJavaSpec.js
9
+ *
10
+ * @nolint
11
+ */
12
+
13
+ package com.saveeyemeterrecognition;
14
+
15
+ import com.facebook.proguard.annotations.DoNotStrip;
16
+ import com.facebook.react.bridge.Promise;
17
+ import com.facebook.react.bridge.ReactApplicationContext;
18
+ import com.facebook.react.bridge.ReactContextBaseJavaModule;
19
+ import com.facebook.react.bridge.ReactMethod;
20
+ import com.facebook.react.turbomodule.core.interfaces.TurboModule;
21
+ import javax.annotation.Nonnull;
22
+
23
+ public abstract class NativeSaveeyeMeterRecognitionSpec extends ReactContextBaseJavaModule implements TurboModule {
24
+ public static final String NAME = "SaveeyeMeterRecognition";
25
+
26
+ public NativeSaveeyeMeterRecognitionSpec(ReactApplicationContext reactContext) {
27
+ super(reactContext);
28
+ }
29
+
30
+ @Override
31
+ public @Nonnull String getName() {
32
+ return NAME;
33
+ }
34
+
35
+ @ReactMethod
36
+ @DoNotStrip
37
+ public abstract void resizeImage(String uri, double maxDimension, double quality, String format, Promise promise);
38
+ }
@@ -0,0 +1,36 @@
1
+ # Copyright (c) Meta Platforms, Inc. and affiliates.
2
+ #
3
+ # This source code is licensed under the MIT license found in the
4
+ # LICENSE file in the root directory of this source tree.
5
+
6
+ cmake_minimum_required(VERSION 3.13)
7
+ set(CMAKE_VERBOSE_MAKEFILE on)
8
+
9
+ file(GLOB react_codegen_SRCS CONFIGURE_DEPENDS *.cpp react/renderer/components/RNSaveeyeMeterRecognitionSpec/*.cpp)
10
+
11
+ add_library(
12
+ react_codegen_RNSaveeyeMeterRecognitionSpec
13
+ OBJECT
14
+ ${react_codegen_SRCS}
15
+ )
16
+
17
+ target_include_directories(react_codegen_RNSaveeyeMeterRecognitionSpec PUBLIC . react/renderer/components/RNSaveeyeMeterRecognitionSpec)
18
+
19
+ target_link_libraries(
20
+ react_codegen_RNSaveeyeMeterRecognitionSpec
21
+ fbjni
22
+ jsi
23
+ # We need to link different libraries based on whether we are building rncore or not, that's necessary
24
+ # because we want to break a circular dependency between react_codegen_rncore and reactnative
25
+ reactnative
26
+ )
27
+
28
+ target_compile_options(
29
+ react_codegen_RNSaveeyeMeterRecognitionSpec
30
+ PRIVATE
31
+ -DLOG_TAG=\"ReactNative\"
32
+ -fexceptions
33
+ -frtti
34
+ -std=c++20
35
+ -Wall
36
+ )
@@ -0,0 +1,32 @@
1
+
2
+ /**
3
+ * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
4
+ *
5
+ * Do not edit this file as changes may cause incorrect behavior and will be lost
6
+ * once the code is regenerated.
7
+ *
8
+ * @generated by codegen project: GenerateModuleJniCpp.js
9
+ */
10
+
11
+ #include "RNSaveeyeMeterRecognitionSpec.h"
12
+
13
+ namespace facebook::react {
14
+
15
+ static facebook::jsi::Value __hostFunction_NativeSaveeyeMeterRecognitionSpecJSI_resizeImage(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
16
+ static jmethodID cachedMethodId = nullptr;
17
+ return static_cast<JavaTurboModule &>(turboModule).invokeJavaMethod(rt, PromiseKind, "resizeImage", "(Ljava/lang/String;DDLjava/lang/String;Lcom/facebook/react/bridge/Promise;)V", args, count, cachedMethodId);
18
+ }
19
+
20
+ NativeSaveeyeMeterRecognitionSpecJSI::NativeSaveeyeMeterRecognitionSpecJSI(const JavaTurboModule::InitParams &params)
21
+ : JavaTurboModule(params) {
22
+ methodMap_["resizeImage"] = MethodMetadata {4, __hostFunction_NativeSaveeyeMeterRecognitionSpecJSI_resizeImage};
23
+ }
24
+
25
+ std::shared_ptr<TurboModule> RNSaveeyeMeterRecognitionSpec_ModuleProvider(const std::string &moduleName, const JavaTurboModule::InitParams &params) {
26
+ if (moduleName == "SaveeyeMeterRecognition") {
27
+ return std::make_shared<NativeSaveeyeMeterRecognitionSpecJSI>(params);
28
+ }
29
+ return nullptr;
30
+ }
31
+
32
+ } // namespace facebook::react
@@ -0,0 +1,31 @@
1
+
2
+ /**
3
+ * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
4
+ *
5
+ * Do not edit this file as changes may cause incorrect behavior and will be lost
6
+ * once the code is regenerated.
7
+ *
8
+ * @generated by codegen project: GenerateModuleJniH.js
9
+ */
10
+
11
+ #pragma once
12
+
13
+ #include <ReactCommon/JavaTurboModule.h>
14
+ #include <ReactCommon/TurboModule.h>
15
+ #include <jsi/jsi.h>
16
+
17
+ namespace facebook::react {
18
+
19
+ /**
20
+ * JNI C++ class for module 'NativeSaveeyeMeterRecognition'
21
+ */
22
+ class JSI_EXPORT NativeSaveeyeMeterRecognitionSpecJSI : public JavaTurboModule {
23
+ public:
24
+ NativeSaveeyeMeterRecognitionSpecJSI(const JavaTurboModule::InitParams &params);
25
+ };
26
+
27
+
28
+ JSI_EXPORT
29
+ std::shared_ptr<TurboModule> RNSaveeyeMeterRecognitionSpec_ModuleProvider(const std::string &moduleName, const JavaTurboModule::InitParams &params);
30
+
31
+ } // namespace facebook::react
@@ -0,0 +1,30 @@
1
+ /**
2
+ * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
3
+ *
4
+ * Do not edit this file as changes may cause incorrect behavior and will be lost
5
+ * once the code is regenerated.
6
+ *
7
+ * @generated by codegen project: GenerateModuleCpp.js
8
+ */
9
+
10
+ #include "RNSaveeyeMeterRecognitionSpecJSI.h"
11
+
12
+ namespace facebook::react {
13
+
14
+ static jsi::Value __hostFunction_NativeSaveeyeMeterRecognitionCxxSpecJSI_resizeImage(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
15
+ return static_cast<NativeSaveeyeMeterRecognitionCxxSpecJSI *>(&turboModule)->resizeImage(
16
+ rt,
17
+ count <= 0 ? throw jsi::JSError(rt, "Expected argument in position 0 to be passed") : args[0].asString(rt),
18
+ count <= 1 ? throw jsi::JSError(rt, "Expected argument in position 1 to be passed") : args[1].asNumber(),
19
+ count <= 2 ? throw jsi::JSError(rt, "Expected argument in position 2 to be passed") : args[2].asNumber(),
20
+ count <= 3 ? throw jsi::JSError(rt, "Expected argument in position 3 to be passed") : args[3].asString(rt)
21
+ );
22
+ }
23
+
24
+ NativeSaveeyeMeterRecognitionCxxSpecJSI::NativeSaveeyeMeterRecognitionCxxSpecJSI(std::shared_ptr<CallInvoker> jsInvoker)
25
+ : TurboModule("SaveeyeMeterRecognition", jsInvoker) {
26
+ methodMap_["resizeImage"] = MethodMetadata {4, __hostFunction_NativeSaveeyeMeterRecognitionCxxSpecJSI_resizeImage};
27
+ }
28
+
29
+
30
+ } // namespace facebook::react
@@ -0,0 +1,114 @@
1
+ /**
2
+ * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
3
+ *
4
+ * Do not edit this file as changes may cause incorrect behavior and will be lost
5
+ * once the code is regenerated.
6
+ *
7
+ * @generated by codegen project: GenerateModuleH.js
8
+ */
9
+
10
+ #pragma once
11
+
12
+ #include <ReactCommon/TurboModule.h>
13
+ #include <react/bridging/Bridging.h>
14
+
15
+ namespace facebook::react {
16
+
17
+
18
+
19
+ #pragma mark - NativeSaveeyeMeterRecognitionResizedImage
20
+
21
+ template <typename P0, typename P1>
22
+ struct NativeSaveeyeMeterRecognitionResizedImage {
23
+ P0 uri;
24
+ P1 size;
25
+ bool operator==(const NativeSaveeyeMeterRecognitionResizedImage &other) const {
26
+ return uri == other.uri && size == other.size;
27
+ }
28
+ };
29
+
30
+ template <typename T>
31
+ struct NativeSaveeyeMeterRecognitionResizedImageBridging {
32
+ static T types;
33
+
34
+ static T fromJs(
35
+ jsi::Runtime &rt,
36
+ const jsi::Object &value,
37
+ const std::shared_ptr<CallInvoker> &jsInvoker) {
38
+ T result{
39
+ bridging::fromJs<decltype(types.uri)>(rt, value.getProperty(rt, "uri"), jsInvoker),
40
+ bridging::fromJs<decltype(types.size)>(rt, value.getProperty(rt, "size"), jsInvoker)};
41
+ return result;
42
+ }
43
+
44
+ #ifdef DEBUG
45
+ static jsi::String uriToJs(jsi::Runtime &rt, decltype(types.uri) value) {
46
+ return bridging::toJs(rt, value);
47
+ }
48
+
49
+ static double sizeToJs(jsi::Runtime &rt, decltype(types.size) value) {
50
+ return bridging::toJs(rt, value);
51
+ }
52
+ #endif
53
+
54
+ static jsi::Object toJs(
55
+ jsi::Runtime &rt,
56
+ const T &value,
57
+ const std::shared_ptr<CallInvoker> &jsInvoker) {
58
+ auto result = facebook::jsi::Object(rt);
59
+ result.setProperty(rt, "uri", bridging::toJs(rt, value.uri, jsInvoker));
60
+ result.setProperty(rt, "size", bridging::toJs(rt, value.size, jsInvoker));
61
+ return result;
62
+ }
63
+ };
64
+
65
+ class JSI_EXPORT NativeSaveeyeMeterRecognitionCxxSpecJSI : public TurboModule {
66
+ protected:
67
+ NativeSaveeyeMeterRecognitionCxxSpecJSI(std::shared_ptr<CallInvoker> jsInvoker);
68
+
69
+ public:
70
+ virtual jsi::Value resizeImage(jsi::Runtime &rt, jsi::String uri, double maxDimension, double quality, jsi::String format) = 0;
71
+
72
+ };
73
+
74
+ template <typename T>
75
+ class JSI_EXPORT NativeSaveeyeMeterRecognitionCxxSpec : public TurboModule {
76
+ public:
77
+ jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propName) override {
78
+ return delegate_.get(rt, propName);
79
+ }
80
+
81
+ static constexpr std::string_view kModuleName = "SaveeyeMeterRecognition";
82
+
83
+ protected:
84
+ NativeSaveeyeMeterRecognitionCxxSpec(std::shared_ptr<CallInvoker> jsInvoker)
85
+ : TurboModule(std::string{NativeSaveeyeMeterRecognitionCxxSpec::kModuleName}, jsInvoker),
86
+ delegate_(reinterpret_cast<T*>(this), jsInvoker) {}
87
+
88
+
89
+ private:
90
+ class Delegate : public NativeSaveeyeMeterRecognitionCxxSpecJSI {
91
+ public:
92
+ Delegate(T *instance, std::shared_ptr<CallInvoker> jsInvoker) :
93
+ NativeSaveeyeMeterRecognitionCxxSpecJSI(std::move(jsInvoker)), instance_(instance) {
94
+
95
+ }
96
+
97
+ jsi::Value resizeImage(jsi::Runtime &rt, jsi::String uri, double maxDimension, double quality, jsi::String format) override {
98
+ static_assert(
99
+ bridging::getParameterCount(&T::resizeImage) == 5,
100
+ "Expected resizeImage(...) to have 5 parameters");
101
+
102
+ return bridging::callFromJs<jsi::Value>(
103
+ rt, &T::resizeImage, jsInvoker_, instance_, std::move(uri), std::move(maxDimension), std::move(quality), std::move(format));
104
+ }
105
+
106
+ private:
107
+ friend class NativeSaveeyeMeterRecognitionCxxSpec;
108
+ T *instance_;
109
+ };
110
+
111
+ Delegate delegate_;
112
+ };
113
+
114
+ } // namespace facebook::react
@@ -0,0 +1,5 @@
1
+ SaveeyeMeterRecognition_kotlinVersion=1.7.0
2
+ SaveeyeMeterRecognition_minSdkVersion=21
3
+ SaveeyeMeterRecognition_targetSdkVersion=31
4
+ SaveeyeMeterRecognition_compileSdkVersion=31
5
+ SaveeyeMeterRecognition_ndkversion=21.4.7075529
@@ -0,0 +1,3 @@
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android"
2
+ package="com.saveeyemeterrecognition">
3
+ </manifest>
@@ -0,0 +1,2 @@
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android">
2
+ </manifest>
@@ -0,0 +1,113 @@
1
+ package com.saveeyemeterrecognition
2
+
3
+ import android.content.Context
4
+ import android.graphics.Bitmap
5
+ import android.graphics.BitmapFactory
6
+ import android.graphics.Matrix
7
+ import android.net.Uri
8
+ import androidx.exifinterface.media.ExifInterface
9
+ import com.facebook.react.bridge.Arguments
10
+ import com.facebook.react.bridge.WritableMap
11
+ import java.io.File
12
+ import java.io.IOException
13
+ import java.io.InputStream
14
+ import java.util.UUID
15
+ import kotlin.math.max
16
+ import kotlin.math.min
17
+
18
+ /**
19
+ * Decodes an image (file:// or content://, any format BitmapFactory supports,
20
+ * incl. HEIC on API 28+), applies its EXIF orientation, scales it down so
21
+ * neither side exceeds maxDimension and writes it to the cache dir.
22
+ */
23
+ object ImageResizer {
24
+ fun resize(
25
+ context: Context,
26
+ uriString: String,
27
+ maxDimension: Int,
28
+ quality: Int,
29
+ format: String
30
+ ): WritableMap {
31
+ val uri = Uri.parse(uriString).let {
32
+ if (it.scheme == null) Uri.fromFile(File(uriString)) else it
33
+ }
34
+
35
+ val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }
36
+ open(context, uri).use { BitmapFactory.decodeStream(it, null, bounds) }
37
+ if (bounds.outWidth <= 0 || bounds.outHeight <= 0) {
38
+ throw IOException("Unable to read image at $uriString")
39
+ }
40
+
41
+ // Subsample while decoding so large photos never sit in memory at full size
42
+ var sampleSize = 1
43
+ while (max(bounds.outWidth, bounds.outHeight) / (sampleSize * 2) >= maxDimension) {
44
+ sampleSize *= 2
45
+ }
46
+ val decoded = open(context, uri).use {
47
+ BitmapFactory.decodeStream(
48
+ it,
49
+ null,
50
+ BitmapFactory.Options().apply { inSampleSize = sampleSize }
51
+ )
52
+ } ?: throw IOException("Unable to decode image at $uriString")
53
+
54
+ val orientation = open(context, uri).use {
55
+ ExifInterface(it).getAttributeInt(
56
+ ExifInterface.TAG_ORIENTATION,
57
+ ExifInterface.ORIENTATION_NORMAL
58
+ )
59
+ }
60
+
61
+ val scale = min(1f, maxDimension.toFloat() / max(decoded.width, decoded.height))
62
+ val matrix = Matrix().apply {
63
+ postScale(scale, scale)
64
+ applyOrientation(this, orientation)
65
+ }
66
+ val output = if (matrix.isIdentity) {
67
+ decoded
68
+ } else {
69
+ Bitmap.createBitmap(decoded, 0, 0, decoded.width, decoded.height, matrix, true)
70
+ }
71
+
72
+ val isPng = format.equals("PNG", ignoreCase = true)
73
+ val file = File(context.cacheDir, "saveeye-${UUID.randomUUID()}.${if (isPng) "png" else "jpg"}")
74
+ file.outputStream().use {
75
+ output.compress(
76
+ if (isPng) Bitmap.CompressFormat.PNG else Bitmap.CompressFormat.JPEG,
77
+ quality.coerceIn(0, 100),
78
+ it
79
+ )
80
+ }
81
+ if (output !== decoded) {
82
+ output.recycle()
83
+ }
84
+ decoded.recycle()
85
+
86
+ return Arguments.createMap().apply {
87
+ putString("uri", Uri.fromFile(file).toString())
88
+ putDouble("size", file.length().toDouble())
89
+ }
90
+ }
91
+
92
+ private fun open(context: Context, uri: Uri): InputStream =
93
+ context.contentResolver.openInputStream(uri)
94
+ ?: throw IOException("Unable to open image at $uri")
95
+
96
+ private fun applyOrientation(matrix: Matrix, orientation: Int) {
97
+ when (orientation) {
98
+ ExifInterface.ORIENTATION_FLIP_HORIZONTAL -> matrix.postScale(-1f, 1f)
99
+ ExifInterface.ORIENTATION_ROTATE_180 -> matrix.postRotate(180f)
100
+ ExifInterface.ORIENTATION_FLIP_VERTICAL -> matrix.postScale(1f, -1f)
101
+ ExifInterface.ORIENTATION_TRANSPOSE -> {
102
+ matrix.postRotate(90f)
103
+ matrix.postScale(-1f, 1f)
104
+ }
105
+ ExifInterface.ORIENTATION_ROTATE_90 -> matrix.postRotate(90f)
106
+ ExifInterface.ORIENTATION_TRANSVERSE -> {
107
+ matrix.postRotate(-90f)
108
+ matrix.postScale(-1f, 1f)
109
+ }
110
+ ExifInterface.ORIENTATION_ROTATE_270 -> matrix.postRotate(270f)
111
+ }
112
+ }
113
+ }