Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not auto-requesting camera permissions #47

Open
boutier opened this issue Apr 7, 2023 · 2 comments
Open

Not auto-requesting camera permissions #47

boutier opened this issue Apr 7, 2023 · 2 comments

Comments

@boutier
Copy link

boutier commented Apr 7, 2023

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!

@triniwiz
Copy link
Member

triniwiz commented Apr 9, 2023

Hey @boutier yes I should add that to the readme, this is how I did it in the demo. Another thing I can do is add a static method to request permission

@Kaspis77
Copy link

Hi, thx @boutier for the code, it helps me a lot... it works till android 12. But 13 is not using WRITE_EXTERNAL_STORAGE permission anymore, so that permission is never set to true... and so scanner is never visible. Any help or suggestions on that? Pretty please :) I feel like I am the only one using this plugin. Thank You!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants