cameraImageToInputImage function
Convert a CameraImage to an InputImage for ML Kit processing.
Returns null if the image format or rotation cannot be determined.
Implementation
InputImage? cameraImageToInputImage(
CameraImage image,
int sensorOrientation,
) {
final rotation = InputImageRotationValue.fromRawValue(sensorOrientation);
if (rotation == null) return null;
final format = InputImageFormatValue.fromRawValue(image.format.raw);
if (format == null) return null;
// nv21 (Android) and bgra8888 (iOS) are single-plane formats.
// yuv420 (Android fallback) has multiple planes that must be concatenated.
final Uint8List bytes;
if (image.planes.length == 1) {
bytes = image.planes[0].bytes;
} else {
bytes = _concatenatePlanes(image.planes);
}
return InputImage.fromBytes(
bytes: bytes,
metadata: InputImageMetadata(
size: Size(image.width.toDouble(), image.height.toDouble()),
rotation: rotation,
format: format,
bytesPerRow: image.planes[0].bytesPerRow,
),
);
}