0

Using the Flutter Camera package (v0.9.4+5), how do I convert a captured photo into a base64 string?

tim-montague
  • 16,217
  • 5
  • 62
  • 51

2 Answers2

1

I believe the following code will work, but welcome to any thoughts on how the code can be improved.

import 'dart:convert' as convert;

void capturePhoto() async {

  // Note: `controller` being initialized as shown in readme
  // https://pub.dev/packages/camera#example

  XFile photo = await controller.takePicture();
  List<int> photoAsBytes = await photo.readAsBytes();
  String photoAsBase64 = convert.base64Encode(photoAsBytes);
}
tim-montague
  • 16,217
  • 5
  • 62
  • 51
0

Try this.

var image = await ImagePicker.pickImage(source: ImageSource.gallery);
    final bytes = Io.File(image.path).readAsBytesSync();
    String img64 = base64Encode(bytes);
    print (img64);
G H Prakash
  • 1,720
  • 10
  • 30
  • I'm looking to capture a photo directly from the camera, without saving it to the image library. I think your answer fits this question instead https://stackoverflow.com/questions/50036393/how-to-convert-an-image-to-base64-image-in-flutter – tim-montague Nov 22 '21 at 06:11