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

Convert InputImage to Base64 #594

Open
hendry456 opened this issue Mar 26, 2024 · 1 comment
Open

Convert InputImage to Base64 #594

hendry456 opened this issue Mar 26, 2024 · 1 comment
Labels
InputImage Issues related to InputImage

Comments

@hendry456
Copy link

hendry456 commented Mar 26, 2024

Hi, i want to thank you for this library, it is really useful for me. One thing i want to ask is, can i get the Base64 version from the InputImage object?

i tried this code

String inputImageToBase64(InputImage inputImage) {
  // Get image byte data
  final byteData = inputImage.bytes;
  if (byteData == null) {
    throw Exception("Failed to get byte data from InputImage");
  }
  final imageData = byteData.buffer.asUint8List();

  // Encode image byte data to Base64
  final base64Image = base64Encode(imageData);

  return base64Image;
}

but it seems like it didn't gave me the right Base64, i couldn't process it in my api server.

i searched in every website but i couldn't find the solution. Or am i missing something somewhere in the process? Any help is appreciated. Thank you!

@fbernaly fbernaly added the InputImage Issues related to InputImage label Apr 17, 2024
@scognito
Copy link

scognito commented Sep 9, 2024

This is not a bug, anyway I use this code and it works:

Future<String?> convertImageToBase64(Image image) async {
    try {
      // Step 1: Convert the image to an ImageProvider (assuming AssetImage or NetworkImage)
      final ImageProvider imageProvider = image.image;

      // Step 2: Resolve the image and obtain a ImageStream
      final ImageStream stream = imageProvider.resolve(ImageConfiguration.empty);
      final Completer<ui.Image> completer = Completer<ui.Image>();

      // Step 3: Add a listener to get the image once it is available
      stream.addListener(ImageStreamListener((ImageInfo info, bool _) {
        completer.complete(info.image);
      }));

      // Step 4: Wait for the image to be loaded
      final ui.Image loadedImage = await completer.future;

      // Step 5: Convert the ui.Image to byte data
      final ByteData? byteData = await loadedImage.toByteData(format: ui.ImageByteFormat.png);

      if (byteData != null) {
        // Step 6: Convert the byte data to Uint8List
        final Uint8List uint8List = byteData.buffer.asUint8List();

        // Step 7: Convert the Uint8List to Base64 string
        String base64String = base64Encode(uint8List);

        return base64String;
      }
    } catch (e) {
      debugPrint('Error converting image to base64: $e');
    }
    return null;
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
InputImage Issues related to InputImage
Projects
None yet
Development

No branches or pull requests

3 participants