yekeskin / flutter_avif

A flutter plugin to view and encode avif images.
https://pub.dev/packages/flutter_avif
MIT License
61 stars 17 forks source link

[Feature Request] decodeAvif #41

Closed Glutamat42 closed 10 months ago

Glutamat42 commented 11 months ago

it would be nice if i could decode avif images so i can convert avif to jpeg. I would like to work with avif images in my app, but for sharing/downloading i want to convert them to jpeg as this format has way broader support.

yekeskin commented 11 months ago

Thank for the suggestion. I will try to add it to the next release. In the meantime, you can use the following code to get the decoded image:

final bytes = await rootBundle.load("assets/butterfly.avif");
final codec = SingleFrameAvifCodec(bytes: bytes.buffer.asUint8List()); // MultiFrameAvifCodec for animated images
await codec.ready();

final AvifFrameInfo frame = await codec.getNextFrame();
// frame.image
codec.dispose();

frame.image is an instance of dart:ui Image class. You can use a plugin like image to convert it to the desired format.

Glutamat42 commented 11 months ago

Thanks for the code snippet. Got it working, althought it was not directly straight forward as i got type errors (different types of Image)

final codec = SingleFrameAvifCodec(bytes: fileBytes);
await codec.ready();
final image = await codec.getNextFrame();

final byteData = await image.image.toByteData(format: ImageByteFormat.png);
final pngBytes = byteData!.buffer.asUint8List();
fileBytes = await FlutterImageCompress.compressWithList(pngBytes, format: CompressFormat.jpeg, quality: 90);
// alternative with Image package
// final img.Image? imageLibImage = img.decodeImage(pngBytes);
// final jpegFileBytes = img.encodeJpg(imageLibImage!, quality: 90);

codec.dispose();
yekeskin commented 11 months ago

You can directly convert dart:ui Image to image library. https://github.com/brendan-duncan/image/blob/main/doc/flutter.md#convert-a-flutter-ui-image-to-the-dart-image-library