createCamera method Null safety
- CameraDescription cameraDescription,
- ResolutionPreset? resolutionPreset,
- {bool enableAudio = false}
override
Creates an uninitialized camera instance and returns the cameraId.
Implementation
@override
Future<int> createCamera(
CameraDescription cameraDescription,
ResolutionPreset? resolutionPreset, {
bool enableAudio = false,
}) async {
try {
if (!camerasMetadata.containsKey(cameraDescription)) {
throw PlatformException(
code: CameraErrorCode.missingMetadata.toString(),
message:
'Missing camera metadata. Make sure to call `availableCameras` before creating a camera.',
);
}
final int textureId = _textureCounter++;
final CameraMetadata cameraMetadata = camerasMetadata[cameraDescription]!;
final CameraType? cameraType = cameraMetadata.facingMode != null
? _cameraService.mapFacingModeToCameraType(cameraMetadata.facingMode!)
: null;
// Use the highest resolution possible
// if the resolution preset is not specified.
final Size videoSize = _cameraService
.mapResolutionPresetToSize(resolutionPreset ?? ResolutionPreset.max);
// Create a camera with the given audio and video constraints.
// Sensor orientation is currently not supported.
final Camera camera = Camera(
textureId: textureId,
cameraService: _cameraService,
options: CameraOptions(
audio: AudioConstraints(enabled: enableAudio),
video: VideoConstraints(
facingMode:
cameraType != null ? FacingModeConstraint(cameraType) : null,
width: VideoSizeConstraint(
ideal: videoSize.width.toInt(),
),
height: VideoSizeConstraint(
ideal: videoSize.height.toInt(),
),
deviceId: cameraMetadata.deviceId,
),
),
);
cameras[textureId] = camera;
return textureId;
} on PlatformException catch (e) {
throw CameraException(e.code, e.message);
}
}