Skip to content

Commit

Permalink
fix: android barcode detection, formats, ios detection & torch
Browse files Browse the repository at this point in the history
fixes: #14
fixes: #12
fixes: #11
fixes: #10
fixes: #9
fixes: #8
fixes: #7
  • Loading branch information
triniwiz committed Dec 21, 2021
1 parent c7ee498 commit 353ede1
Show file tree
Hide file tree
Showing 22 changed files with 464 additions and 312 deletions.
66 changes: 33 additions & 33 deletions packages/mlkit-barcode-scanning/package.json
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
{
"name": "@nativescript/mlkit-barcode-scanning",
"version": "1.0.0-alpha.2",
"description": "NativeScript MLKit Barcode Scanner module",
"main": "index",
"typings": "index.d.ts",
"nativescript": {
"platforms": {
"ios": "6.0.0",
"android": "6.0.0"
}
},
"repository": {
"type": "git",
"url": "https://github.com/NativeScript/mlkit.git"
},
"keywords": [
"NativeScript",
"JavaScript",
"TypeScript",
"iOS",
"Android"
],
"author": {
"name": "NativeScript",
"email": "oss@nativescript.org"
},
"bugs": {
"url": "https://github.com/NativeScript/mlkit/issues"
},
"license": "Apache-2.0",
"homepage": "https://github.com/NativeScript/mlkit",
"readmeFilename": "README.md",
"bootstrapper": "@nativescript/plugin-seed"
"name": "@nativescript/mlkit-barcode-scanning",
"version": "1.0.0-alpha.3",
"description": "NativeScript MLKit Barcode Scanner module",
"main": "index",
"typings": "index.d.ts",
"nativescript": {
"platforms": {
"ios": "6.0.0",
"android": "6.0.0"
}
},
"repository": {
"type": "git",
"url": "https://github.com/NativeScript/mlkit.git"
},
"keywords": [
"NativeScript",
"JavaScript",
"TypeScript",
"iOS",
"Android"
],
"author": {
"name": "NativeScript",
"email": "oss@nativescript.org"
},
"bugs": {
"url": "https://github.com/NativeScript/mlkit/issues"
},
"license": "Apache-2.0",
"homepage": "https://github.com/NativeScript/mlkit",
"readmeFilename": "README.md",
"bootstrapper": "@nativescript/plugin-seed"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dependencies {
implementation 'io.github.triniwiz.fancycamera:barcodeScanning:1.0.0-alpha.2'
implementation 'io.github.triniwiz.fancycamera:barcodeScanning:1.0.0-alpha.3'
implementation 'com.google.mlkit:barcode-scanning:17.0.1'
}
16 changes: 8 additions & 8 deletions packages/mlkit-core/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ export class MLKitView extends MLKitViewBase {
}
let formats;
if (Array.isArray(value)) {
if (value.indexOf(BarcodeFormats.ALL)) {
if (value.indexOf(BarcodeFormats.ALL) !== -1) {
formats = Array.create('io.github.triniwiz.fancycamera.barcodescanning.BarcodeScanner$BarcodeFormat', 1);
formats[0] = io.github.triniwiz.fancycamera.barcodescanning.BarcodeScanner.BarcodeFormat.ALL;
} else {
Expand Down Expand Up @@ -369,7 +369,7 @@ export class MLKitView extends MLKitViewBase {
this.#barcodeScannerOptions.setBarcodeFormat(formats);
}

this.#getFancyCamera().setBarcodeScannerOptions(this.#barcodeScannerOptions);
this.#camera.setBarcodeScannerOptions(this.#barcodeScannerOptions);
}

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

this.#faceDetectionOptions.setFaceTracking(value);

this.#getFancyCamera().setFaceDetectionOptions(this.#faceDetectionOptions);
this.#camera.setFaceDetectionOptions(this.#faceDetectionOptions);
}

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

this.#faceDetectionOptions.setMinimumFaceSize(value);
this.#getFancyCamera().setFaceDetectionOptions(this.#faceDetectionOptions);
this.#camera.setFaceDetectionOptions(this.#faceDetectionOptions);
}

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

this.#faceDetectionOptions.setMinimumFaceSize(value);
this.#getFancyCamera().setFaceDetectionOptions(this.#faceDetectionOptions);
this.#camera.setFaceDetectionOptions(this.#faceDetectionOptions);
}

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

this.#imageLabelerOptions.setConfidenceThreshold(value);
this.#getFancyCamera().setImageLabelingOptions(this.#imageLabelerOptions);
this.#camera.setImageLabelingOptions(this.#imageLabelerOptions);
}

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

this.#objectDetectionOptions.setClassification(value);
this.#getFancyCamera().setObjectDetectionOptions(this.#objectDetectionOptions);
this.#camera.setObjectDetectionOptions(this.#objectDetectionOptions);
}

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

this.#objectDetectionOptions.setMultiple(value);
this.#getFancyCamera().setObjectDetectionOptions(this.#objectDetectionOptions);
this.#camera.setObjectDetectionOptions(this.#objectDetectionOptions);
}


Expand Down
16 changes: 11 additions & 5 deletions packages/mlkit-core/index.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class MLKitView extends MLKitViewBase {
#poseDetectionOptions: MLKPoseDetectorOptions;
#selfieSegmentationOptions: MLKSelfieSegmenterOptions;

#mlkitHelper;
#mlkitHelper: TNSMLKitHelper;

constructor() {
super();
Expand Down Expand Up @@ -110,7 +110,11 @@ export class MLKitView extends MLKitViewBase {


[torchOnProperty.setNative](value: boolean) {
this.#mlkitHelper.torch = value;
if (value) {
this.#mlkitHelper.torchMode = TNSMLKitTorchMode.On;
} else {
this.#mlkitHelper.torchMode = TNSMLKitTorchMode.Off;
}
}

[pauseProperty.setNative](value: boolean) {
Expand Down Expand Up @@ -241,7 +245,7 @@ export class MLKitView extends MLKitViewBase {
}
let formats: MLKBarcodeFormat = 0;
if (Array.isArray(value)) {
if (value.indexOf(BarcodeFormats.ALL)) {
if (value.indexOf(BarcodeFormats.ALL) !== -1) {
formats = MLKBarcodeFormat.All;
} else {
value.forEach(format => {
Expand Down Expand Up @@ -409,7 +413,9 @@ export class MLKitView extends MLKitViewBase {

public startPreview(): void {
this.#mlkitHelper.openCamera();
this.#mlkitHelper.startPreview();
if (!this.pause) {
this.#mlkitHelper.startPreview();
}
}


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


TNSML.processImage(nativeImage, {
...options || {} , detectorType: type
...options || {}, detectorType: type
} as any, (ret: NSArray<TNSMLResult>) => {
const result = {}
const count = ret.count;
Expand Down
66 changes: 33 additions & 33 deletions packages/mlkit-core/package.json
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
{
"name": "@nativescript/mlkit-core",
"version": "1.0.0-alpha.2",
"description": "NativeScript MLKit Core",
"main": "index",
"typings": "index.d.ts",
"nativescript": {
"platforms": {
"ios": "6.0.0",
"android": "6.0.0"
}
},
"repository": {
"type": "git",
"url": "https://github.com/NativeScript/mlkit.git"
},
"keywords": [
"NativeScript",
"JavaScript",
"TypeScript",
"iOS",
"Android"
],
"author": {
"name": "NativeScript",
"email": "oss@nativescript.org"
},
"bugs": {
"url": "https://github.com/NativeScript/mlkit/issues"
},
"license": "Apache-2.0",
"homepage": "https://github.com/NativeScript/mlkit",
"readmeFilename": "README.md",
"bootstrapper": "@nativescript/plugin-seed"
"name": "@nativescript/mlkit-core",
"version": "1.0.0-alpha.3",
"description": "NativeScript MLKit Core",
"main": "index",
"typings": "index.d.ts",
"nativescript": {
"platforms": {
"ios": "6.0.0",
"android": "6.0.0"
}
},
"repository": {
"type": "git",
"url": "https://github.com/NativeScript/mlkit.git"
},
"keywords": [
"NativeScript",
"JavaScript",
"TypeScript",
"iOS",
"Android"
],
"author": {
"name": "NativeScript",
"email": "oss@nativescript.org"
},
"bugs": {
"url": "https://github.com/NativeScript/mlkit/issues"
},
"license": "Apache-2.0",
"homepage": "https://github.com/NativeScript/mlkit",
"readmeFilename": "README.md",
"bootstrapper": "@nativescript/plugin-seed"
}
2 changes: 1 addition & 1 deletion packages/mlkit-core/platforms/android/include.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ allprojects {
}

dependencies {
implementation 'io.github.triniwiz:fancycamera:3.0.0-alpha.31'
implementation 'io.github.triniwiz:fancycamera:3.0.0-alpha.32'
}


Expand Down
91 changes: 82 additions & 9 deletions packages/mlkit-core/platforms/ios/src/TNSMLKitHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,57 @@ enum TNSMLKitDetectionType: Int, RawRepresentable {
}





@objc(TNSMLKitTorchMode)
public enum TNSMLKitTorchMode: Int, RawRepresentable {
case Off
case On
case Auto
public typealias RawValue = UInt32

public var rawValue: RawValue {
switch self {
case .Off:
return 0
case .On:
return 1
case .Auto:
return 2
}
}


public init?(rawValue: RawValue) {
switch rawValue {
case 0:
self = .Off
case 1:
self = .On
case 2:
self = .Auto
default:
return nil
}
}


public init?(string: String) {
switch string {
case "off":
self = .Off
case "on":
self = .On
case "auto":
self = .Auto
default:
return nil
}
}
}


@objc(TNSMLKitHelper)
@objcMembers
public class TNSMLKitHelper: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate {
Expand Down Expand Up @@ -262,12 +313,28 @@ public class TNSMLKitHelper: NSObject, AVCaptureVideoDataOutputSampleBufferDeleg
let encoder = JSONEncoder()
var detectorType = TNSMLKitDetectionType.All

public var flashMode: Bool = false {
public var torchMode: TNSMLKitTorchMode = .Off {
didSet {
if(flashMode){
self.videoInput?.device.torchMode = .auto
}else {
self.videoInput?.device.torchMode = .on
guard self.videoInput?.device != nil else {
return
}

switch(torchMode){
case .Off:
if(self.videoInput!.device.isTorchModeSupported(.off)){
self.videoInput!.device.torchMode = .off
}
break
case .On:
if(self.videoInput!.device.isTorchModeSupported(.on)){
self.videoInput!.device.torchMode = .on
}
break
case .Auto:
if(self.videoInput!.device.isTorchModeSupported(.auto)){
self.videoInput!.device.torchMode = .auto
}
break
}
}
}
Expand Down Expand Up @@ -383,10 +450,16 @@ public class TNSMLKitHelper: NSObject, AVCaptureVideoDataOutputSampleBufferDeleg
return
}

if(self.flashMode){
videoInput?.device.torchMode = .auto
}else {
videoInput?.device.torchMode = .on
switch(torchMode){
case .Off:
videoInput!.device.torchMode = .off
break
case .On:
videoInput!.device.torchMode = .on
break
case .Auto:
videoInput!.device.torchMode = .auto
break
}


Expand Down
Loading

0 comments on commit 353ede1

Please sign in to comment.