@@ -118,14 +118,49 @@ class _WebQrViewState extends State<WebQrView> {
118
118
}
119
119
120
120
try {
121
- var constraints = UserMediaOptions (
122
- video: VideoOptions (
123
- facingMode: (facing == CameraFacing .front ? 'user' : 'environment' ),
124
- ));
125
- // dart style, not working properly:
126
- // var stream =
127
- // await html.window.navigator.mediaDevices.getUserMedia(constraints);
128
- // straight JS:
121
+ final UserMediaOptions constraints;
122
+ switch (facing) {
123
+ case CameraFacing .front:
124
+ constraints = UserMediaOptions (
125
+ video: VideoOptions (facingMode: 'user' ),
126
+ );
127
+ break ;
128
+ case CameraFacing .unknown:
129
+ // fall through
130
+ case CameraFacing .back:
131
+ final List <web.MediaDeviceInfo > devices =
132
+ (await enumerateDevices ().toDart).toDart;
133
+
134
+ final List <web.MediaDeviceInfo > backCameras = devices
135
+ .where (
136
+ (device) =>
137
+ device.kind == 'videoinput' &&
138
+ device.label.toLowerCase ().contains ('back' ),
139
+ )
140
+ .toList ();
141
+
142
+ // attempt to find a main/primary camera. If none, use last back camera entry if it exists
143
+ final web.MediaDeviceInfo ? idealCameraMediaInfo = backCameras
144
+ .where (
145
+ (camera) =>
146
+ camera.label.toLowerCase ().contains ('main' ) ||
147
+ camera.label.toLowerCase ().contains ('primary' ),
148
+ )
149
+ .lastOrNull ??
150
+ backCameras.lastOrNull;
151
+
152
+ constraints = UserMediaOptions (
153
+ video: idealCameraMediaInfo != null
154
+ ? VideoOptions (
155
+ deviceId:
156
+ DeviceIdOptions (exact: idealCameraMediaInfo.deviceId),
157
+ )
158
+ : VideoOptions (
159
+ facingMode: "environment" ,
160
+ ),
161
+ );
162
+ }
163
+
129
164
if (_controller == null ) {
130
165
_controller = QRViewControllerWeb (this );
131
166
widget.onPlatformViewCreated (_controller! );
0 commit comments