Skip to content

Commit 353ede1

Browse files
committed
fix: android barcode detection, formats, ios detection & torch
fixes: #14 fixes: #12 fixes: #11 fixes: #10 fixes: #9 fixes: #8 fixes: #7
1 parent c7ee498 commit 353ede1

File tree

22 files changed

+464
-312
lines changed

22 files changed

+464
-312
lines changed
+33-33
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
{
2-
"name": "@nativescript/mlkit-barcode-scanning",
3-
"version": "1.0.0-alpha.2",
4-
"description": "NativeScript MLKit Barcode Scanner module",
5-
"main": "index",
6-
"typings": "index.d.ts",
7-
"nativescript": {
8-
"platforms": {
9-
"ios": "6.0.0",
10-
"android": "6.0.0"
11-
}
12-
},
13-
"repository": {
14-
"type": "git",
15-
"url": "https://github.com./NativeScript/mlkit.git"
16-
},
17-
"keywords": [
18-
"NativeScript",
19-
"JavaScript",
20-
"TypeScript",
21-
"iOS",
22-
"Android"
23-
],
24-
"author": {
25-
"name": "NativeScript",
26-
"email": "[email protected]"
27-
},
28-
"bugs": {
29-
"url": "https://github.com./NativeScript/mlkit/issues"
30-
},
31-
"license": "Apache-2.0",
32-
"homepage": "https://github.com./NativeScript/mlkit",
33-
"readmeFilename": "README.md",
34-
"bootstrapper": "@nativescript/plugin-seed"
2+
"name": "@nativescript/mlkit-barcode-scanning",
3+
"version": "1.0.0-alpha.3",
4+
"description": "NativeScript MLKit Barcode Scanner module",
5+
"main": "index",
6+
"typings": "index.d.ts",
7+
"nativescript": {
8+
"platforms": {
9+
"ios": "6.0.0",
10+
"android": "6.0.0"
11+
}
12+
},
13+
"repository": {
14+
"type": "git",
15+
"url": "https://github.com./NativeScript/mlkit.git"
16+
},
17+
"keywords": [
18+
"NativeScript",
19+
"JavaScript",
20+
"TypeScript",
21+
"iOS",
22+
"Android"
23+
],
24+
"author": {
25+
"name": "NativeScript",
26+
"email": "[email protected]"
27+
},
28+
"bugs": {
29+
"url": "https://github.com./NativeScript/mlkit/issues"
30+
},
31+
"license": "Apache-2.0",
32+
"homepage": "https://github.com./NativeScript/mlkit",
33+
"readmeFilename": "README.md",
34+
"bootstrapper": "@nativescript/plugin-seed"
3535
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
dependencies {
2-
implementation 'io.github.triniwiz.fancycamera:barcodeScanning:1.0.0-alpha.2'
2+
implementation 'io.github.triniwiz.fancycamera:barcodeScanning:1.0.0-alpha.3'
3+
implementation 'com.google.mlkit:barcode-scanning:17.0.1'
34
}

packages/mlkit-core/index.android.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ export class MLKitView extends MLKitViewBase {
328328
}
329329
let formats;
330330
if (Array.isArray(value)) {
331-
if (value.indexOf(BarcodeFormats.ALL)) {
331+
if (value.indexOf(BarcodeFormats.ALL) !== -1) {
332332
formats = Array.create('io.github.triniwiz.fancycamera.barcodescanning.BarcodeScanner$BarcodeFormat', 1);
333333
formats[0] = io.github.triniwiz.fancycamera.barcodescanning.BarcodeScanner.BarcodeFormat.ALL;
334334
} else {
@@ -369,7 +369,7 @@ export class MLKitView extends MLKitViewBase {
369369
this.#barcodeScannerOptions.setBarcodeFormat(formats);
370370
}
371371

372-
this.#getFancyCamera().setBarcodeScannerOptions(this.#barcodeScannerOptions);
372+
this.#camera.setBarcodeScannerOptions(this.#barcodeScannerOptions);
373373
}
374374

375375
[faceDetectionTrackingEnabledProperty.setNative](value) {
@@ -382,7 +382,7 @@ export class MLKitView extends MLKitViewBase {
382382

383383
this.#faceDetectionOptions.setFaceTracking(value);
384384

385-
this.#getFancyCamera().setFaceDetectionOptions(this.#faceDetectionOptions);
385+
this.#camera.setFaceDetectionOptions(this.#faceDetectionOptions);
386386
}
387387

388388
[faceDetectionMinFaceSizeProperty.setNative](value) {
@@ -395,7 +395,7 @@ export class MLKitView extends MLKitViewBase {
395395
}
396396

397397
this.#faceDetectionOptions.setMinimumFaceSize(value);
398-
this.#getFancyCamera().setFaceDetectionOptions(this.#faceDetectionOptions);
398+
this.#camera.setFaceDetectionOptions(this.#faceDetectionOptions);
399399
}
400400

401401
[faceDetectionPerformanceModeProperty.setNative](value) {
@@ -408,7 +408,7 @@ export class MLKitView extends MLKitViewBase {
408408
}
409409

410410
this.#faceDetectionOptions.setMinimumFaceSize(value);
411-
this.#getFancyCamera().setFaceDetectionOptions(this.#faceDetectionOptions);
411+
this.#camera.setFaceDetectionOptions(this.#faceDetectionOptions);
412412
}
413413

414414
[imageLabelerConfidenceThresholdProperty.setNative](value) {
@@ -420,7 +420,7 @@ export class MLKitView extends MLKitViewBase {
420420
}
421421

422422
this.#imageLabelerOptions.setConfidenceThreshold(value);
423-
this.#getFancyCamera().setImageLabelingOptions(this.#imageLabelerOptions);
423+
this.#camera.setImageLabelingOptions(this.#imageLabelerOptions);
424424
}
425425

426426
[objectDetectionClassifyProperty.setNative](value) {
@@ -433,7 +433,7 @@ export class MLKitView extends MLKitViewBase {
433433
}
434434

435435
this.#objectDetectionOptions.setClassification(value);
436-
this.#getFancyCamera().setObjectDetectionOptions(this.#objectDetectionOptions);
436+
this.#camera.setObjectDetectionOptions(this.#objectDetectionOptions);
437437
}
438438

439439
[objectDetectionMultipleProperty.setNative](value) {
@@ -446,7 +446,7 @@ export class MLKitView extends MLKitViewBase {
446446
}
447447

448448
this.#objectDetectionOptions.setMultiple(value);
449-
this.#getFancyCamera().setObjectDetectionOptions(this.#objectDetectionOptions);
449+
this.#camera.setObjectDetectionOptions(this.#objectDetectionOptions);
450450
}
451451

452452

packages/mlkit-core/index.ios.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class MLKitView extends MLKitViewBase {
3737
#poseDetectionOptions: MLKPoseDetectorOptions;
3838
#selfieSegmentationOptions: MLKSelfieSegmenterOptions;
3939

40-
#mlkitHelper;
40+
#mlkitHelper: TNSMLKitHelper;
4141

4242
constructor() {
4343
super();
@@ -110,7 +110,11 @@ export class MLKitView extends MLKitViewBase {
110110

111111

112112
[torchOnProperty.setNative](value: boolean) {
113-
this.#mlkitHelper.torch = value;
113+
if (value) {
114+
this.#mlkitHelper.torchMode = TNSMLKitTorchMode.On;
115+
} else {
116+
this.#mlkitHelper.torchMode = TNSMLKitTorchMode.Off;
117+
}
114118
}
115119

116120
[pauseProperty.setNative](value: boolean) {
@@ -241,7 +245,7 @@ export class MLKitView extends MLKitViewBase {
241245
}
242246
let formats: MLKBarcodeFormat = 0;
243247
if (Array.isArray(value)) {
244-
if (value.indexOf(BarcodeFormats.ALL)) {
248+
if (value.indexOf(BarcodeFormats.ALL) !== -1) {
245249
formats = MLKBarcodeFormat.All;
246250
} else {
247251
value.forEach(format => {
@@ -409,7 +413,9 @@ export class MLKitView extends MLKitViewBase {
409413

410414
public startPreview(): void {
411415
this.#mlkitHelper.openCamera();
412-
this.#mlkitHelper.startPreview();
416+
if (!this.pause) {
417+
this.#mlkitHelper.startPreview();
418+
}
413419
}
414420

415421

@@ -495,7 +501,7 @@ export function detectWithStillImage(image: any, options?: StillImageDetectionOp
495501

496502

497503
TNSML.processImage(nativeImage, {
498-
...options || {} , detectorType: type
504+
...options || {}, detectorType: type
499505
} as any, (ret: NSArray<TNSMLResult>) => {
500506
const result = {}
501507
const count = ret.count;

packages/mlkit-core/package.json

+33-33
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
{
2-
"name": "@nativescript/mlkit-core",
3-
"version": "1.0.0-alpha.2",
4-
"description": "NativeScript MLKit Core",
5-
"main": "index",
6-
"typings": "index.d.ts",
7-
"nativescript": {
8-
"platforms": {
9-
"ios": "6.0.0",
10-
"android": "6.0.0"
11-
}
12-
},
13-
"repository": {
14-
"type": "git",
15-
"url": "https://github.com./NativeScript/mlkit.git"
16-
},
17-
"keywords": [
18-
"NativeScript",
19-
"JavaScript",
20-
"TypeScript",
21-
"iOS",
22-
"Android"
23-
],
24-
"author": {
25-
"name": "NativeScript",
26-
"email": "[email protected]"
27-
},
28-
"bugs": {
29-
"url": "https://github.com./NativeScript/mlkit/issues"
30-
},
31-
"license": "Apache-2.0",
32-
"homepage": "https://github.com./NativeScript/mlkit",
33-
"readmeFilename": "README.md",
34-
"bootstrapper": "@nativescript/plugin-seed"
2+
"name": "@nativescript/mlkit-core",
3+
"version": "1.0.0-alpha.3",
4+
"description": "NativeScript MLKit Core",
5+
"main": "index",
6+
"typings": "index.d.ts",
7+
"nativescript": {
8+
"platforms": {
9+
"ios": "6.0.0",
10+
"android": "6.0.0"
11+
}
12+
},
13+
"repository": {
14+
"type": "git",
15+
"url": "https://github.com./NativeScript/mlkit.git"
16+
},
17+
"keywords": [
18+
"NativeScript",
19+
"JavaScript",
20+
"TypeScript",
21+
"iOS",
22+
"Android"
23+
],
24+
"author": {
25+
"name": "NativeScript",
26+
"email": "[email protected]"
27+
},
28+
"bugs": {
29+
"url": "https://github.com./NativeScript/mlkit/issues"
30+
},
31+
"license": "Apache-2.0",
32+
"homepage": "https://github.com./NativeScript/mlkit",
33+
"readmeFilename": "README.md",
34+
"bootstrapper": "@nativescript/plugin-seed"
3535
}

packages/mlkit-core/platforms/android/include.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ allprojects {
66
}
77

88
dependencies {
9-
implementation 'io.github.triniwiz:fancycamera:3.0.0-alpha.31'
9+
implementation 'io.github.triniwiz:fancycamera:3.0.0-alpha.32'
1010
}
1111

1212

packages/mlkit-core/platforms/ios/src/TNSMLKitHelper.swift

+82-9
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,57 @@ enum TNSMLKitDetectionType: Int, RawRepresentable {
235235
}
236236

237237

238+
239+
240+
241+
@objc(TNSMLKitTorchMode)
242+
public enum TNSMLKitTorchMode: Int, RawRepresentable {
243+
case Off
244+
case On
245+
case Auto
246+
public typealias RawValue = UInt32
247+
248+
public var rawValue: RawValue {
249+
switch self {
250+
case .Off:
251+
return 0
252+
case .On:
253+
return 1
254+
case .Auto:
255+
return 2
256+
}
257+
}
258+
259+
260+
public init?(rawValue: RawValue) {
261+
switch rawValue {
262+
case 0:
263+
self = .Off
264+
case 1:
265+
self = .On
266+
case 2:
267+
self = .Auto
268+
default:
269+
return nil
270+
}
271+
}
272+
273+
274+
public init?(string: String) {
275+
switch string {
276+
case "off":
277+
self = .Off
278+
case "on":
279+
self = .On
280+
case "auto":
281+
self = .Auto
282+
default:
283+
return nil
284+
}
285+
}
286+
}
287+
288+
238289
@objc(TNSMLKitHelper)
239290
@objcMembers
240291
public class TNSMLKitHelper: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate {
@@ -262,12 +313,28 @@ public class TNSMLKitHelper: NSObject, AVCaptureVideoDataOutputSampleBufferDeleg
262313
let encoder = JSONEncoder()
263314
var detectorType = TNSMLKitDetectionType.All
264315

265-
public var flashMode: Bool = false {
316+
public var torchMode: TNSMLKitTorchMode = .Off {
266317
didSet {
267-
if(flashMode){
268-
self.videoInput?.device.torchMode = .auto
269-
}else {
270-
self.videoInput?.device.torchMode = .on
318+
guard self.videoInput?.device != nil else {
319+
return
320+
}
321+
322+
switch(torchMode){
323+
case .Off:
324+
if(self.videoInput!.device.isTorchModeSupported(.off)){
325+
self.videoInput!.device.torchMode = .off
326+
}
327+
break
328+
case .On:
329+
if(self.videoInput!.device.isTorchModeSupported(.on)){
330+
self.videoInput!.device.torchMode = .on
331+
}
332+
break
333+
case .Auto:
334+
if(self.videoInput!.device.isTorchModeSupported(.auto)){
335+
self.videoInput!.device.torchMode = .auto
336+
}
337+
break
271338
}
272339
}
273340
}
@@ -383,10 +450,16 @@ public class TNSMLKitHelper: NSObject, AVCaptureVideoDataOutputSampleBufferDeleg
383450
return
384451
}
385452

386-
if(self.flashMode){
387-
videoInput?.device.torchMode = .auto
388-
}else {
389-
videoInput?.device.torchMode = .on
453+
switch(torchMode){
454+
case .Off:
455+
videoInput!.device.torchMode = .off
456+
break
457+
case .On:
458+
videoInput!.device.torchMode = .on
459+
break
460+
case .Auto:
461+
videoInput!.device.torchMode = .auto
462+
break
390463
}
391464

392465

0 commit comments

Comments
 (0)