Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image dimension, ByteBuffer size and format don't match. Please check if the ByteBuffer is in the decalred format. #314

Closed
bqubique opened this issue Aug 17, 2022 · 3 comments
Labels
Image capture Issues related to image capturing using camera or image_picker plugins InputImage Issues related to InputImage

Comments

@bqubique
Copy link

bqubique commented Aug 17, 2022

/flutter (32499): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: PlatformException(error, Image dimension, ByteBuffer size and format don't match. Please check if the ByteBuffer is in the decalred format., null, java.lang.IllegalArgumentException: Image dimension, ByteBuffer size and format don't match. Please check if the ByteBuffer is in the decalred format.

I'm getting this error when using Pose Detector. This is how I receive the image from camera:

    ...
    await cameraController.initialize();
    cameraController.startImageStream((image) => _processCameraImage(image));

_processCameraImage(CameraImage image) method is as follows:

Future _processCameraImage(CameraImage image) async {
    final WriteBuffer allBytes = WriteBuffer();
    for (final Plane plane in image.planes) {
      allBytes.putUint8List(plane.bytes);
    }
    final bytes = allBytes.done().buffer.asUint8List();

    final Size imageSize =
        Size(image.width.toDouble(), image.height.toDouble());

    final camera = _cameras[_cameraIndex];
    final imageRotation =
        InputImageRotationValue.fromRawValue(camera.sensorOrientation) ??
            InputImageRotation.rotation0deg;

    final inputImageFormat =
        InputImageFormatValue.fromRawValue(image.format.raw) ??
            InputImageFormat.nv21;

    final planeData = image.planes.map(
      (Plane plane) {
        return InputImagePlaneMetadata(
          bytesPerRow: plane.bytesPerRow,
          height: plane.height,
          width: plane.width,
        );
      },
    ).toList();

    final inputImageData = InputImageData(
      size: imageSize,
      imageRotation: imageRotation,
      inputImageFormat: inputImageFormat,
      planeData: planeData,
    );

    final inputImage =
        InputImage.fromBytes(bytes: bytes, inputImageData: inputImageData);

    processImage(inputImage);
  }

processImage(InputImage inputImage) is as follows:

Future<void> processImage(InputImage inputImage) async {
    if (!_canProcess) return;
    if (_isBusy) return;
    _isBusy = true;

    final poses = await poseDetector.processImage(inputImage); <<<---- the error is thrown here

    if (poses.isNotEmpty) {
      
      // log('hello');
    }
    if (inputImage.inputImageData?.size != null &&
        inputImage.inputImageData?.imageRotation != null) {
      // log('hello');
    } else {
      // log('hello');
    }
    _isBusy = false;
    if (mounted) {
      setState(() {});
    }
  }

pubspec.yaml

  camera: ^0.10.0
  google_mlkit_pose_detection: ^0.2.0
@meet7-sagar23
Copy link

Facing the same issue on One Plus 7 Pro.

    animate_do: ^3.0.2
    camera: ^0.10.1
    collection: ^1.16.0
    equatable: ^2.0.5
    flutter:
        sdk: flutter
    google_mlkit_face_detection: ^0.5.0
    image: ^4.0.5

@fbernaly fbernaly added InputImage Issues related to InputImage Image capture Issues related to image capturing using camera or image_picker plugins labels Jun 26, 2023
@fbernaly
Copy link
Collaborator

Use NV21 for Android.
In this comment: #287 (comment)
Someone reported that the native apps are using that for MLKit and when doing the same in Flutter solved the issue for Android. That was merged in this PR #454. Update to the latest plugin version.

@MikheilMusaelyan
Copy link

MikheilMusaelyan commented Dec 28, 2023

Same issue in this function:

void detectit(XFile file) async {
    final myFile = File(file.path);
    final Uint8List imageBytes = await myFile.readAsBytes();
    final dimensions = await getImageDimensions(imageBytes);
    
    int bytesperrow = dimensions['w'] * 4;
    int w = dimensions['w'];
    int h = dimensions['h'];

    final inputImage = InputImage.fromBytes(
      bytes: imageBytes,
      metadata: InputImageMetadata(
        size: Size(
          w.toDouble(),
          h.toDouble()
        ),
        format: InputImageFormat.bgra8888,
        rotation: InputImageRotation.rotation0deg,
        bytesPerRow: bytesperrow
      )
    );
    
    final List<Pose> poses = await poseDetector.processImage(inputImage);
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Image capture Issues related to image capturing using camera or image_picker plugins InputImage Issues related to InputImage
Projects
None yet
Development

No branches or pull requests

4 participants