@@ -24,7 +24,7 @@ class Camera {
2424  } 
2525} 
2626
27- const  narrowDownFacingMode  =  async  facingMode  =>  { 
27+ const  narrowDownFacingMode  =  async  camera  =>  { 
2828  // Modern phones often have multipe front/rear cameras. 
2929  // Sometimes special purpose cameras like the wide-angle camera are picked 
3030  // by default. Those are not optimal for scanning QR codes but standard 
@@ -36,17 +36,32 @@ const narrowDownFacingMode = async facingMode => {
3636    ( {  kind } )  =>  kind  ===  "videoinput" 
3737  ) ; 
3838
39-   if  ( devices . length  >  2 )  { 
39+   // if (devices.length > 2) { 
40+   if  ( false )  { 
4041    const  frontCamera  =  devices [ 0 ] ; 
4142    const  rearCamera  =  devices [ devices . length  -  1 ] ; 
4243
43-     if  ( facingMode  ===  "front" )  { 
44-       return  {  deviceId : {  exact : frontCamera  }  } ; 
45-     }  else  { 
46-       return  {  deviceId : {  exact : rearCamera  }  } ; 
44+     switch  ( camera )  { 
45+       case  "auto" :
46+         return  {  deviceId : {  exact : rearCamera . deviceId  }  } ; 
47+       case  "rear" :
48+         return  {  deviceId : {  exact : rearCamera . deviceId  }  } ; 
49+       case  "front" :
50+         return  {  deviceId : {  exact : frontCamera . deviceId  }  } ; 
51+       default :
52+         return  undefined ; 
4753    } 
4854  }  else  { 
49-     return  {  facingMode } ; 
55+     switch  ( camera )  { 
56+       case  "auto" :
57+         return  {  facingMode : {  ideal : "environment"  }  } ; 
58+       case  "rear" :
59+         return  {  facingMode : {  exact : "environment"  }  } ; 
60+       case  "front" :
61+         return  {  facingMode : {  exact : "user"  }  } ; 
62+       default :
63+         return  undefined ; 
64+     } 
5065  } 
5166} ; 
5267
@@ -60,7 +75,7 @@ const STREAM_API_NOT_SUPPORTED = !(
6075
6176let  streamApiShimApplied  =  false ; 
6277
63- export  default  async  function ( videoEl ,  {  facingMode ,  torch } )  { 
78+ export  default  async  function ( videoEl ,  {  camera ,  torch } )  { 
6479  // At least in Chrome `navigator.mediaDevices` is undefined when the page is 
6580  // loaded using HTTP rather than HTTPS. Thus `STREAM_API_NOT_SUPPORTED` is 
6681  // initialized with `false` although the API might actually be supported. 
@@ -87,7 +102,7 @@ export default async function(videoEl, { facingMode, torch }) {
87102    video : { 
88103      width : {  min : 360 ,  ideal : 640 ,  max : 1920  } , 
89104      height : {  min : 240 ,  ideal : 480 ,  max : 1080  } , 
90-       ...( await  narrowDownFacingMode ( facingMode ) ) 
105+       ...( await  narrowDownFacingMode ( camera ) ) 
91106    } 
92107  } ; 
93108
0 commit comments