Skip to content

Not auto-requesting camera permissions #47

Open
@boutier

Description

@boutier

I had a few difficulties to make this plugin work (with angular). The last one was that I didn't think about explicitly asking camera permission to the user. I guess this should (at least) be mentioned in the README page.

    "@angular/…": "~15.2.0",
    "@nativescript/angular": "^15.0.0",
    "@nativescript/camera": "~5.0.15",  # I used this to require camera permissions
    "@nativescript/core": "~8.5.0",
    "@nativescript/mlkit-barcode-scanning": "~2.0.0",
    "@nativescript/mlkit-core": "~2.0.0",

I tried to create a fresh NativeScript app, with ns create test --ng and the mlkit packages above.

And then I just had a black screen. But it worked from ns-preview. So I realized that ns-preview actually ask for camera permission, and so it worked after adding @nativescript/camera to my package.json and making something like:

import * as Camera from '@nativescript/camera';

export class BarcodeComponent {
  havePermission = false;

  constructor() {
    Camera.requestPermissions().then((v) => (this.havePermission = true));
  }
}
  <MLKitView *ngIf="havePermission" ...

Notes:

  • I was not able to add (onLoaded)="$event.object.requestCameraPermission() to MLKitView.
  • I didn't try to use a @ViewChild

Clearly, it would be great to add some documentation here https://github.com/NativeScript/mlkit/blob/main/packages/mlkit-core/README.md

I didn't propose a patch request because it may not be related to Angular only… but the angular section could be:

#### Angular

```ts
import { MLKitView } from "@nativescript/mlkit-core";
import { MLKitModule } from '@nativescript/mlkit-core/angular';

registerElement("MLKitView", () => MLKitView);

@NgModule({
    imports: [
    MLKitModule
    ],
    declarations: [
        AppComponent
    ],
    bootstrap: [AppComponent]
})
```

```html
<MLKitView
*ngIf="haveCameraPermission"
cameraPosition="back"
detectionType="all" 
(detection)="onDetection($event)"
></MLKitView>
```

You should also request for camera permission:
```ts
import * as Camera from '@nativescript/camera';

[…] SomeComponent() {
  haveCameraPermission = false;
  constructor() {
    Camera.requestPermissions().then((v) => (this.havePermission = true));
```

A last thing: thanks a lot for this plugin!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions