How to convert JPG images to PNG format. My current approach involves using a method that should theoretically convert the images and save them in the gallery. However, I’m encountering an issue where the images are being saved to the gallery, but they remain in JPG format rather than converting to PNG.
Here’s the method I’m using:
getImages() async {
File _image;
final pickedFile = await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
if (pickedFile != null) {
img.Image image = decodeImage(pickedFile.readAsBytesSync());
img.Image thumbnail = copyResize(image, width: 120);
_image = File(pickedFile.path)..writeAsBytesSync(img.encodePng(thumbnail));
print("_image.path");
print(_image.path);
GallerySaver.saveImage(_image.path, albumName: 'Image Resizer')
.then((bool success) {
Fluttertoast.showToast(
msg: "Image saved",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 5,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0);
});
print('_image');
print(_image.toString());
} else {
print('No image selected.');
}
return _image;
});
}
In this method, I’m using the ImagePicker to select an image from the gallery, then using decodeImage and copyResize to process the image, and finally attempting to save it as a PNG using writeAsBytesSync(img.encodePng(thumbnail)). Despite these steps, the image saved in the gallery is still in JPG format. I need help understanding why the conversion isn’t working as expected and how to ensure the images are saved in PNG format.