Skip to content

Commit a39ac7e

Browse files
committed
feat: pause & torch
1 parent 485aa80 commit a39ac7e

File tree

8 files changed

+128
-8
lines changed

8 files changed

+128
-8
lines changed

apps/demo/src/plugin-demos/mlkit-core.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function navigatingTo(args: EventData) {
1515
export class DemoModel extends DemoSharedMlkitCore {
1616
camera: MLKitView;
1717
detectorType = "all";
18-
18+
isPaused = false;
1919
onLoaded(args) {
2020
this.camera = args.object;
2121
}
@@ -53,5 +53,10 @@ export class DemoModel extends DemoSharedMlkitCore {
5353
}
5454
})
5555
}
56+
57+
togglePause(args) {
58+
this.camera.pause = !this.camera.pause;
59+
this.set('isPaused', this.camera.pause);
60+
}
5661
}
5762

apps/demo/src/plugin-demos/mlkit-core.xml

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
<ActionBar title="mlkit-core" icon="" class="action-bar">
44
</ActionBar>
55
</Page.actionBar>
6-
<GridLayout rows="*,auto,auto,auto,auto" height="100%">
7-
<ui:MLKitView height="100%" rowSpan="3" loaded="{{ onLoaded }}" cameraPosition="back" detectionType="{{ detectorType }}" detection="{{ onDetection }}"/>
6+
<GridLayout rows="*,auto,auto,auto,auto,auto,auto" height="100%">
7+
<ui:MLKitView pause="true" height="100%" rowSpan="3" loaded="{{ onLoaded }}" cameraPosition="back" detectionType="{{ detectorType }}" detection="{{ onDetection }}"/>
88
<Button row="1" height="40" text="Toggle Camera" tap="{{ toggleCamera }}" />
99
<Button row="2" height="40" text="Request Camera Permission" tap="{{ requestPermission }}" />
1010
<Label row="3" text="{{'Detecting ' + detectorType }}"/>
1111
<Button row="4" height="40" text="Change Detector Type" tap="{{ changeType }}" />
12+
<Label row="5" text="{{'isPaused : ' + isPaused }}"/>
13+
<Button row="6" height="40" text="Toggle Pause" tap="{{ togglePause }}" />
1214
</GridLayout>
1315
</Page>

packages/mlkit-core/common.ts

+18
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ export class MLKitViewBase extends ContainerView {
5454
imageLabelerConfidenceThreshold: number;
5555
objectDetectionMultiple: boolean;
5656
objectDetectionClassify: boolean;
57+
torchOn: boolean;
58+
pause: boolean;
5759
}
5860

5961
export const cameraPositionProperty = new Property<MLKitViewBase, CameraPosition>({
@@ -128,4 +130,20 @@ export const objectDetectionClassifyProperty = new Property<MLKitViewBase, boole
128130
objectDetectionClassifyProperty.register(MLKitViewBase);
129131

130132

133+
export const torchOnProperty = new Property<MLKitViewBase, boolean>({
134+
name: 'torchOn',
135+
defaultValue: false,
136+
valueConverter: booleanConverter
137+
});
138+
139+
torchOnProperty.register(MLKitViewBase);
140+
141+
142+
143+
export const pauseProperty = new Property<MLKitViewBase, boolean>({
144+
name: 'pause',
145+
defaultValue: false,
146+
valueConverter: booleanConverter
147+
});
131148

149+
pauseProperty.register(MLKitViewBase);

packages/mlkit-core/index.android.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BarcodeFormats, barcodeFormatsProperty, CameraPosition, cameraPositionProperty, DetectionType, detectionTypeProperty, faceDetectionMinFaceSizeProperty, faceDetectionPerformanceModeProperty, faceDetectionTrackingEnabledProperty, imageLabelerConfidenceThresholdProperty, MLKitViewBase, objectDetectionClassifyProperty, objectDetectionMultipleProperty } from "./common";
1+
import { BarcodeFormats, barcodeFormatsProperty, CameraPosition, cameraPositionProperty, DetectionType, detectionTypeProperty, faceDetectionMinFaceSizeProperty, faceDetectionPerformanceModeProperty, faceDetectionTrackingEnabledProperty, imageLabelerConfidenceThresholdProperty, MLKitViewBase, objectDetectionClassifyProperty, objectDetectionMultipleProperty, pauseProperty, torchOnProperty } from "./common";
22
import { Application, Device, Utils, AndroidActivityRequestPermissionsEventData } from '@nativescript/core';
33
import lazy from '@nativescript/core/utils/lazy';
44

@@ -19,6 +19,10 @@ const IMAGE_LABELING_SUPPORTED = lazy(() => typeof io.github.triniwiz.fancycamer
1919
const OBJECT_DETECTION_SUPPORTED = lazy(() => typeof io.github.triniwiz.fancycamera?.objectdetection?.ObjectDetection);
2020
const POSE_DETECTION_SUPPORTED = lazy(() => typeof io.github.triniwiz.fancycamera?.posedetection?.PoseDetection);
2121

22+
23+
const TORCH_MODE_ON = lazy(() => io.github.triniwiz.fancycamera.CameraFlashMode.TORCH);
24+
const TORCH_MODE_OFF = lazy(() => io.github.triniwiz.fancycamera.CameraFlashMode.OFF);
25+
2226
export { BarcodeFormats, barcodeFormatsProperty, CameraPosition, cameraPositionProperty, DetectionType, faceDetectionMinFaceSizeProperty, faceDetectionPerformanceModeProperty, faceDetectionTrackingEnabledProperty, imageLabelerConfidenceThresholdProperty, objectDetectionClassifyProperty, objectDetectionMultipleProperty } from './common';
2327

2428
export class MLKitView extends MLKitViewBase {
@@ -32,6 +36,9 @@ export class MLKitView extends MLKitViewBase {
3236
#onObjectListener: io.github.triniwiz.fancycamera.ImageAnalysisCallback;
3337
#onPoseListener: io.github.triniwiz.fancycamera.ImageAnalysisCallback;
3438
#permissionHandler = (args: AndroidActivityRequestPermissionsEventData) => {
39+
if (this.pause) {
40+
return;
41+
}
3542
this.#camera.onPermissionHandler(
3643
args.requestCode, args.permissions, args.grantResults
3744
)
@@ -86,6 +93,22 @@ export class MLKitView extends MLKitViewBase {
8693
return this.#hasCamera;
8794
}
8895

96+
[torchOnProperty.setNative](value: boolean) {
97+
if (this.#camera) {
98+
if (value) {
99+
this.#camera.setFlashMode(TORCH_MODE_ON());
100+
} else {
101+
this.#camera.setFlashMode(TORCH_MODE_OFF());
102+
}
103+
}
104+
}
105+
106+
[pauseProperty.setNative](value: boolean) {
107+
if (this.#camera) {
108+
this.#camera.setPause(value);
109+
}
110+
}
111+
89112
[detectionTypeProperty.setNative](value) {
90113
let type = DetectorType_None();
91114
switch (value) {

packages/mlkit-core/index.ios.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Utils } from "@nativescript/core";
2-
import { BarcodeFormats, barcodeFormatsProperty, CameraPosition, cameraPositionProperty, DetectionType, detectionTypeProperty, faceDetectionMinFaceSizeProperty, faceDetectionPerformanceModeProperty, faceDetectionTrackingEnabledProperty, imageLabelerConfidenceThresholdProperty, MLKitViewBase, objectDetectionClassifyProperty, objectDetectionMultipleProperty } from "./common";
2+
import { BarcodeFormats, barcodeFormatsProperty, CameraPosition, cameraPositionProperty, DetectionType, detectionTypeProperty, faceDetectionMinFaceSizeProperty, faceDetectionPerformanceModeProperty, faceDetectionTrackingEnabledProperty, imageLabelerConfidenceThresholdProperty, MLKitViewBase, objectDetectionClassifyProperty, objectDetectionMultipleProperty, pauseProperty, torchOnProperty } from "./common";
33
import '@nativescript/core';
44
import lazy from "@nativescript/core/utils/lazy";
55
import { DetectionEvent } from ".";
@@ -101,6 +101,15 @@ export class MLKitView extends MLKitViewBase {
101101
}
102102

103103

104+
[torchOnProperty.setNative](value: boolean) {
105+
this.#mlkitHelper.torch = value;
106+
}
107+
108+
[pauseProperty.setNative](value: boolean) {
109+
this.#mlkitHelper.pause = value;
110+
}
111+
112+
104113
[detectionTypeProperty.setNative](value) {
105114
let type = 8 /* None */
106115
switch (value) {
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
allprojects {
2+
repositories {
3+
maven { url "https://repo1.maven.org/maven2/" }
4+
maven { url "https://s01.oss.sonatype.org/content/repositories/releases/" }
5+
}
6+
}
7+
18
dependencies {
2-
implementation 'io.github.triniwiz:fancycamera:3.0.0-alpha23'
9+
implementation 'io.github.triniwiz:fancycamera:3.0.0-alpha.28'
10+
}
11+
12+
13+
android {
14+
packagingOptions {
15+
exclude 'META-INF/DEPENDENCIES'
16+
exclude 'META-INF/LICENSE'
17+
exclude 'META-INF/LICENSE.txt'
18+
exclude 'META-INF/license.txt'
19+
exclude 'META-INF/NOTICE'
20+
exclude 'META-INF/NOTICE.txt'
21+
exclude 'META-INF/notice.txt'
22+
exclude 'META-INF/ASL2.0'
23+
exclude("META-INF/*.kotlin_module")
24+
}
325
}

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

+35-2
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,33 @@ public class TNSMLKitHelper: NSObject, AVCaptureVideoDataOutputSampleBufferDeleg
249249
let encoder = JSONEncoder()
250250
var detectorType = TNSMLKitDetectionType.All
251251

252+
public var flashMode: Bool {
253+
didSet {
254+
if(flashMode){
255+
self.videoInput?.device.torchMode = .auto
256+
}else {
257+
self.videoInput?.device.torchMode = .on
258+
}
259+
}
260+
}
261+
262+
263+
public var pause: Bool {
264+
didSet {
265+
sessionQueue.async {
266+
if(self.isSessionSetup){
267+
if(self.pause && self.session.isRunning){
268+
self.session.stopRunning()
269+
}
270+
271+
if(!self.pause && !self.session.isRunning){
272+
self.session.startRunning()
273+
}
274+
}
275+
}
276+
}
277+
}
278+
252279

253280
#if canImport(MLKitBarcodeScanning)
254281
var barcodeScanner: BarcodeScanner?
@@ -322,7 +349,7 @@ public class TNSMLKitHelper: NSObject, AVCaptureVideoDataOutputSampleBufferDeleg
322349
setCamera()
323350
}
324351
private func setCamera(){
325-
sessionQueue.async {
352+
sessionQueue.async { [self] in
326353
if(self.isSessionSetup){
327354
let wasRunning = self.session.isRunning
328355

@@ -341,6 +368,12 @@ public class TNSMLKitHelper: NSObject, AVCaptureVideoDataOutputSampleBufferDeleg
341368
return
342369
}
343370

371+
if(self.flashMode){
372+
videoInput?.device.torchMode = .auto
373+
}else {
374+
videoInput?.device.torchMode = .on
375+
}
376+
344377

345378
if(wasRunning){
346379
self.session.stopRunning()
@@ -356,7 +389,7 @@ public class TNSMLKitHelper: NSObject, AVCaptureVideoDataOutputSampleBufferDeleg
356389
if(self.session.canAddInput(videoInput!)){
357390
self.session.addInput(videoInput!)
358391
}
359-
if(wasRunning){
392+
if(wasRunning && !self.pause){
360393
self.session.startRunning()
361394
}
362395
}

packages/mlkit-core/typings/android.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ declare module io {
2222
public static class: java.lang.Class<io.github.triniwiz.fancycamera.Camera>;
2323
public getDisplayRatio(): string;
2424
public cameraRecording(): boolean;
25+
public setPause(param0: boolean): void;
2526
public getAllowExifRotation(): boolean;
2627
public setMaxVideoFrameRate(param0: number): void;
2728
public getDisableHEVC(): boolean;
@@ -82,6 +83,7 @@ declare module io {
8283
public toggleCamera(): void;
8384
public stopRecording(): void;
8485
public constructor(param0: globalAndroid.content.Context, param1: globalAndroid.util.AttributeSet, param2: number);
86+
public getPause(): boolean;
8587
public hasFlash(): boolean;
8688
public constructor(param0: globalAndroid.content.Context);
8789
public setDetectorType(param0: io.github.triniwiz.fancycamera.DetectorType): void;
@@ -106,6 +108,7 @@ declare module io {
106108
public static class: java.lang.Class<io.github.triniwiz.fancycamera.Camera2>;
107109
public getDisplayRatio(): string;
108110
public cameraRecording(): boolean;
111+
public setPause(param0: boolean): void;
109112
public getAllowExifRotation(): boolean;
110113
public setMaxVideoFrameRate(param0: number): void;
111114
public getDisableHEVC(): boolean;
@@ -159,6 +162,7 @@ declare module io {
159162
public toggleCamera(): void;
160163
public stopRecording(): void;
161164
public constructor(param0: globalAndroid.content.Context, param1: globalAndroid.util.AttributeSet, param2: number);
165+
public getPause(): boolean;
162166
public hasFlash(): boolean;
163167
public constructor(param0: globalAndroid.content.Context);
164168
public setDetectorType(param0: io.github.triniwiz.fancycamera.DetectorType): void;
@@ -184,6 +188,7 @@ declare module io {
184188
public static Companion: io.github.triniwiz.fancycamera.CameraBase.Companion;
185189
public setFaceDetectionOptions(param0: any): void;
186190
public setMTimerTask$fancycamera_release(param0: java.util.TimerTask): void;
191+
public setPause(param0: boolean): void;
187192
public getAllowExifRotation(): boolean;
188193
public setOnBarcodeScanningListener$fancycamera_release(param0: io.github.triniwiz.fancycamera.ImageAnalysisCallback): void;
189194
public setCurrentOrientation(param0: number): void;
@@ -323,6 +328,7 @@ declare module io {
323328
public requestStoragePermission(): void;
324329
public toggleCamera(): void;
325330
public constructor(param0: globalAndroid.content.Context, param1: globalAndroid.util.AttributeSet, param2: number);
331+
public getPause(): boolean;
326332
public setOnPoseDetectedListener(param0: io.github.triniwiz.fancycamera.ImageAnalysisCallback): void;
327333
public setMDuration$fancycamera_release(param0: number): void;
328334
public setOnImageLabelingListener$fancycamera_release(param0: io.github.triniwiz.fancycamera.ImageAnalysisCallback): void;
@@ -569,6 +575,7 @@ declare module io {
569575
export class FancyCamera {
570576
public static class: java.lang.Class<io.github.triniwiz.fancycamera.FancyCamera>;
571577
public static Companion: io.github.triniwiz.fancycamera.FancyCamera.Companion;
578+
public setPause(param0: boolean): void;
572579
public getAllowExifRotation(): boolean;
573580
public getCameraOrientation(): io.github.triniwiz.fancycamera.CameraOrientation;
574581
public setMaxVideoFrameRate(param0: number): void;
@@ -635,6 +642,7 @@ declare module io {
635642
public setRatio(param0: string): void;
636643
public toggleCamera(): void;
637644
public stopRecording(): void;
645+
public getPause(): boolean;
638646
public setCameraOrientation(param0: io.github.triniwiz.fancycamera.CameraOrientation): void;
639647
public setOnPoseDetectedListener(param0: io.github.triniwiz.fancycamera.ImageAnalysisCallback): void;
640648
public constructor(param0: globalAndroid.content.Context);

0 commit comments

Comments
 (0)