11// ignore_for_file: depend_on_referenced_packages
2- import 'package:flutter/foundation.dart' ;
32import 'package:flutter_liveness_detection_randomized_plugin/index.dart' ;
43import 'package:flutter_liveness_detection_randomized_plugin/src/core/constants/liveness_detection_step_constant.dart' ;
54import 'package:collection/collection.dart' ;
@@ -260,8 +259,14 @@ class _LivenessDetectionScreenState extends State<LivenessDetectionView> {
260259
261260 void _startLiveFeed () async {
262261 final camera = availableCams[_cameraIndex];
263- _cameraController =
264- CameraController (camera, ResolutionPreset .high, enableAudio: false );
262+ _cameraController = CameraController (
263+ camera,
264+ widget.config.cameraResolution,
265+ enableAudio: false ,
266+ imageFormatGroup: Platform .isAndroid
267+ ? ImageFormatGroup .nv21
268+ : ImageFormatGroup .bgra8888,
269+ );
265270
266271 _cameraController? .initialize ().then ((_) {
267272 if (! mounted) return ;
@@ -278,39 +283,44 @@ class _LivenessDetectionScreenState extends State<LivenessDetectionView> {
278283 }
279284
280285 Future <void > _processCameraImage (CameraImage cameraImage) async {
281- final WriteBuffer allBytes = WriteBuffer ();
282- for (final Plane plane in cameraImage.planes) {
283- allBytes.putUint8List (plane.bytes);
284- }
285- final bytes = allBytes.done ().buffer.asUint8List ();
286-
287- final Size imageSize = Size (
288- cameraImage.width.toDouble (),
289- cameraImage.height.toDouble (),
290- );
291-
292286 final camera = availableCams[_cameraIndex];
293287 final imageRotation =
294288 InputImageRotationValue .fromRawValue (camera.sensorOrientation);
295289 if (imageRotation == null ) return ;
296290
297- final inputImageFormat =
298- InputImageFormatValue .fromRawValue (cameraImage.format.raw);
299- if (inputImageFormat == null ) return ;
300-
301- final inputImageData = InputImageMetadata (
302- size: imageSize,
303- rotation: imageRotation,
304- format: inputImageFormat,
305- bytesPerRow: cameraImage.planes[0 ].bytesPerRow,
306- );
291+ InputImage ? inputImage;
307292
308- final inputImage = InputImage .fromBytes (
309- metadata: inputImageData,
310- bytes: bytes,
311- );
293+ if (Platform .isAndroid) {
294+ if (cameraImage.format.group == ImageFormatGroup .nv21) {
295+ inputImage = InputImage .fromBytes (
296+ bytes: cameraImage.planes[0 ].bytes,
297+ metadata: InputImageMetadata (
298+ size: Size (
299+ cameraImage.width.toDouble (), cameraImage.height.toDouble ()),
300+ rotation: imageRotation,
301+ format: InputImageFormat .nv21,
302+ bytesPerRow: cameraImage.planes[0 ].bytesPerRow,
303+ ),
304+ );
305+ }
306+ } else if (Platform .isIOS) {
307+ if (cameraImage.format.group == ImageFormatGroup .bgra8888) {
308+ inputImage = InputImage .fromBytes (
309+ bytes: cameraImage.planes[0 ].bytes,
310+ metadata: InputImageMetadata (
311+ size: Size (
312+ cameraImage.width.toDouble (), cameraImage.height.toDouble ()),
313+ rotation: imageRotation,
314+ format: InputImageFormat .bgra8888,
315+ bytesPerRow: cameraImage.planes[0 ].bytesPerRow,
316+ ),
317+ );
318+ }
319+ }
312320
313- _processImage (inputImage);
321+ if (inputImage != null ) {
322+ _processImage (inputImage);
323+ }
314324 }
315325
316326 Future <void > _processImage (InputImage inputImage) async {
0 commit comments