normalizePoint static method
Offset
normalizePoint({
- required Offset local,
- required Size widgetSize,
- required UCameraSize previewSize,
- BoxFit fit = BoxFit.cover,
- bool mirrored = false,
Maps a tap inside a preview box to the normalized (0..1) sensor point the
controller expects, accounting for the preview being letterboxed or
cropped by fit and for a mirrored front camera.
Implementation
static Offset normalizePoint({
required Offset local,
required Size widgetSize,
required UCameraSize previewSize,
BoxFit fit = BoxFit.cover,
bool mirrored = false,
}) {
if (widgetSize.width <= 0 || widgetSize.height <= 0 || previewSize.width <= 0 || previewSize.height <= 0) {
return const Offset(0.5, 0.5);
}
final double previewAspect = previewSize.aspectRatio;
final double widgetAspect = widgetSize.width / widgetSize.height;
double scale;
if (fit == BoxFit.contain) {
scale = previewAspect > widgetAspect ? widgetSize.width / previewSize.width : widgetSize.height / previewSize.height;
} else {
scale = previewAspect > widgetAspect ? widgetSize.height / previewSize.height : widgetSize.width / previewSize.width;
}
final double renderedWidth = previewSize.width * scale;
final double renderedHeight = previewSize.height * scale;
final double offsetX = (widgetSize.width - renderedWidth) / 2;
final double offsetY = (widgetSize.height - renderedHeight) / 2;
double x = ((local.dx - offsetX) / renderedWidth).clamp(0.0, 1.0);
final double y = ((local.dy - offsetY) / renderedHeight).clamp(0.0, 1.0);
if (mirrored) x = 1 - x;
return Offset(x, y);
}