question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Camera in Browser Fails With setOptions failed Error

See original GitHub issue

In an Ionic 4 + Capacitor project the camera is working okay on a mobile device but gives the following error when running on a desktop browser: “Unable to take photo! DOMException: setOptions failed”.

https://www.screencast.com/t/UqhdZcAzJ0

To reproduce:

  1. Create an Ionic 4 blank sample project.
  2. Add capacitor.
  3. Add ionicpwaelements.js to index.html head tag. <script src="https://unpkg.com/@ionic/pwa-elements@0.0.14/dist/ionicpwaelements.js"></script>
  4. Add camera example code from docs: https://capacitor.ionicframework.com/docs/apis/camera

src/app/home/home.page.ts

import { Component} from '@angular/core';
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';
import { Plugins, CameraResultType, CameraSource } from '@capacitor/core';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {
    image: SafeResourceUrl;

    constructor(private sanitizer: DomSanitizer) {
    }

    async takePicture() {
        const { Camera } = Plugins;

        const image = await Camera.getPhoto({
            quality: 90,
            allowEditing: true,
            resultType: CameraResultType.Base64,
            source: CameraSource.Camera
        });

        // Example of using the Base64 return type. It's recommended to use CameraResultType.Uri
        // instead for performance reasons when showing large, or a large amount of images.
        this.image = this.sanitizer.bypassSecurityTrustResourceUrl(image && (image.base64Data));
    }
}

src/app/home/home.page.html

<ion-header>
  <ion-toolbar>
    <ion-buttons slot="start">
      <ion-menu-button></ion-menu-button>
    </ion-buttons>
    <ion-title>
      Home
    </ion-title>
  </ion-toolbar>
</ion-header>

<ion-content padding>
  The world is your oyster.
  <p>If you get lost, the
    <a target="_blank" href="https://ionicframework.com/docs">docs</a> will be your guide.</p>

  <img [src]="image" />
  <ion-button (click)="takePicture()" ion-button color="primary">Take Picture</ion-button>

</ion-content>
$ ionic info
√ Gathering environment info - done!

Ionic:

   ionic (Ionic CLI)          : 4.0.6 (C:\Users\matth\AppData\Roaming\npm\node_modules\ionic)
   Ionic Framework            : @ionic/angular 4.0.0-beta.2
   @angular-devkit/core       : 0.7.3
   @angular-devkit/schematics : 0.7.3
   @angular/cli               : 6.1.3
   @ionic/ng-toolkit          : 1.0.6
   @ionic/schematics-angular  : 1.0.4

System:

   NodeJS : v8.9.4 (C:\Program Files\nodejs\node.exe)
   npm    : 5.6.0
   OS     : Windows 10

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
skyhip0811commented, Apr 3, 2020

I am still facing it with Chrome version 80.0.3987.149 with ionic/pwa-elements version 1.5.1 capacito/core version 1.5.2

0reactions
ionitron-bot[bot]commented, Nov 12, 2022

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.

Read more comments on GitHub >

github_iconTop Results From Across the Web

DOMException: setOptions failed on takePhoto() : r/ionic - Reddit
The problem is that the camera modal opens and shows the camera stream, but when you click the capture button, the camera windows...
Read more >
ImageCapture -> takePhoto() method fails in windows 10
I am trying to capture an image via the camera using "video" tag. Found an example at https:// ...
Read more >
Had the same DOMException: setOptions failed on takePhoto ...
With work-around found from above discussion, using grabFrame() instead and converted bitmap to dataurl and then converted to blob again to ...
Read more >
FIX Hardware Access Error Chrome Camera or Microphone ...
If you are trying to use a camera or microphone on your chrome browser and you get hardware access error, then this means...
Read more >
ImageCapture.takePhoto() - Web APIs | MDN
Check the Browser compatibility table carefully before using this in production. The takePhoto() method of the ImageCapture interface takes ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found