resolve static method

CameraDeviceFormat? resolve(
  1. CameraDeviceInfo device,
  2. List<CameraConstraint> constraints
)

Returns the best-matching format for device given constraints, or null if the device has no formats. With an empty constraint list the highest-resolution format is returned (a sensible default).

Implementation

static CameraDeviceFormat? resolve(
  CameraDeviceInfo device,
  List<CameraConstraint> constraints,
) {
  final formats = device.formats;
  if (formats.isEmpty) return null;

  final effective = constraints.isEmpty ? const [ResolutionConstraint(TargetResolution.max())] : constraints;

  final stats = FormatStats.from(formats);
  final n = effective.length;

  CameraDeviceFormat? best;
  double bestScore = double.infinity;
  for (final format in formats) {
    double score = 0;
    for (var i = 0; i < n; i++) {
      // Priority weight: first constraint (i=0) has the highest weight.
      final weight = (n - i).toDouble();
      score += weight * effective[i].penalty(format, stats);
    }
    if (score < bestScore) {
      bestScore = score;
      best = format;
    }
  }
  return best;
}