rn-document-scanner-vision 1.0.9 → 1.0.10

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.
@@ -2,7 +2,6 @@ package com.rndocumentscanner;
2
2
 
3
3
  import android.app.Activity;
4
4
  import android.content.Intent;
5
- import androidx.activity.result.ActivityResult;
6
5
 
7
6
  import com.facebook.react.bridge.ActivityEventListener;
8
7
  import com.facebook.react.bridge.Arguments;
@@ -15,7 +14,6 @@ import com.facebook.react.bridge.ReadableMap;
15
14
  import com.facebook.react.bridge.WritableArray;
16
15
  import com.facebook.react.bridge.WritableMap;
17
16
 
18
- import com.websitebeaver.documentscanner.DocumentScanner;
19
17
  import com.websitebeaver.documentscanner.constants.DocumentScannerExtra;
20
18
 
21
19
  import java.util.ArrayList;
@@ -24,17 +22,57 @@ public class RNDocumentScannerModule extends ReactContextBaseJavaModule {
24
22
 
25
23
  private static final int DOCUMENT_SCAN_REQUEST = 1;
26
24
  private Promise scanPromise;
27
- private DocumentScanner documentScanner;
25
+ private String responseType = "imageFilePath";
28
26
 
29
27
  private final ActivityEventListener activityEventListener = new BaseActivityEventListener() {
30
28
  @Override
31
29
  public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
32
30
  if (requestCode == DOCUMENT_SCAN_REQUEST) {
33
- if (documentScanner != null) {
34
- // Use a wrapper ActivityResult object
35
- ActivityResult result = new ActivityResult(resultCode, data);
36
- documentScanner.handleDocumentScanIntentResult(result);
31
+ if (scanPromise == null) {
32
+ return;
37
33
  }
34
+
35
+ if (resultCode == Activity.RESULT_OK && data != null) {
36
+ // Get the scanned document results
37
+ ArrayList<String> croppedImageResults = data.getStringArrayListExtra(
38
+ DocumentScannerExtra.EXTRA_CROPPED_IMAGE_RESULTS
39
+ );
40
+
41
+ if (croppedImageResults != null && !croppedImageResults.isEmpty()) {
42
+ WritableMap response = Arguments.createMap();
43
+ WritableArray scannedImages = Arguments.createArray();
44
+
45
+ for (String imagePath : croppedImageResults) {
46
+ scannedImages.pushString(imagePath);
47
+ }
48
+
49
+ response.putArray("scannedImages", scannedImages);
50
+ response.putString("status", "success");
51
+ scanPromise.resolve(response);
52
+ } else {
53
+ // No images returned
54
+ WritableMap response = Arguments.createMap();
55
+ WritableArray emptyArray = Arguments.createArray();
56
+ response.putArray("scannedImages", emptyArray);
57
+ response.putString("status", "cancel");
58
+ scanPromise.resolve(response);
59
+ }
60
+ } else if (resultCode == Activity.RESULT_CANCELED) {
61
+ // User cancelled
62
+ WritableMap response = Arguments.createMap();
63
+ WritableArray emptyArray = Arguments.createArray();
64
+ response.putArray("scannedImages", emptyArray);
65
+ response.putString("status", "cancel");
66
+ scanPromise.resolve(response);
67
+ } else {
68
+ // Check for error
69
+ String errorMessage = data != null ?
70
+ data.getStringExtra(DocumentScannerExtra.EXTRA_ERROR_MESSAGE) :
71
+ "Unknown error";
72
+ scanPromise.reject("SCAN_ERROR", errorMessage != null ? errorMessage : "Document scan failed");
73
+ }
74
+
75
+ scanPromise = null;
38
76
  }
39
77
  }
40
78
  };
@@ -52,7 +90,7 @@ public class RNDocumentScannerModule extends ReactContextBaseJavaModule {
52
90
  @ReactMethod
53
91
  public void scanDocument(ReadableMap options, Promise promise) {
54
92
  Activity activity = getCurrentActivity();
55
-
93
+
56
94
  if (activity == null) {
57
95
  promise.reject("NO_ACTIVITY", "Activity doesn't exist");
58
96
  return;
@@ -61,66 +99,27 @@ public class RNDocumentScannerModule extends ReactContextBaseJavaModule {
61
99
  scanPromise = promise;
62
100
 
63
101
  // Get options with defaults
64
- String responseType = options.hasKey("responseType") ? options.getString("responseType") : "imageFilePath";
102
+ responseType = options.hasKey("responseType") ? options.getString("responseType") : "imageFilePath";
65
103
  Boolean letUserAdjustCrop = options.hasKey("letUserAdjustCrop") ? options.getBoolean("letUserAdjustCrop") : true;
66
104
  Integer maxNumDocuments = options.hasKey("maxNumDocuments") ? options.getInt("maxNumDocuments") : 1;
67
105
  Integer croppedImageQuality = options.hasKey("croppedImageQuality") ? options.getInt("croppedImageQuality") : 100;
68
106
 
69
107
  try {
70
- // Create document scanner with callbacks
71
- documentScanner = new DocumentScanner(
72
- activity,
73
- (ArrayList<String> documentScanResults) -> {
74
- // Success callback
75
- WritableMap response = Arguments.createMap();
76
- WritableArray scannedImages = Arguments.createArray();
77
-
78
- for (String imagePath : documentScanResults) {
79
- scannedImages.pushString(imagePath);
80
- }
81
-
82
- response.putArray("scannedImages", scannedImages);
83
- response.putString("status", "success");
84
-
85
- if (scanPromise != null) {
86
- scanPromise.resolve(response);
87
- scanPromise = null;
88
- }
89
- return null;
90
- },
91
- (String errorMessage) -> {
92
- // Error callback
93
- if (scanPromise != null) {
94
- scanPromise.reject("SCAN_ERROR", errorMessage);
95
- scanPromise = null;
96
- }
97
- return null;
98
- },
99
- () -> {
100
- // Cancel callback
101
- WritableMap response = Arguments.createMap();
102
- WritableArray emptyArray = Arguments.createArray();
103
- response.putArray("scannedImages", emptyArray);
104
- response.putString("status", "cancel");
105
-
106
- if (scanPromise != null) {
107
- scanPromise.resolve(response);
108
- scanPromise = null;
109
- }
110
- return null;
111
- },
112
- responseType,
113
- letUserAdjustCrop,
114
- maxNumDocuments,
115
- croppedImageQuality
116
- );
117
-
118
- // Launch the document scanner
119
- Intent scanIntent = documentScanner.createDocumentScanIntent();
108
+ // Create intent directly for DocumentScannerActivity
109
+ Intent scanIntent = new Intent(activity, com.websitebeaver.documentscanner.DocumentScannerActivity.class);
110
+
111
+ // Set extras
112
+ scanIntent.putExtra(DocumentScannerExtra.EXTRA_LET_USER_ADJUST_CROP, letUserAdjustCrop);
113
+ scanIntent.putExtra(DocumentScannerExtra.EXTRA_MAX_NUM_DOCUMENTS, maxNumDocuments);
114
+ scanIntent.putExtra(DocumentScannerExtra.EXTRA_CROPPED_IMAGE_QUALITY, croppedImageQuality);
115
+ scanIntent.putExtra(DocumentScannerExtra.EXTRA_RESPONSE_TYPE, responseType);
116
+
117
+ // Launch the document scanner activity
120
118
  activity.startActivityForResult(scanIntent, DOCUMENT_SCAN_REQUEST);
121
119
 
122
120
  } catch (Exception e) {
123
121
  promise.reject("INITIALIZATION_ERROR", "Failed to initialize document scanner: " + e.getMessage());
122
+ scanPromise = null;
124
123
  }
125
124
  }
126
125
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rn-document-scanner-vision",
3
- "version": "1.0.9",
3
+ "version": "1.0.10",
4
4
  "description": "A React Native library for scanning documents on iOS and Android",
5
5
  "main": "lib/commonjs/index.js",
6
6
  "module": "lib/module/index.js",