Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter convert image to binary data

Tags:

flutter

How can i covert Image file to binary data ? I'm using a library call image_picker for pick the image from gallery or camera. And I want to convert the image that I picked to binary data.

File image = await ImagePicker.pickImage(source: ImageSource.gallery)
(image as Image).toByteData // the method toByteData here is not pop up.
like image 226
Chheangly Prum Avatar asked Jul 20 '26 12:07

Chheangly Prum


2 Answers

toByteData() method allows converting the image into a byte array. We need to pass the format in the format argument which specifies the format in which the bytes will be returned. It'll return the future that completes with binary data or error.

final pngByteData = await image.toByteData(format: ImageByteFormat.png);

ImageByteFormat enum contains the following constants.

  • png
  • rawRgba
  • rawUnmodified
  • values

For more information about ImageByteFormat, please have a look at this documentation.

Update : If you want to convert the image file into bytes. Then use readAsByte() method.

var bytes = await ImagePicker.pickImage(source: ImageSource.gallery).readAsBytes();

For converting image into a file, Check out this answer.

like image 169
Vinoth Vino Avatar answered Jul 23 '26 06:07

Vinoth Vino


simply use this method

var bytes = await File('filename').readAsBytes();
like image 41
Muhammad Noman Avatar answered Jul 23 '26 05:07

Muhammad Noman