I'm using the Image
class from the package image. I have to download a image from internet and save it in my local storage. Then modify this image and save the changes. I have done the first 2 steps, but when i'm saving the file, this is corrupted and the Image Viewer dont recognizit as a image file. This is the code where the image is saved after the changes.
var response = await client.get(urlToImage);
var img = await File('./network_image.jpg').writeAsBytes(response.bodyBytes);
Image image = decodeImage(img.readAsBytesSync())
var f = await File('./image.jpg').writeAsBytes(image.getBytes()); // this doesnt work
// var f = await File('./image.jpg').writeAsBytes(image.data); // this doesnt work
The File.writeAsBytes
expects a List<int> bytes
, and the Image.data
and Image.getBytes()
returns Uint32List
and Uint8List
respectively.
I'm not using Flutter, only dart for a command line program.
The image object stores the image data in an unencoded format. Remember you decoded it with decodeImage
. It stores the raw colors of the image. You need to re encode the image data or your image viewer needs to handle whatever image format of data getBytes
returns.
The easier solution is probably to re encode. You seem to want a JpegEncoder
.
var f = await File('./image.jpg').writeAsBytes(JpegEncoder().encodeImage(image));