diff --git a/demo-ng/app/tabs/mlkit/facedetection/facedetection.component.html b/demo-ng/app/tabs/mlkit/facedetection/facedetection.component.html index d2237902..f83b30b5 100644 --- a/demo-ng/app/tabs/mlkit/facedetection/facedetection.component.html +++ b/demo-ng/app/tabs/mlkit/facedetection/facedetection.component.html @@ -10,8 +10,8 @@ processEveryNthFrame="30" enableFaceTracking="true" minimumFaceSize="0.2" + preferFrontCamera="true" modeType="accurate" - [torchOn]="torchOn" (scanResult)="onFaceDetectionResult($event)"> diff --git a/docs/ML_KIT.md b/docs/ML_KIT.md index a3e4a5c5..4d2d9a9f 100644 --- a/docs/ML_KIT.md +++ b/docs/ML_KIT.md @@ -118,8 +118,9 @@ The exact details of using the live camera view depend on whether or not you're You can use any view-related property you like as we're extending `ContentView`. So things like `class`, `row`, `width`, `horizontalAlignment`, `style` are all valid properties. -Plugin-specific are the optional properties `processEveryNthFrame` and `torchOn`, and optional event `scanResult`. -You can `processEveryNthFrame` set to a lower value than the default (5) to put less strain on the device. +Plugin-specific are the optional properties `processEveryNthFrame`, `preferFrontCamera` (default `false`), and `torchOn`, and the optional `scanResult` event. + +You can set `processEveryNthFrame` set to a lower value than the default (5) to put less strain on the device. Especially 'Face detection' seems a bit more CPU intensive, but for 'Text recognition' the default is fine. > 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: width="260" height="380" processEveryNthFrame="10" + preferFrontCamera="false" [torchOn]="torchOn" (scanResult)="onTextRecognitionResult($event)"> @@ -158,6 +160,7 @@ Declare a namespace at the top of the embedding page, and use it anywhere on the width="260" height="380" processEveryNthFrame="3" + preferFrontCamera="false" scanResult="onTextRecognitionResult" /> @@ -201,6 +204,7 @@ registerElement("MLKitFaceDetection", () => require("nativescript-plugin-firebas detectionMode="accurate" enableFaceTracking="true" minimumFaceSize="0.2" + preferFrontCamera="true" [torchOn]="torchOn" (scanResult)="onFaceDetectionResult($event)"> @@ -238,6 +242,7 @@ registerElement("MLKitBarcodeScanner", () => require("nativescript-plugin-fireba width="260" height="380" formats="QR_CODE, EAN_8, EAN_13" + preferFrontCamera="false" [torchOn]="torchOn" (scanResult)="onBarcodeScanningResult($event)"> @@ -293,6 +298,7 @@ registerElement("MLKitImageLabeling", () => require("nativescript-plugin-firebas width="260" height="380" confidenceThreshold="0.6" + preferFrontCamera="false" [torchOn]="torchOn" (scanResult)="onImageLabelingResult($event)"> diff --git a/src/mlkit/mlkit-cameraview.android.ts b/src/mlkit/mlkit-cameraview.android.ts index 754a7ea3..135304b7 100644 --- a/src/mlkit/mlkit-cameraview.android.ts +++ b/src/mlkit/mlkit-cameraview.android.ts @@ -102,13 +102,8 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase { } private initView(nativeView) { - // if (this.preferFrontCamera) { - // this._reader.switchDeviceInput(); - // } - this.surfaceView = new android.view.SurfaceView(utils.ad.getApplicationContext()); nativeView.addView(this.surfaceView); - this.runCamera(); } @@ -119,7 +114,7 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase { return; } const surfaceHolder = this.surfaceView.getHolder(); - const cameraFacingRequested = android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK; + const cameraFacingRequested = this.preferFrontCamera ? android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT : android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK; const cameraInfo = new android.hardware.Camera.CameraInfo(); let requestedCameraId = android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK; // use this as the default @@ -160,7 +155,9 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase { } if (this.torchOn) { - parameters.setFlashMode(android.hardware.Camera.Parameters.FLASH_MODE_TORCH); + if (parameters.getSupportedFlashModes() && parameters.getSupportedFlashModes().contains(android.hardware.Camera.Parameters.FLASH_MODE_TORCH)) { + parameters.setFlashMode(android.hardware.Camera.Parameters.FLASH_MODE_TORCH); + } } this.camera.setParameters(parameters); diff --git a/src/mlkit/mlkit-cameraview.ios.ts b/src/mlkit/mlkit-cameraview.ios.ts index 30bf0a1e..baa20be8 100644 --- a/src/mlkit/mlkit-cameraview.ios.ts +++ b/src/mlkit/mlkit-cameraview.ios.ts @@ -42,15 +42,11 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase { } private initView() { - // if (this.preferFrontCamera) { - // this._reader.switchDeviceInput(); - // } - // find a suitable device this.captureDevice = AVCaptureDeviceDiscoverySession.discoverySessionWithDeviceTypesMediaTypePosition( [AVCaptureDeviceTypeBuiltInWideAngleCamera], AVMediaTypeVideo, - AVCaptureDevicePosition.Back + this.preferFrontCamera ? AVCaptureDevicePosition.Front : AVCaptureDevicePosition.Back ).devices.firstObject; if (this.torchOn) { @@ -142,7 +138,7 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase { protected updateTorch(): void { const device = this.captureDevice; - if (device && device.lockForConfiguration()) { + if (device && device.hasTorch && device.lockForConfiguration()) { if (this.torchOn) { device.torchMode = AVCaptureTorchMode.On; device.flashMode = AVCaptureFlashMode.On;