Home > Software engineering >  Using Flutter Camera package, how do I convert a photo to a base64 string?
Using Flutter Camera package, how do I convert a photo to a base64 string?

Time:11-23

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

CodePudding user response:

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);
}

CodePudding user response:

Try this.

var image = await ImagePicker.pickImage(source: ImageSource.gallery);
    final bytes = Io.File(image.path).readAsBytesSync();
    String img64 = base64Encode(bytes);
    print (img64);
  • Related