Search code examples
reactjsangularjswebcam

ngx-webcam open front camera by default in mobile


<webcam (imageCapture)="imageCaptured($event)" [trigger]="capture$" (initError)="handleInitError($event)">

On mobile real camera open by default. I want to set front camera by default.


Solution

  • https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackConstraints/facingMode

    camera.component.ts

    //facingMode: string = 'environment'; Set rear camera 
    facingMode: string = 'user';  //Set front camera
    allowCameraSwitch = false;
    
    
    public get videoOptions(): MediaTrackConstraints {
        const result: MediaTrackConstraints = {};
        if (this.facingMode && this.facingMode !== '') {
            result.facingMode = { ideal: this.facingMode };
        }
        return result;
    }
    

    camera.component.html

    <webcam (imageCapture)="imageCaptured($event)" [trigger]="capture$" (initError)="handleInitError($event)" [videoOptions]="videoOptions" [allowCameraSwitch]="allowCameraSwitch">