capacitor-card-scanner 0.1.0 → 0.2.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.
package/README.md CHANGED
@@ -328,11 +328,16 @@ requestPermissions() => Promise<PermissionStatus>
328
328
 
329
329
  #### DetectionOptions
330
330
 
331
- | Prop | Type | Description | Default |
332
- | ------------------ | -------------------- | ----------------------------------------------------------------------------------------------------------- | ------------------ |
333
- | **`intervalMs`** | <code>number</code> | Minimum time between two OCR runs, in milliseconds. The red rectangle is updated on every frame regardless. | <code>300</code> |
334
- | **`stableFrames`** | <code>number</code> | How many consecutive OCR runs must read the same card before `cardDetected` fires. | <code>2</code> |
335
- | **`includeImage`** | <code>boolean</code> | Include a JPEG of the detected card in the event. | <code>false</code> |
331
+ | Prop | Type | Description | Default |
332
+ | --------------------- | -------------------- | ------------------------------------------------------------------------------------------------------------------------------- | ------------------------------- |
333
+ | **`intervalMs`** | <code>number</code> | Minimum time between two OCR runs, in milliseconds. The red rectangle is updated on every frame regardless. | <code>300</code> |
334
+ | **`stableFrames`** | <code>number</code> | How many consecutive OCR runs must read the same card before `cardDetected` fires. | <code>2</code> |
335
+ | **`includeImage`** | <code>boolean</code> | Include a JPEG of the detected card in the event. | <code>false</code> |
336
+ | **`cardAspectRatio`** | <code>number</code> | Expected width / height of an upright card. Only upright cards are detected; a card lying sideways falls outside the tolerance. | <code>0.716 (63 x 88 mm)</code> |
337
+ | **`aspectTolerance`** | <code>number</code> | Allowed relative deviation from `cardAspectRatio` (0.15 = ±15%). Increase it if tilted cards are not detected. | <code>0.15</code> |
338
+ | **`minCardArea`** | <code>number</code> | Smallest card size, as a fraction of the camera frame (0-1). | <code>0.08</code> |
339
+ | **`maxCardArea`** | <code>number</code> | Largest card size, as a fraction of the camera frame (0-1). | <code>0.9</code> |
340
+ | **`requireCard`** | <code>boolean</code> | Only read text when a card-shaped object is visible. When false, the whole frame is read if no card is found. | <code>true</code> |
336
341
 
337
342
 
338
343
  #### PluginListenerHandle
@@ -1,249 +1,283 @@
1
- package pt.tcgclassifier.cardscanner;
2
-
3
- import android.graphics.Bitmap;
4
- import android.graphics.Matrix;
5
- import android.graphics.Rect;
6
- import android.graphics.RectF;
7
- import android.os.SystemClock;
8
- import android.util.Base64;
9
- import androidx.annotation.NonNull;
10
- import androidx.annotation.Nullable;
11
- import androidx.camera.core.ImageAnalysis;
12
- import androidx.camera.core.ImageProxy;
13
- import com.getcapacitor.JSArray;
14
- import com.getcapacitor.JSObject;
15
- import com.getcapacitor.Logger;
16
- import com.google.android.gms.tasks.Tasks;
17
- import com.google.mlkit.vision.common.InputImage;
18
- import com.google.mlkit.vision.objects.DetectedObject;
19
- import com.google.mlkit.vision.objects.ObjectDetection;
20
- import com.google.mlkit.vision.objects.ObjectDetector;
21
- import com.google.mlkit.vision.objects.defaults.ObjectDetectorOptions;
22
- import com.google.mlkit.vision.text.Text;
23
- import com.google.mlkit.vision.text.TextRecognition;
24
- import com.google.mlkit.vision.text.TextRecognizer;
25
- import com.google.mlkit.vision.text.latin.TextRecognizerOptions;
26
- import java.io.ByteArrayOutputStream;
27
- import java.util.List;
28
- import java.util.regex.Matcher;
29
- import java.util.regex.Pattern;
30
-
31
- /**
32
- * Locates the card in every camera frame (for the on-screen rectangle) and,
33
- * every {@code intervalMs}, runs OCR inside it. A card is reported once the
34
- * same collector number (or name) has been read on several consecutive runs.
35
- */
36
- public class CardDetector implements ImageAnalysis.Analyzer {
37
-
38
- public interface Listener {
39
- void onCardDetected(JSObject result);
40
- }
41
-
42
- public interface BoxListener {
43
- /**
44
- * @param box card bounds normalized (0-1) to the upright frame, or null when no card is visible
45
- */
46
- void onBox(@Nullable RectF box, int frameWidth, int frameHeight);
47
- }
48
-
49
- // e.g. "025/165", "SV001/SV122", "TG05/TG30", "GG01/GG70"
50
- private static final Pattern NUMBER = Pattern.compile("\\b([A-Za-z]{0,4}\\d{1,3}[a-z]?)\\s*/\\s*([A-Za-z]{0,4}\\d{1,3})\\b");
51
- private static final Pattern HEADER = Pattern.compile(
52
- "^(basic|stage\\s*\\d|trainer|item|supporter|stadium|energy|pok[eé]mon|evolves|hp\\b)",
53
- Pattern.CASE_INSENSITIVE
54
- );
55
- private static final Pattern HP_SUFFIX = Pattern.compile("\\s*HP\\s*\\d+\\s*$", Pattern.CASE_INSENSITIVE);
56
- private static final Pattern LETTER = Pattern.compile("\\p{L}");
57
- /** After this long without reading a card, the same card can be reported again. */
58
- private static final long RESET_AFTER_MS = 1500;
59
- /** Objects smaller than this fraction of the frame are ignored. */
60
- private static final float MIN_CARD_AREA = 0.08f;
61
- /** Extra margin around the detected card before running OCR. */
62
- private static final float CROP_MARGIN = 0.04f;
63
-
64
- private final ObjectDetector objectDetector = ObjectDetection.getClient(
65
- new ObjectDetectorOptions.Builder().setDetectorMode(ObjectDetectorOptions.STREAM_MODE).build()
66
- );
67
- private final TextRecognizer recognizer = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS);
68
- private final long intervalMs;
69
- private final int stableFrames;
70
- private final boolean includeImage;
71
- private final Listener listener;
72
- private volatile BoxListener boxListener;
73
-
74
- // Only touched from the analyzer thread.
75
- private long lastOcrAt;
76
- private long lastSeenAt;
77
- private String candidateKey;
78
- private int candidateCount;
79
- private String lastEmittedKey;
80
-
81
- public CardDetector(long intervalMs, int stableFrames, boolean includeImage, Listener listener) {
82
- this.intervalMs = intervalMs;
83
- this.stableFrames = Math.max(1, stableFrames);
84
- this.includeImage = includeImage;
85
- this.listener = listener;
86
- }
87
-
88
- public void setBoxListener(BoxListener boxListener) {
89
- this.boxListener = boxListener;
90
- }
91
-
92
- public void close() {
93
- objectDetector.close();
94
- recognizer.close();
95
- }
96
-
97
- @Override
98
- public void analyze(@NonNull ImageProxy image) {
99
- long now = SystemClock.elapsedRealtime();
100
- int rotation = image.getImageInfo().getRotationDegrees();
101
- Bitmap raw;
102
- try {
103
- raw = image.toBitmap();
104
- } finally {
105
- image.close();
106
- }
107
- int frameW = rotation % 180 == 0 ? raw.getWidth() : raw.getHeight();
108
- int frameH = rotation % 180 == 0 ? raw.getHeight() : raw.getWidth();
109
-
110
- try {
111
- List<DetectedObject> objects = Tasks.await(objectDetector.process(InputImage.fromBitmap(raw, rotation)));
112
- Rect card = pickCard(objects, frameW, frameH);
113
- RectF cardBox = card == null ? null : new RectF(
114
- card.left / (float) frameW,
115
- card.top / (float) frameH,
116
- card.right / (float) frameW,
117
- card.bottom / (float) frameH
118
- );
119
- BoxListener bl = boxListener;
120
- if (bl != null) bl.onBox(cardBox, frameW, frameH);
121
-
122
- if (now - lastOcrAt < intervalMs) return;
123
- lastOcrAt = now;
124
-
125
- Bitmap upright = rotate(raw, rotation);
126
- Bitmap region = card == null ? upright : crop(upright, card);
127
- Text text = Tasks.await(recognizer.process(InputImage.fromBitmap(region, 0)));
128
- handle(text, region, cardBox, now);
129
- } catch (Exception e) {
130
- Logger.error("CardDetector", "Detection failed", e);
131
- }
132
- }
133
-
134
- /** The biggest detected object, if it is large enough to be a card held close to the camera. */
135
- @Nullable
136
- private static Rect pickCard(List<DetectedObject> objects, int frameW, int frameH) {
137
- Rect best = null;
138
- long bestArea = 0;
139
- for (DetectedObject o : objects) {
140
- Rect r = new Rect(o.getBoundingBox());
141
- if (!r.intersect(0, 0, frameW, frameH)) continue;
142
- long area = (long) r.width() * r.height();
143
- if (area > bestArea) {
144
- best = r;
145
- bestArea = area;
146
- }
147
- }
148
- if (best == null || bestArea < MIN_CARD_AREA * frameW * frameH) return null;
149
- return best;
150
- }
151
-
152
- private static Bitmap rotate(Bitmap bitmap, int rotation) {
153
- if (rotation == 0) return bitmap;
154
- Matrix matrix = new Matrix();
155
- matrix.postRotate(rotation);
156
- return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
157
- }
158
-
159
- private static Bitmap crop(Bitmap bitmap, Rect card) {
160
- int mx = Math.round(card.width() * CROP_MARGIN);
161
- int my = Math.round(card.height() * CROP_MARGIN);
162
- int left = Math.max(0, card.left - mx);
163
- int top = Math.max(0, card.top - my);
164
- int right = Math.min(bitmap.getWidth(), card.right + mx);
165
- int bottom = Math.min(bitmap.getHeight(), card.bottom + my);
166
- return Bitmap.createBitmap(bitmap, left, top, right - left, bottom - top);
167
- }
168
-
169
- private void handle(Text text, Bitmap region, @Nullable RectF cardBox, long now) {
170
- float w = region.getWidth();
171
- float h = region.getHeight();
172
- JSArray lines = new JSArray();
173
- String number = null;
174
- String name = null;
175
- int nameTop = Integer.MAX_VALUE;
176
-
177
- for (Text.TextBlock block : text.getTextBlocks()) {
178
- for (Text.Line line : block.getLines()) {
179
- String s = line.getText().trim();
180
- Rect r = line.getBoundingBox();
181
- if (s.isEmpty() || r == null) continue;
182
-
183
- JSObject item = new JSObject();
184
- item.put("text", s);
185
- item.put("box", box(r.left / w, r.top / h, r.width() / w, r.height() / h));
186
- lines.put(item);
187
-
188
- if (number == null) {
189
- Matcher m = NUMBER.matcher(s);
190
- if (m.find()) number = m.group(1) + "/" + m.group(2);
191
- }
192
- if (r.top < nameTop && isNameCandidate(s)) {
193
- name = HP_SUFFIX.matcher(s).replaceAll("").trim();
194
- nameTop = r.top;
195
- }
196
- }
197
- }
198
-
199
- String key = number != null ? number : name;
200
- if (key == null) {
201
- candidateKey = null;
202
- candidateCount = 0;
203
- if (now - lastSeenAt > RESET_AFTER_MS) lastEmittedKey = null;
204
- return;
205
- }
206
- lastSeenAt = now;
207
-
208
- if (key.equals(candidateKey)) {
209
- candidateCount++;
210
- } else {
211
- candidateKey = key;
212
- candidateCount = 1;
213
- }
214
- if (candidateCount < stableFrames || key.equals(lastEmittedKey)) return;
215
- lastEmittedKey = key;
216
-
217
- JSObject result = new JSObject();
218
- result.put("name", name);
219
- result.put("collectorNumber", number);
220
- result.put("text", text.getText());
221
- result.put("lines", lines);
222
- if (cardBox != null) {
223
- result.put("cardBox", box(cardBox.left, cardBox.top, cardBox.width(), cardBox.height()));
224
- }
225
- if (includeImage) {
226
- ByteArrayOutputStream out = new ByteArrayOutputStream();
227
- region.compress(Bitmap.CompressFormat.JPEG, 80, out);
228
- result.put("image", Base64.encodeToString(out.toByteArray(), Base64.NO_WRAP));
229
- }
230
- listener.onCardDetected(result);
231
- }
232
-
233
- private static JSObject box(float x, float y, float width, float height) {
234
- JSObject box = new JSObject();
235
- box.put("x", x);
236
- box.put("y", y);
237
- box.put("width", width);
238
- box.put("height", height);
239
- return box;
240
- }
241
-
242
- private static boolean isNameCandidate(String s) {
243
- if (HEADER.matcher(s).find()) return false;
244
- Matcher m = LETTER.matcher(s);
245
- int letters = 0;
246
- while (m.find()) letters++;
247
- return letters >= 3;
248
- }
249
- }
1
+ package pt.tcgclassifier.cardscanner;
2
+
3
+ import android.graphics.Bitmap;
4
+ import android.graphics.Matrix;
5
+ import android.graphics.Rect;
6
+ import android.graphics.RectF;
7
+ import android.os.SystemClock;
8
+ import android.util.Base64;
9
+ import androidx.annotation.NonNull;
10
+ import androidx.annotation.Nullable;
11
+ import androidx.camera.core.ImageAnalysis;
12
+ import androidx.camera.core.ImageProxy;
13
+ import com.getcapacitor.JSArray;
14
+ import com.getcapacitor.JSObject;
15
+ import com.getcapacitor.Logger;
16
+ import com.google.android.gms.tasks.Tasks;
17
+ import com.google.mlkit.vision.common.InputImage;
18
+ import com.google.mlkit.vision.objects.DetectedObject;
19
+ import com.google.mlkit.vision.objects.ObjectDetection;
20
+ import com.google.mlkit.vision.objects.ObjectDetector;
21
+ import com.google.mlkit.vision.objects.defaults.ObjectDetectorOptions;
22
+ import com.google.mlkit.vision.text.Text;
23
+ import com.google.mlkit.vision.text.TextRecognition;
24
+ import com.google.mlkit.vision.text.TextRecognizer;
25
+ import com.google.mlkit.vision.text.latin.TextRecognizerOptions;
26
+ import java.io.ByteArrayOutputStream;
27
+ import java.util.List;
28
+ import java.util.regex.Matcher;
29
+ import java.util.regex.Pattern;
30
+
31
+ /**
32
+ * Locates the card in every camera frame (for the on-screen rectangle) and,
33
+ * every {@code intervalMs}, runs OCR inside it. A card is reported once the
34
+ * same collector number (or name) has been read on several consecutive runs.
35
+ */
36
+ public class CardDetector implements ImageAnalysis.Analyzer {
37
+
38
+ public interface Listener {
39
+ void onCardDetected(JSObject result);
40
+ }
41
+
42
+ /** Tuning knobs, all overridable from JS through startDetection(). */
43
+ public static class Options {
44
+
45
+ /** Minimum time between two OCR runs. */
46
+ public long intervalMs = 300;
47
+ /** Consecutive OCR runs that must agree before a card is reported. */
48
+ public int stableFrames = 2;
49
+ /** Include a JPEG of the card in the event. */
50
+ public boolean includeImage = false;
51
+ /** Expected width / height of an upright card (63 x 88 mm). */
52
+ public float cardAspectRatio = 63f / 88f;
53
+ /** Allowed relative deviation from cardAspectRatio (0.15 = +/-15%). */
54
+ public float aspectTolerance = 0.15f;
55
+ /** Smallest card size, as a fraction of the frame area. */
56
+ public float minCardArea = 0.08f;
57
+ /** Largest card size, as a fraction of the frame area. */
58
+ public float maxCardArea = 0.9f;
59
+ /** Only run OCR when a card-shaped object is visible. */
60
+ public boolean requireCard = true;
61
+ }
62
+
63
+ public interface BoxListener {
64
+ /**
65
+ * @param box card bounds normalized (0-1) to the upright frame, or null when no card is visible
66
+ */
67
+ void onBox(@Nullable RectF box, int frameWidth, int frameHeight);
68
+ }
69
+
70
+ // e.g. "025/165", "SV001/SV122", "TG05/TG30", "GG01/GG70"
71
+ private static final Pattern NUMBER = Pattern.compile("\\b([A-Za-z]{0,4}\\d{1,3}[a-z]?)\\s*/\\s*([A-Za-z]{0,4}\\d{1,3})\\b");
72
+ private static final Pattern HEADER = Pattern.compile(
73
+ "^(basic|stage\\s*\\d|trainer|item|supporter|stadium|energy|pok[eé]mon|evolves|hp\\b)",
74
+ Pattern.CASE_INSENSITIVE
75
+ );
76
+ private static final Pattern HP_SUFFIX = Pattern.compile("\\s*HP\\s*\\d+\\s*$", Pattern.CASE_INSENSITIVE);
77
+ private static final Pattern LETTER = Pattern.compile("\\p{L}");
78
+ /** After this long without reading a card, the same card can be reported again. */
79
+ private static final long RESET_AFTER_MS = 1500;
80
+ /** Extra margin around the detected card before running OCR. */
81
+ private static final float CROP_MARGIN = 0.04f;
82
+
83
+ private final ObjectDetector objectDetector = ObjectDetection.getClient(
84
+ new ObjectDetectorOptions.Builder().setDetectorMode(ObjectDetectorOptions.STREAM_MODE).build()
85
+ );
86
+ private final TextRecognizer recognizer = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS);
87
+ private final Options options;
88
+ private final Listener listener;
89
+ private volatile BoxListener boxListener;
90
+
91
+ // Only touched from the analyzer thread.
92
+ private long lastOcrAt;
93
+ private long lastSeenAt;
94
+ private String candidateKey;
95
+ private int candidateCount;
96
+ private String lastEmittedKey;
97
+
98
+ public CardDetector(Options options, Listener listener) {
99
+ this.options = options;
100
+ this.options.stableFrames = Math.max(1, options.stableFrames);
101
+ this.listener = listener;
102
+ }
103
+
104
+ public void setBoxListener(BoxListener boxListener) {
105
+ this.boxListener = boxListener;
106
+ }
107
+
108
+ public void close() {
109
+ objectDetector.close();
110
+ recognizer.close();
111
+ }
112
+
113
+ @Override
114
+ public void analyze(@NonNull ImageProxy image) {
115
+ long now = SystemClock.elapsedRealtime();
116
+ int rotation = image.getImageInfo().getRotationDegrees();
117
+ Bitmap raw;
118
+ try {
119
+ raw = image.toBitmap();
120
+ } finally {
121
+ image.close();
122
+ }
123
+ int frameW = rotation % 180 == 0 ? raw.getWidth() : raw.getHeight();
124
+ int frameH = rotation % 180 == 0 ? raw.getHeight() : raw.getWidth();
125
+
126
+ try {
127
+ List<DetectedObject> objects = Tasks.await(objectDetector.process(InputImage.fromBitmap(raw, rotation)));
128
+ Rect card = pickCard(objects, frameW, frameH);
129
+ RectF cardBox = card == null ? null : new RectF(
130
+ card.left / (float) frameW,
131
+ card.top / (float) frameH,
132
+ card.right / (float) frameW,
133
+ card.bottom / (float) frameH
134
+ );
135
+ BoxListener bl = boxListener;
136
+ if (bl != null) bl.onBox(cardBox, frameW, frameH);
137
+
138
+ if (now - lastOcrAt < options.intervalMs) return;
139
+ lastOcrAt = now;
140
+
141
+ if (card == null && options.requireCard) {
142
+ noCardRead(now);
143
+ return;
144
+ }
145
+
146
+ Bitmap upright = rotate(raw, rotation);
147
+ Bitmap region = card == null ? upright : crop(upright, card);
148
+ Text text = Tasks.await(recognizer.process(InputImage.fromBitmap(region, 0)));
149
+ handle(text, region, cardBox, now);
150
+ } catch (Exception e) {
151
+ Logger.error("CardDetector", "Detection failed", e);
152
+ }
153
+ }
154
+
155
+ /** The biggest detected object with the size and upright proportions of a card. */
156
+ @Nullable
157
+ private Rect pickCard(List<DetectedObject> objects, int frameW, int frameH) {
158
+ float frameArea = (float) frameW * frameH;
159
+ float minRatio = options.cardAspectRatio * (1f - options.aspectTolerance);
160
+ float maxRatio = options.cardAspectRatio * (1f + options.aspectTolerance);
161
+ Rect best = null;
162
+ long bestArea = 0;
163
+ for (DetectedObject o : objects) {
164
+ Rect r = new Rect(o.getBoundingBox());
165
+ if (!r.intersect(0, 0, frameW, frameH) || r.height() == 0) continue;
166
+
167
+ long area = (long) r.width() * r.height();
168
+ float fraction = area / frameArea;
169
+ if (fraction < options.minCardArea || fraction > options.maxCardArea) continue;
170
+
171
+ float ratio = r.width() / (float) r.height();
172
+ if (ratio < minRatio || ratio > maxRatio) continue;
173
+
174
+ if (area > bestArea) {
175
+ best = r;
176
+ bestArea = area;
177
+ }
178
+ }
179
+ return best;
180
+ }
181
+
182
+ private static Bitmap rotate(Bitmap bitmap, int rotation) {
183
+ if (rotation == 0) return bitmap;
184
+ Matrix matrix = new Matrix();
185
+ matrix.postRotate(rotation);
186
+ return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
187
+ }
188
+
189
+ private static Bitmap crop(Bitmap bitmap, Rect card) {
190
+ int mx = Math.round(card.width() * CROP_MARGIN);
191
+ int my = Math.round(card.height() * CROP_MARGIN);
192
+ int left = Math.max(0, card.left - mx);
193
+ int top = Math.max(0, card.top - my);
194
+ int right = Math.min(bitmap.getWidth(), card.right + mx);
195
+ int bottom = Math.min(bitmap.getHeight(), card.bottom + my);
196
+ return Bitmap.createBitmap(bitmap, left, top, right - left, bottom - top);
197
+ }
198
+
199
+ private void handle(Text text, Bitmap region, @Nullable RectF cardBox, long now) {
200
+ float w = region.getWidth();
201
+ float h = region.getHeight();
202
+ JSArray lines = new JSArray();
203
+ String number = null;
204
+ String name = null;
205
+ int nameTop = Integer.MAX_VALUE;
206
+
207
+ for (Text.TextBlock block : text.getTextBlocks()) {
208
+ for (Text.Line line : block.getLines()) {
209
+ String s = line.getText().trim();
210
+ Rect r = line.getBoundingBox();
211
+ if (s.isEmpty() || r == null) continue;
212
+
213
+ JSObject item = new JSObject();
214
+ item.put("text", s);
215
+ item.put("box", box(r.left / w, r.top / h, r.width() / w, r.height() / h));
216
+ lines.put(item);
217
+
218
+ if (number == null) {
219
+ Matcher m = NUMBER.matcher(s);
220
+ if (m.find()) number = m.group(1) + "/" + m.group(2);
221
+ }
222
+ if (r.top < nameTop && isNameCandidate(s)) {
223
+ name = HP_SUFFIX.matcher(s).replaceAll("").trim();
224
+ nameTop = r.top;
225
+ }
226
+ }
227
+ }
228
+
229
+ String key = number != null ? number : name;
230
+ if (key == null) {
231
+ noCardRead(now);
232
+ return;
233
+ }
234
+ lastSeenAt = now;
235
+
236
+ if (key.equals(candidateKey)) {
237
+ candidateCount++;
238
+ } else {
239
+ candidateKey = key;
240
+ candidateCount = 1;
241
+ }
242
+ if (candidateCount < options.stableFrames || key.equals(lastEmittedKey)) return;
243
+ lastEmittedKey = key;
244
+
245
+ JSObject result = new JSObject();
246
+ result.put("name", name);
247
+ result.put("collectorNumber", number);
248
+ result.put("text", text.getText());
249
+ result.put("lines", lines);
250
+ if (cardBox != null) {
251
+ result.put("cardBox", box(cardBox.left, cardBox.top, cardBox.width(), cardBox.height()));
252
+ }
253
+ if (options.includeImage) {
254
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
255
+ region.compress(Bitmap.CompressFormat.JPEG, 80, out);
256
+ result.put("image", Base64.encodeToString(out.toByteArray(), Base64.NO_WRAP));
257
+ }
258
+ listener.onCardDetected(result);
259
+ }
260
+
261
+ private void noCardRead(long now) {
262
+ candidateKey = null;
263
+ candidateCount = 0;
264
+ if (now - lastSeenAt > RESET_AFTER_MS) lastEmittedKey = null;
265
+ }
266
+
267
+ private static JSObject box(float x, float y, float width, float height) {
268
+ JSObject box = new JSObject();
269
+ box.put("x", x);
270
+ box.put("y", y);
271
+ box.put("width", width);
272
+ box.put("height", height);
273
+ return box;
274
+ }
275
+
276
+ private static boolean isNameCandidate(String s) {
277
+ if (HEADER.matcher(s).find()) return false;
278
+ Matcher m = LETTER.matcher(s);
279
+ int letters = 0;
280
+ while (m.find()) letters++;
281
+ return letters >= 3;
282
+ }
283
+ }
@@ -91,12 +91,17 @@ public class CardScannerPlugin extends Plugin {
91
91
  @PluginMethod
92
92
  public void startDetection(PluginCall call) {
93
93
  if (!ensureRunning(call)) return;
94
- CardDetector detector = new CardDetector(
95
- call.getInt("intervalMs", 300),
96
- call.getInt("stableFrames", 2),
97
- Boolean.TRUE.equals(call.getBoolean("includeImage", false)),
98
- result -> notifyListeners("cardDetected", result)
99
- );
94
+ CardDetector.Options options = new CardDetector.Options();
95
+ options.intervalMs = call.getInt("intervalMs", (int) options.intervalMs);
96
+ options.stableFrames = call.getInt("stableFrames", options.stableFrames);
97
+ options.includeImage = call.getBoolean("includeImage", options.includeImage);
98
+ options.cardAspectRatio = call.getFloat("cardAspectRatio", options.cardAspectRatio);
99
+ options.aspectTolerance = call.getFloat("aspectTolerance", options.aspectTolerance);
100
+ options.minCardArea = call.getFloat("minCardArea", options.minCardArea);
101
+ options.maxCardArea = call.getFloat("maxCardArea", options.maxCardArea);
102
+ options.requireCard = call.getBoolean("requireCard", options.requireCard);
103
+
104
+ CardDetector detector = new CardDetector(options, result -> notifyListeners("cardDetected", result));
100
105
  getActivity().runOnUiThread(() -> {
101
106
  try {
102
107
  implementation.startDetection(detector);
package/dist/docs.json CHANGED
@@ -336,6 +336,66 @@
336
336
  "docs": "Include a JPEG of the detected card in the event.",
337
337
  "complexTypes": [],
338
338
  "type": "boolean | undefined"
339
+ },
340
+ {
341
+ "name": "cardAspectRatio",
342
+ "tags": [
343
+ {
344
+ "text": "0.716 (63 x 88 mm)",
345
+ "name": "default"
346
+ }
347
+ ],
348
+ "docs": "Expected width / height of an upright card. Only upright cards are\ndetected; a card lying sideways falls outside the tolerance.",
349
+ "complexTypes": [],
350
+ "type": "number | undefined"
351
+ },
352
+ {
353
+ "name": "aspectTolerance",
354
+ "tags": [
355
+ {
356
+ "text": "0.15",
357
+ "name": "default"
358
+ }
359
+ ],
360
+ "docs": "Allowed relative deviation from `cardAspectRatio` (0.15 = ±15%).\nIncrease it if tilted cards are not detected.",
361
+ "complexTypes": [],
362
+ "type": "number | undefined"
363
+ },
364
+ {
365
+ "name": "minCardArea",
366
+ "tags": [
367
+ {
368
+ "text": "0.08",
369
+ "name": "default"
370
+ }
371
+ ],
372
+ "docs": "Smallest card size, as a fraction of the camera frame (0-1).",
373
+ "complexTypes": [],
374
+ "type": "number | undefined"
375
+ },
376
+ {
377
+ "name": "maxCardArea",
378
+ "tags": [
379
+ {
380
+ "text": "0.9",
381
+ "name": "default"
382
+ }
383
+ ],
384
+ "docs": "Largest card size, as a fraction of the camera frame (0-1).",
385
+ "complexTypes": [],
386
+ "type": "number | undefined"
387
+ },
388
+ {
389
+ "name": "requireCard",
390
+ "tags": [
391
+ {
392
+ "text": "true",
393
+ "name": "default"
394
+ }
395
+ ],
396
+ "docs": "Only read text when a card-shaped object is visible. When false, the whole\nframe is read if no card is found.",
397
+ "complexTypes": [],
398
+ "type": "boolean | undefined"
339
399
  }
340
400
  ]
341
401
  },
@@ -48,6 +48,34 @@ export interface DetectionOptions {
48
48
  * @default false
49
49
  */
50
50
  includeImage?: boolean;
51
+ /**
52
+ * Expected width / height of an upright card. Only upright cards are
53
+ * detected; a card lying sideways falls outside the tolerance.
54
+ * @default 0.716 (63 x 88 mm)
55
+ */
56
+ cardAspectRatio?: number;
57
+ /**
58
+ * Allowed relative deviation from `cardAspectRatio` (0.15 = ±15%).
59
+ * Increase it if tilted cards are not detected.
60
+ * @default 0.15
61
+ */
62
+ aspectTolerance?: number;
63
+ /**
64
+ * Smallest card size, as a fraction of the camera frame (0-1).
65
+ * @default 0.08
66
+ */
67
+ minCardArea?: number;
68
+ /**
69
+ * Largest card size, as a fraction of the camera frame (0-1).
70
+ * @default 0.9
71
+ */
72
+ maxCardArea?: number;
73
+ /**
74
+ * Only read text when a card-shaped object is visible. When false, the whole
75
+ * frame is read if no card is found.
76
+ * @default true
77
+ */
78
+ requireCard?: boolean;
51
79
  }
52
80
  /** Normalized rectangle (0-1). */
53
81
  export interface Box {
@@ -1 +1 @@
1
- {"version":3,"file":"definitions.js","sourceRoot":"","sources":["../../src/definitions.ts"],"names":[],"mappings":"","sourcesContent":["import type { PermissionState, PluginListenerHandle } from '@capacitor/core';\n\nexport type CameraPosition = 'rear' | 'front';\n\nexport interface StartOptions {\n /**\n * Which camera to use.\n * @default 'rear'\n */\n position?: CameraPosition;\n}\n\nexport interface CaptureOptions {\n /**\n * JPEG quality (0-100).\n * @default 85\n */\n quality?: number;\n}\n\nexport interface CaptureResult {\n /** Base64 encoded JPEG (without the `data:` prefix). */\n base64: string;\n width: number;\n height: number;\n}\n\nexport interface TorchOptions {\n enabled: boolean;\n}\n\nexport interface ZoomOptions {\n /** Zoom ratio, clamped between the camera's min and max. */\n ratio: number;\n}\n\nexport interface PermissionStatus {\n camera: PermissionState;\n}\n\nexport interface DetectionOptions {\n /**\n * Minimum time between two OCR runs, in milliseconds.\n * The red rectangle is updated on every frame regardless.\n * @default 300\n */\n intervalMs?: number;\n /**\n * How many consecutive OCR runs must read the same card before\n * `cardDetected` fires.\n * @default 2\n */\n stableFrames?: number;\n /**\n * Include a JPEG of the detected card in the event.\n * @default false\n */\n includeImage?: boolean;\n}\n\n/** Normalized rectangle (0-1). */\nexport interface Box {\n x: number;\n y: number;\n width: number;\n height: number;\n}\n\nexport interface TextLine {\n text: string;\n /** Position inside the card image. */\n box: Box;\n}\n\nexport interface CardDetectedEvent {\n /** Best guess for the card name (topmost line of text on the card). */\n name?: string;\n /** Collector number, e.g. `025/165`. */\n collectorNumber?: string;\n /** All text read on the card. */\n text: string;\n lines: TextLine[];\n /** Where the card is in the camera frame. Missing if no card outline was found. */\n cardBox?: Box;\n /** Base64 JPEG of the card, when `includeImage` is true. */\n image?: string;\n}\n\nexport interface CardScannerPlugin {\n /**\n * Opens the camera **behind** the WebView. The WebView background is made\n * transparent, so the HTML page must also use transparent backgrounds\n * where the camera should be visible.\n */\n start(options?: StartOptions): Promise<void>;\n\n /** Stops the camera and removes the preview. */\n stop(): Promise<void>;\n\n /** Takes a photo from the running camera. */\n capture(options?: CaptureOptions): Promise<CaptureResult>;\n\n /** Turns the flashlight on/off. */\n setTorch(options: TorchOptions): Promise<void>;\n\n /** Sets the zoom ratio. */\n setZoom(options: ZoomOptions): Promise<void>;\n\n /** Switches between the rear and front camera. */\n flip(): Promise<void>;\n\n /**\n * Starts looking for cards: draws a red rectangle around the card and reads\n * its text, emitting `cardDetected`. Requires `start()` first. Android only.\n */\n startDetection(options?: DetectionOptions): Promise<void>;\n\n /** Stops card detection and hides the rectangle. */\n stopDetection(): Promise<void>;\n\n /** Fired once per card, when its text has been read consistently. */\n addListener(eventName: 'cardDetected', listener: (event: CardDetectedEvent) => void): Promise<PluginListenerHandle>;\n\n removeAllListeners(): Promise<void>;\n\n checkPermissions(): Promise<PermissionStatus>;\n\n requestPermissions(): Promise<PermissionStatus>;\n}\n"]}
1
+ {"version":3,"file":"definitions.js","sourceRoot":"","sources":["../../src/definitions.ts"],"names":[],"mappings":"","sourcesContent":["import type { PermissionState, PluginListenerHandle } from '@capacitor/core';\n\nexport type CameraPosition = 'rear' | 'front';\n\nexport interface StartOptions {\n /**\n * Which camera to use.\n * @default 'rear'\n */\n position?: CameraPosition;\n}\n\nexport interface CaptureOptions {\n /**\n * JPEG quality (0-100).\n * @default 85\n */\n quality?: number;\n}\n\nexport interface CaptureResult {\n /** Base64 encoded JPEG (without the `data:` prefix). */\n base64: string;\n width: number;\n height: number;\n}\n\nexport interface TorchOptions {\n enabled: boolean;\n}\n\nexport interface ZoomOptions {\n /** Zoom ratio, clamped between the camera's min and max. */\n ratio: number;\n}\n\nexport interface PermissionStatus {\n camera: PermissionState;\n}\n\nexport interface DetectionOptions {\n /**\n * Minimum time between two OCR runs, in milliseconds.\n * The red rectangle is updated on every frame regardless.\n * @default 300\n */\n intervalMs?: number;\n /**\n * How many consecutive OCR runs must read the same card before\n * `cardDetected` fires.\n * @default 2\n */\n stableFrames?: number;\n /**\n * Include a JPEG of the detected card in the event.\n * @default false\n */\n includeImage?: boolean;\n /**\n * Expected width / height of an upright card. Only upright cards are\n * detected; a card lying sideways falls outside the tolerance.\n * @default 0.716 (63 x 88 mm)\n */\n cardAspectRatio?: number;\n /**\n * Allowed relative deviation from `cardAspectRatio` (0.15 = ±15%).\n * Increase it if tilted cards are not detected.\n * @default 0.15\n */\n aspectTolerance?: number;\n /**\n * Smallest card size, as a fraction of the camera frame (0-1).\n * @default 0.08\n */\n minCardArea?: number;\n /**\n * Largest card size, as a fraction of the camera frame (0-1).\n * @default 0.9\n */\n maxCardArea?: number;\n /**\n * Only read text when a card-shaped object is visible. When false, the whole\n * frame is read if no card is found.\n * @default true\n */\n requireCard?: boolean;\n}\n\n/** Normalized rectangle (0-1). */\nexport interface Box {\n x: number;\n y: number;\n width: number;\n height: number;\n}\n\nexport interface TextLine {\n text: string;\n /** Position inside the card image. */\n box: Box;\n}\n\nexport interface CardDetectedEvent {\n /** Best guess for the card name (topmost line of text on the card). */\n name?: string;\n /** Collector number, e.g. `025/165`. */\n collectorNumber?: string;\n /** All text read on the card. */\n text: string;\n lines: TextLine[];\n /** Where the card is in the camera frame. Missing if no card outline was found. */\n cardBox?: Box;\n /** Base64 JPEG of the card, when `includeImage` is true. */\n image?: string;\n}\n\nexport interface CardScannerPlugin {\n /**\n * Opens the camera **behind** the WebView. The WebView background is made\n * transparent, so the HTML page must also use transparent backgrounds\n * where the camera should be visible.\n */\n start(options?: StartOptions): Promise<void>;\n\n /** Stops the camera and removes the preview. */\n stop(): Promise<void>;\n\n /** Takes a photo from the running camera. */\n capture(options?: CaptureOptions): Promise<CaptureResult>;\n\n /** Turns the flashlight on/off. */\n setTorch(options: TorchOptions): Promise<void>;\n\n /** Sets the zoom ratio. */\n setZoom(options: ZoomOptions): Promise<void>;\n\n /** Switches between the rear and front camera. */\n flip(): Promise<void>;\n\n /**\n * Starts looking for cards: draws a red rectangle around the card and reads\n * its text, emitting `cardDetected`. Requires `start()` first. Android only.\n */\n startDetection(options?: DetectionOptions): Promise<void>;\n\n /** Stops card detection and hides the rectangle. */\n stopDetection(): Promise<void>;\n\n /** Fired once per card, when its text has been read consistently. */\n addListener(eventName: 'cardDetected', listener: (event: CardDetectedEvent) => void): Promise<PluginListenerHandle>;\n\n removeAllListeners(): Promise<void>;\n\n checkPermissions(): Promise<PermissionStatus>;\n\n requestPermissions(): Promise<PermissionStatus>;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "capacitor-card-scanner",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Recognize a pokemon card with camera open in background",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",