Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 40bf1c0

Browse files
Adding a FRONT or REAR property when using Ml Kit, in order to select the camera-facing direction. #955
1 parent 9418ecc commit 40bf1c0

File tree

4 files changed

+15
-16
lines changed

4 files changed

+15
-16
lines changed

demo-ng/app/tabs/mlkit/facedetection/facedetection.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
processEveryNthFrame="30"
1111
enableFaceTracking="true"
1212
minimumFaceSize="0.2"
13+
preferFrontCamera="true"
1314
modeType="accurate"
14-
[torchOn]="torchOn"
1515
(scanResult)="onFaceDetectionResult($event)">
1616
</MLKitFaceDetection>
1717

docs/ML_KIT.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,9 @@ The exact details of using the live camera view depend on whether or not you're
118118
You can use any view-related property you like as we're extending `ContentView`.
119119
So things like `class`, `row`, `width`, `horizontalAlignment`, `style` are all valid properties.
120120
121-
Plugin-specific are the optional properties `processEveryNthFrame` and `torchOn`, and optional event `scanResult`.
122-
You can `processEveryNthFrame` set to a lower value than the default (5) to put less strain on the device.
121+
Plugin-specific are the optional properties `processEveryNthFrame`, `preferFrontCamera` (default `false`), and `torchOn`, and the optional `scanResult` event.
122+
123+
You can set `processEveryNthFrame` set to a lower value than the default (5) to put less strain on the device.
123124
Especially 'Face detection' seems a bit more CPU intensive, but for 'Text recognition' the default is fine.
124125
125126
> Look at [the demo app](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/tree/master/demo-ng) to see how to wire up that `onTextRecognitionResult` function, and how to wire `torchOn` to a `Switch`.
@@ -140,6 +141,7 @@ Now you're able to use the registered element in the view:
140141
width="260"
141142
height="380"
142143
processEveryNthFrame="10"
144+
preferFrontCamera="false"
143145
[torchOn]="torchOn"
144146
(scanResult)="onTextRecognitionResult($event)">
145147
</MLKitTextRecognition>
@@ -158,6 +160,7 @@ Declare a namespace at the top of the embedding page, and use it anywhere on the
158160
width="260"
159161
height="380"
160162
processEveryNthFrame="3"
163+
preferFrontCamera="false"
161164
scanResult="onTextRecognitionResult" />
162165
163166
</Page>
@@ -201,6 +204,7 @@ registerElement("MLKitFaceDetection", () => require("nativescript-plugin-firebas
201204
detectionMode="accurate"
202205
enableFaceTracking="true"
203206
minimumFaceSize="0.2"
207+
preferFrontCamera="true"
204208
[torchOn]="torchOn"
205209
(scanResult)="onFaceDetectionResult($event)">
206210
</MLKitFaceDetection>
@@ -238,6 +242,7 @@ registerElement("MLKitBarcodeScanner", () => require("nativescript-plugin-fireba
238242
width="260"
239243
height="380"
240244
formats="QR_CODE, EAN_8, EAN_13"
245+
preferFrontCamera="false"
241246
[torchOn]="torchOn"
242247
(scanResult)="onBarcodeScanningResult($event)">
243248
</MLKitBarcodeScanner>
@@ -293,6 +298,7 @@ registerElement("MLKitImageLabeling", () => require("nativescript-plugin-firebas
293298
width="260"
294299
height="380"
295300
confidenceThreshold="0.6"
301+
preferFrontCamera="false"
296302
[torchOn]="torchOn"
297303
(scanResult)="onImageLabelingResult($event)">
298304
</MLKitImageLabeling>

src/mlkit/mlkit-cameraview.android.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,8 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
102102
}
103103

104104
private initView(nativeView) {
105-
// if (this.preferFrontCamera) {
106-
// this._reader.switchDeviceInput();
107-
// }
108-
109105
this.surfaceView = new android.view.SurfaceView(utils.ad.getApplicationContext());
110106
nativeView.addView(this.surfaceView);
111-
112107
this.runCamera();
113108
}
114109

@@ -119,7 +114,7 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
119114
return;
120115
}
121116
const surfaceHolder = this.surfaceView.getHolder();
122-
const cameraFacingRequested = android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK;
117+
const cameraFacingRequested = this.preferFrontCamera ? android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT : android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK;
123118
const cameraInfo = new android.hardware.Camera.CameraInfo();
124119

125120
let requestedCameraId = android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK; // use this as the default
@@ -160,7 +155,9 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
160155
}
161156

162157
if (this.torchOn) {
163-
parameters.setFlashMode(android.hardware.Camera.Parameters.FLASH_MODE_TORCH);
158+
if (parameters.getSupportedFlashModes() && parameters.getSupportedFlashModes().contains(android.hardware.Camera.Parameters.FLASH_MODE_TORCH)) {
159+
parameters.setFlashMode(android.hardware.Camera.Parameters.FLASH_MODE_TORCH);
160+
}
164161
}
165162

166163
this.camera.setParameters(parameters);

src/mlkit/mlkit-cameraview.ios.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,11 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
4242
}
4343

4444
private initView() {
45-
// if (this.preferFrontCamera) {
46-
// this._reader.switchDeviceInput();
47-
// }
48-
4945
// find a suitable device
5046
this.captureDevice = AVCaptureDeviceDiscoverySession.discoverySessionWithDeviceTypesMediaTypePosition(
5147
<any>[AVCaptureDeviceTypeBuiltInWideAngleCamera],
5248
AVMediaTypeVideo,
53-
AVCaptureDevicePosition.Back
49+
this.preferFrontCamera ? AVCaptureDevicePosition.Front : AVCaptureDevicePosition.Back
5450
).devices.firstObject;
5551

5652
if (this.torchOn) {
@@ -142,7 +138,7 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
142138

143139
protected updateTorch(): void {
144140
const device = this.captureDevice;
145-
if (device && device.lockForConfiguration()) {
141+
if (device && device.hasTorch && device.lockForConfiguration()) {
146142
if (this.torchOn) {
147143
device.torchMode = AVCaptureTorchMode.On;
148144
device.flashMode = AVCaptureFlashMode.On;

0 commit comments

Comments
 (0)