You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
diplomatic-quarter/lib/vital_signs/components/bounding_box_widget.dart

88 lines
2.3 KiB
Dart

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:vital_sign_camera/vital_sign_camera.dart';
class BoundingBoxWidget extends StatelessWidget {
final Size deviceSize;
final NormalizedFaceBox? facebox;
final VideoFrameInfo? videoFrameInfo;
const BoundingBoxWidget({
Key? key,
required this.deviceSize,
required this.facebox,
required this.videoFrameInfo,
}) : super(key: key);
@override
Widget build(BuildContext context) {
if (facebox == null || videoFrameInfo == null) {
return Container();
}
// Get a map with left, top, width, and height of the bounding box for the face
Map boundingBox = computeBoundingBox();
return Positioned(
left: boundingBox['left'],
top: boundingBox['top'],
child: Container(
width: boundingBox['width'],
height: boundingBox['height'],
decoration: BoxDecoration(
border: Border.all(
color: Colors.red,
width: 2,
)),
),
);
}
// Helper function to compute the left, top, width, and height of the bounding box
Map computeBoundingBox() {
double width = videoFrameInfo!.width;
double height = videoFrameInfo!.height;
double windowWidth = deviceSize.width;
double windowHeight = deviceSize.height;
double screenAspectRatio = deviceSize.aspectRatio;
double frameAspectRatio = width / height;
int xTransform = 1;
if (Platform.isAndroid) {
xTransform = -1;
}
double scale = 1;
if (screenAspectRatio < frameAspectRatio) {
scale = windowHeight / height;
} else {
scale = windowWidth / width;
}
Map scaledFrameBounds = {
'x': (windowWidth - scale * width) / 2,
'y': (windowHeight - scale * height) / 2,
'width': scale * width,
'height': scale * height,
};
double x = facebox!.xCenter - facebox!.width / 2;
double y = facebox!.yCenter - facebox!.height / 2;
if (xTransform < 0) {
x = 1 - x - facebox!.width;
}
Map result = {
'left': scaledFrameBounds['x'] + x * scaledFrameBounds['width'],
'top': scaledFrameBounds['y'] + y * scaledFrameBounds['height'],
'width': facebox!.width * scaledFrameBounds['width'],
'height': facebox!.height * scaledFrameBounds['height'],
};
return result;
}
}