How to convert image from path to Base64 in flutter?

2,805

print function did not print everything.
you can see full result with debug mode
and copy paste full result in debug mode to online tool

enter image description here

enter image description here

Share:
2,805
Sprowk
Author by

Sprowk

Updated on December 01, 2022

Comments

  • Sprowk
    Sprowk over 1 year

    I'm trying to capture an image, then send path of the image to a function that returns the image in Base64. For capturing an image I'm using the example on Flutter website.

    Future _takePhoto(BuildContext context) async {
      try {
        await _initializeControllerFuture;
    
        final path = join(
          (await getTemporaryDirectory()).path,
          '${DateTime.now()}.png',
        );
    
        await _cameraController.takePicture(path);
    
        setState(() {
          _imagePath = path;
        });
    
      } catch (e) {
        print(e);
      }
    }
    

    It's working well. I can see the captured image in widget Image.file(File(_imagePath))

    The problem starts when I'm trying to convert the image into Base64.

    File file = File(_imagePath);
    final _imageFile = ImageProcess.decodeImage(
      file.readAsBytesSync(),
    );
    
    String base64Image = base64Encode(ImageProcess.encodePng(_imageFile));
    print(base64Image);
    

    I copy and paste the printed message into an online tool that generates an image from Base64 and either it's all black or there is tiny top layer of the image and the rest is black.

  • Sprowk
    Sprowk about 4 years
    Where can I see variables during testing?
  • chunhunghan
    chunhunghan about 4 years
    In the bottom of Android Studio. You can see 5: Debug, please see my updated picture