Flutter Android rotate image from camera stream for ocr

Viewed 417

Good day. I am developing a cash reader for people like me that is blind. I get the image from camera stream and with regular expression grab the digits from the banknote and use google_ml_kit to get the nomination. But it only works if the banknote is in the uprite position. So I also do a check if the number startswith '0' then I want the image to be rotated 180 degrees or if a uint8list flipped. But this is where I run into how to do this. I searched and could not get a way to flip the uint8list, to see if it will work if I reverse it. Also I seem not to find a way to rotate the image from the camera stream. Even though in the code below there is a image rotate variable but do nothing if I set it to 180. Can anyone please direct me to a solution. Below is part of the code I think is relevant on this.

    Future _processCameraImage(CameraImage image) async {
      final WriteBuffer allBytes = WriteBuffer();
      for (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 =
          InputImageRotationMethods.fromRawValue(camera.sensorOrientation) ??
              InputImageRotation.Rotation_0deg;

      final inputImageFormat =
          InputImageFormatMethods.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);

    

      widget.onImage(inputImage);
      List? speakText = [];
      speakText = widget.outputText;
      if (speakText!.isNotEmpty) {
        await _speak('${widget.outputText}');
      } else {
        _speak('');
      }
    }
0 Answers
Related