tim-montague
tim-montague

Reputation: 17412

Using Flutter Camera package, how do I convert a photo to a base64 string?

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

Upvotes: 0

Views: 1056

Answers (2)

G H Prakash
G H Prakash

Reputation: 1857

Try this.

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

Upvotes: 0

tim-montague
tim-montague

Reputation: 17412

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

Upvotes: 1

Related Questions