Camera in Browser Fails With setOptions failed Error
See original GitHub issueIn 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:
- Create an Ionic 4 blank sample project.
- Add capacitor.
- Add ionicpwaelements.js to index.html head tag. <script src="https://unpkg.com/@ionic/pwa-elements@0.0.14/dist/ionicpwaelements.js"></script>
- 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:
- Created 5 years ago
- Comments:7 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

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
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.