@@ -206,117 +206,118 @@ public async Task RetrieveCameraDevice(bool force = false)
206
206
207
207
// _texture.ClearCanvas(Element.BackgroundColor.ToAndroid()); // HANG after select valid camera...
208
208
Element ? . RaiseMediaCaptureFailed ( $ "No { Element . CameraOptions } camera found") ;
209
+ return ;
209
210
}
210
- else
211
+
212
+ try
211
213
{
212
- try
213
- {
214
- var characteristics = Manager . GetCameraCharacteristics ( cameraId ) ;
215
- var map = ( StreamConfigurationMap ) ( characteristics ? . Get ( CameraCharacteristics . ScalerStreamConfigurationMap ) ?? throw new NullReferenceException ( ) ) ;
214
+ var characteristics = Manager . GetCameraCharacteristics ( cameraId ) ;
215
+ var map = ( StreamConfigurationMap ) ( characteristics ? . Get ( CameraCharacteristics . ScalerStreamConfigurationMap ) ?? throw new NullReferenceException ( ) ) ;
216
216
217
- flashSupported = characteristics . Get ( CameraCharacteristics . FlashInfoAvailable ) == Java . Lang . Boolean . True ;
218
- stabilizationSupported = false ;
219
- var stabilizationModes = characteristics . Get ( CameraCharacteristics . ControlAvailableVideoStabilizationModes ) ;
217
+ flashSupported = characteristics . Get ( CameraCharacteristics . FlashInfoAvailable ) == Java . Lang . Boolean . True ;
218
+ stabilizationSupported = false ;
219
+ var stabilizationModes = characteristics . Get ( CameraCharacteristics . ControlAvailableVideoStabilizationModes ) ;
220
220
221
- if ( stabilizationModes is IEnumerable < int > modes )
221
+ if ( stabilizationModes is IEnumerable < int > modes )
222
+ {
223
+ foreach ( var mode in modes )
222
224
{
223
- foreach ( var mode in modes )
224
- {
225
- if ( mode == ( int ) ControlVideoStabilizationMode . On )
226
- stabilizationSupported = true ;
227
- }
225
+ if ( mode == ( int ) ControlVideoStabilizationMode . On )
226
+ stabilizationSupported = true ;
228
227
}
228
+ }
229
229
230
- if ( Element != null )
231
- Element . MaxZoom = maxDigitalZoom = ( float ) ( characteristics . Get ( CameraCharacteristics . ScalerAvailableMaxDigitalZoom ) ?? throw new NullReferenceException ( ) ) ;
232
-
233
- activeRect = ( Rect ) ( characteristics . Get ( CameraCharacteristics . SensorInfoActiveArraySize ) ?? throw new NullReferenceException ( ) ) ;
234
- sensorOrientation = ( int ) ( characteristics . Get ( CameraCharacteristics . SensorOrientation ) ?? throw new NullReferenceException ( ) ) ;
235
-
236
- var displaySize = new APoint ( ) ;
237
- Activity . WindowManager ? . DefaultDisplay ? . GetSize ( displaySize ) ;
230
+ if ( Element != null )
231
+ Element . MaxZoom = maxDigitalZoom = ( float ) ( characteristics . Get ( CameraCharacteristics . ScalerAvailableMaxDigitalZoom ) ?? throw new NullReferenceException ( ) ) ;
238
232
239
- _ = texture ?? throw new NullReferenceException ( ) ;
240
- var rotatedViewWidth = texture . Width ;
241
- var rotatedViewHeight = texture . Height ;
242
- var maxPreviewWidth = displaySize . X ;
243
- var maxPreviewHeight = displaySize . Y ;
233
+ activeRect = ( Rect ) ( characteristics . Get ( CameraCharacteristics . SensorInfoActiveArraySize ) ?? throw new NullReferenceException ( ) ) ;
234
+ sensorOrientation = ( int ) ( characteristics . Get ( CameraCharacteristics . SensorOrientation ) ?? throw new NullReferenceException ( ) ) ;
244
235
245
- if ( sensorOrientation == 90 || sensorOrientation == 270 )
246
- {
247
- rotatedViewWidth = texture . Height ;
248
- rotatedViewHeight = texture . Width ;
249
- maxPreviewWidth = displaySize . Y ;
250
- maxPreviewHeight = displaySize . X ;
251
- }
236
+ var displaySize = new APoint ( ) ;
237
+ Activity . WindowManager ? . DefaultDisplay ? . GetSize ( displaySize ) ;
252
238
253
- if ( maxPreviewHeight > CameraFragment . maxPreviewHeight )
254
- {
255
- maxPreviewHeight = CameraFragment . maxPreviewHeight ;
256
- }
239
+ _ = texture ?? throw new NullReferenceException ( ) ;
240
+ var rotatedViewWidth = texture . Width ;
241
+ var rotatedViewHeight = texture . Height ;
242
+ var maxPreviewWidth = displaySize . X ;
243
+ var maxPreviewHeight = displaySize . Y ;
257
244
258
- if ( maxPreviewWidth > CameraFragment . maxPreviewWidth )
259
- {
260
- maxPreviewWidth = CameraFragment . maxPreviewWidth ;
261
- }
245
+ if ( sensorOrientation == 90 || sensorOrientation == 270 )
246
+ {
247
+ rotatedViewWidth = texture . Height ;
248
+ rotatedViewHeight = texture . Width ;
249
+ maxPreviewWidth = displaySize . Y ;
250
+ maxPreviewHeight = displaySize . X ;
251
+ }
262
252
263
- photoSize = GetMaxSize ( map . GetOutputSizes ( ( int ) ImageFormatType . Jpeg ) ) ;
264
- videoSize = GetMaxSize ( map . GetOutputSizes ( Class . FromType ( typeof ( MediaRecorder ) ) ) ) ;
265
- previewSize = ChooseOptimalSize (
266
- map . GetOutputSizes ( Class . FromType ( typeof ( SurfaceTexture ) ) ) ?? throw new NullReferenceException ( ) ,
267
- rotatedViewWidth ,
268
- rotatedViewHeight ,
269
- maxPreviewWidth ,
270
- maxPreviewHeight ,
271
- cameraTemplate == CameraTemplate . Record ? videoSize : photoSize ) ;
272
- cameraType = ( LensFacing ) ( int ) ( characteristics . Get ( CameraCharacteristics . LensFacing ) ?? throw new NullReferenceException ( ) ) ;
273
-
274
- if ( Resources . Configuration ? . Orientation == AOrientation . Landscape )
275
- texture . SetAspectRatio ( previewSize . Width , previewSize . Height ) ;
276
- else
277
- texture . SetAspectRatio ( previewSize . Height , previewSize . Width ) ;
253
+ if ( maxPreviewHeight > CameraFragment . maxPreviewHeight )
254
+ {
255
+ maxPreviewHeight = CameraFragment . maxPreviewHeight ;
256
+ }
278
257
279
- initTaskSource = new TaskCompletionSource < CameraDevice ? > ( ) ;
258
+ if ( maxPreviewWidth > CameraFragment . maxPreviewWidth )
259
+ {
260
+ maxPreviewWidth = CameraFragment . maxPreviewWidth ;
261
+ }
280
262
281
- Manager . OpenCamera (
282
- cameraId ,
283
- new CameraStateListener
263
+ var outputSizes = map . GetOutputSizes ( Class . FromType ( typeof ( SurfaceTexture ) ) ) ?? throw new NullReferenceException ( ) ;
264
+
265
+ photoSize = GetMaxSize ( map . GetOutputSizes ( ( int ) ImageFormatType . Jpeg ) ) ;
266
+ videoSize = GetMaxSize ( map . GetOutputSizes ( Class . FromType ( typeof ( MediaRecorder ) ) ) ) ;
267
+ previewSize = ChooseOptimalSize (
268
+ outputSizes ,
269
+ rotatedViewWidth ,
270
+ rotatedViewHeight ,
271
+ maxPreviewWidth ,
272
+ maxPreviewHeight ,
273
+ cameraTemplate == CameraTemplate . Record ? videoSize : photoSize ) ;
274
+ cameraType = ( LensFacing ) ( int ) ( characteristics . Get ( CameraCharacteristics . LensFacing ) ?? throw new NullReferenceException ( ) ) ;
275
+
276
+ if ( Resources . Configuration ? . Orientation == AOrientation . Landscape )
277
+ texture . SetAspectRatio ( previewSize . Width , previewSize . Height ) ;
278
+ else
279
+ texture . SetAspectRatio ( previewSize . Height , previewSize . Width ) ;
280
+
281
+ initTaskSource = new TaskCompletionSource < CameraDevice ? > ( ) ;
282
+
283
+ Manager . OpenCamera (
284
+ cameraId ,
285
+ new CameraStateListener
286
+ {
287
+ OnOpenedAction = device => initTaskSource ? . TrySetResult ( device ) ,
288
+ OnDisconnectedAction = device =>
284
289
{
285
- OnOpenedAction = device => initTaskSource ? . TrySetResult ( device ) ,
286
- OnDisconnectedAction = device =>
287
- {
288
- initTaskSource ? . TrySetResult ( null ) ;
289
- CloseDevice ( device ) ;
290
- } ,
291
- OnErrorAction = ( device , error ) =>
292
- {
293
- initTaskSource ? . TrySetResult ( device ) ;
294
- Element ? . RaiseMediaCaptureFailed ( $ "Camera device error: { error } ") ;
295
- CloseDevice ( device ) ;
296
- } ,
297
- OnClosedAction = device =>
298
- {
299
- initTaskSource ? . TrySetResult ( null ) ;
300
- CloseDevice ( device ) ;
301
- }
290
+ initTaskSource ? . TrySetResult ( null ) ;
291
+ CloseDevice ( device ) ;
302
292
} ,
303
- backgroundHandler ) ;
293
+ OnErrorAction = ( device , error ) =>
294
+ {
295
+ initTaskSource ? . TrySetResult ( device ) ;
296
+ Element ? . RaiseMediaCaptureFailed ( $ "Camera device error: { error } ") ;
297
+ CloseDevice ( device ) ;
298
+ } ,
299
+ OnClosedAction = device =>
300
+ {
301
+ initTaskSource ? . TrySetResult ( null ) ;
302
+ CloseDevice ( device ) ;
303
+ }
304
+ } ,
305
+ backgroundHandler ) ;
304
306
305
- captureSessionOpenCloseLock . Release ( ) ;
306
- device = await initTaskSource . Task ;
307
- initTaskSource = null ;
308
- if ( device != null )
309
- await PrepareSession ( ) ;
310
- }
311
- catch ( Java . Lang . Exception error )
312
- {
313
- LogError ( "Failed to open camera" , error ) ;
314
- Available = false ;
315
- }
316
- finally
317
- {
318
- IsBusy = false ;
319
- }
307
+ captureSessionOpenCloseLock . Release ( ) ;
308
+ device = await initTaskSource . Task ;
309
+ initTaskSource = null ;
310
+ if ( device != null )
311
+ await PrepareSession ( ) ;
312
+ }
313
+ catch ( Java . Lang . Exception error )
314
+ {
315
+ LogError ( "Failed to open camera" , error ) ;
316
+ Available = false ;
317
+ }
318
+ finally
319
+ {
320
+ IsBusy = false ;
320
321
}
321
322
}
322
323
@@ -602,7 +603,7 @@ async Task PrepareSession()
602
603
OnConfigureFailedAction = captureSession =>
603
604
{
604
605
tcs . SetResult ( null ) ;
605
- Element ? . RaiseMediaCaptureFailed ( "Failed to create captire sesstion " ) ;
606
+ Element ? . RaiseMediaCaptureFailed ( "Failed to create capture session " ) ;
606
607
} ,
607
608
OnConfiguredAction = captureSession => tcs . SetResult ( captureSession )
608
609
} ,
@@ -1058,7 +1059,17 @@ ASize ChooseOptimalSize(ASize[] choices, int width, int height, int maxWidth, in
1058
1059
}
1059
1060
1060
1061
LogError ( "Couldn't find any suitable preview size" ) ;
1061
- return choices [ 0 ] ;
1062
+
1063
+ var fallbackChoice = choices [ 0 ] ;
1064
+
1065
+ if ( fallbackChoice . Height > maxHeight || fallbackChoice . Width > maxWidth )
1066
+ {
1067
+ LogError ( "Fallback choice is too large, using max preview size" ) ;
1068
+
1069
+ fallbackChoice = new ASize ( maxWidth , maxHeight ) ;
1070
+ }
1071
+
1072
+ return fallbackChoice ;
1062
1073
}
1063
1074
}
1064
1075
}
0 commit comments