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

Commit

Permalink
Adding a FRONT or REAR property when using Ml Kit, in order to select…
Browse files Browse the repository at this point in the history
… the camera-facing direction. #955
  • Loading branch information
EddyVerbruggen committed Oct 12, 2018
1 parent 9418ecc commit 40bf1c0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
processEveryNthFrame="30"
enableFaceTracking="true"
minimumFaceSize="0.2"
preferFrontCamera="true"
modeType="accurate"
[torchOn]="torchOn"
(scanResult)="onFaceDetectionResult($event)">
</MLKitFaceDetection>

Expand Down
10 changes: 8 additions & 2 deletions docs/ML_KIT.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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)">
</MLKitTextRecognition>
Expand All @@ -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" />
</Page>
Expand Down Expand Up @@ -201,6 +204,7 @@ registerElement("MLKitFaceDetection", () => require("nativescript-plugin-firebas
detectionMode="accurate"
enableFaceTracking="true"
minimumFaceSize="0.2"
preferFrontCamera="true"
[torchOn]="torchOn"
(scanResult)="onFaceDetectionResult($event)">
</MLKitFaceDetection>
Expand Down Expand Up @@ -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)">
</MLKitBarcodeScanner>
Expand Down Expand Up @@ -293,6 +298,7 @@ registerElement("MLKitImageLabeling", () => require("nativescript-plugin-firebas
width="260"
height="380"
confidenceThreshold="0.6"
preferFrontCamera="false"
[torchOn]="torchOn"
(scanResult)="onImageLabelingResult($event)">
</MLKitImageLabeling>
Expand Down
11 changes: 4 additions & 7 deletions src/mlkit/mlkit-cameraview.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 2 additions & 6 deletions src/mlkit/mlkit-cameraview.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<any>[AVCaptureDeviceTypeBuiltInWideAngleCamera],
AVMediaTypeVideo,
AVCaptureDevicePosition.Back
this.preferFrontCamera ? AVCaptureDevicePosition.Front : AVCaptureDevicePosition.Back
).devices.firstObject;

if (this.torchOn) {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 40bf1c0

Please sign in to comment.