-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_data.dart
68 lines (61 loc) · 2.3 KB
/
image_data.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import 'dart:io';
import 'package:camera/camera.dart';
import 'package:flutter/services.dart';
import 'package:gmlkit_liveness/data/models/camera_image.dart';
import 'package:google_mlkit_face_detection/google_mlkit_face_detection.dart';
class CameraImageMetaData {
final CameraLensDirection lensDirection;
final DeviceOrientation deviceOrientation;
final int sensorOrientation;
final CameraImage cameraImage;
final _orientations = {
DeviceOrientation.portraitUp: 0,
DeviceOrientation.landscapeLeft: 90,
DeviceOrientation.portraitDown: 180,
DeviceOrientation.landscapeRight: 270,
};
CameraImageMetaData({
required this.lensDirection,
required this.deviceOrientation,
required this.sensorOrientation,
required this.cameraImage,
});
InputImage? inputImageFromCameraImageMetaData() {
InputImageRotation? rotation = _getPlatformRotation();
if (rotation == null) return null;
final format = InputImageFormatValue.fromRawValue(
cameraImage.format.raw,
);
if (format == null ||
(Platform.isAndroid && format != InputImageFormat.yuv_420_888) ||
(Platform.isIOS && format != InputImageFormat.bgra8888)) return null;
final plane = cameraImage.planes.first;
return InputImage.fromBytes(
bytes: cameraImage.getNv21Uint8List(),
metadata: InputImageMetadata(
size: Size(cameraImage.width.toDouble(), cameraImage.height.toDouble()),
rotation: rotation, // used only in Android
format: format, // used only in iOS
bytesPerRow: plane.bytesPerRow, // used only in iOS
),
);
}
InputImageRotation? _getPlatformRotation() {
if (!Platform.isAndroid && !Platform.isIOS) return null;
if (Platform.isIOS) {
return InputImageRotationValue.fromRawValue(sensorOrientation);
} else if (Platform.isAndroid) {
var rotationCompensation = _orientations[deviceOrientation];
if (rotationCompensation == null) return null;
if (lensDirection == CameraLensDirection.front) {
rotationCompensation = (sensorOrientation + rotationCompensation) % 360;
} else {
rotationCompensation =
(sensorOrientation - rotationCompensation + 360) % 360;
}
return InputImageRotationValue.fromRawValue(rotationCompensation);
} else {
return null;
}
}
}