flutter_image_compress icon indicating copy to clipboard operation
flutter_image_compress copied to clipboard

[Discussions] Input format?

Open lukehutch opened this issue 1 year ago • 6 comments

Content

I can't see any information in the documentation about the supported input formats. Does the input format have to be the same as the output format? I took a look at the Android code, and it looks like it does have to be the same, or maybe the input is assumed to be JPEG.

lukehutch avatar Aug 29 '23 01:08 lukehutch

jpeg and png mostly. More details in the docs.

SoftWyer avatar Aug 29 '23 07:08 SoftWyer

Hi, but this doesn't specify whether the input format and output format have to be the same. Do they have to be? Or are they autodetected?

It would be nice to be able to pass raw rgba pixel data to the compressor, or even better, a ui.Image, to avoid having to compress to PNG or JPEG first.

My usecase is that I am generating an image, and need to compress it to JPEG. It defeats the purpose of using flutter_image_compress if I have to compress the image to JPEG just so that I can pass it to flutter_image_compress in order to have it compressed to JPEG.

lukehutch avatar Aug 29 '23 07:08 lukehutch

Ahh, I think it compresses to the same format, at least there seems to be no option to supply input & output formats.

How are you generating your image? If it's using Flutter and Canvas, then maybe you don't need to use this and can just compress/resize the Canvas and export it as jpeg?

SoftWyer avatar Aug 29 '23 07:08 SoftWyer

Ahh, I think it compresses to the same format, at least there seems to be no option to supply input & output formats.

Yes exactly, that seems to be what the source indicates, if I'm reading it right.

How are you generating your image? If it's using Flutter and Canvas, then maybe you don't need to use this and can just compress/resize the Canvas and export it as jpeg?

Flutter does not include JPEG compression support. Hence the need for flutter_image_compress.

So if I want to compress an image, produced using Canvas, to JPEG, it's basically impossible with flutter_image_compress. I can only compress to PNG, using Flutter's built-in PNG encoder, and then use flutter_image_compress to save it, again, as a PNG (which is basically a no-op).

lukehutch avatar Aug 29 '23 08:08 lukehutch

I had a similar issue and what I ended up doing (for now) is to save it as png using flutter's built-in encoder, and then to jpeg using this package:

    final data = await image.toByteData(format: ImageByteFormat.png);
    final jpg = await FlutterImageCompress.compressWithList(
        data!.buffer.asUint8List(),
        minWidth: 1024,
        minHeight: 1024,
        quality: 90);

You can pass in format argument to compressWithList, jpeg is default. And afaik, only jpeg and png can be the input.

Unfortunately this is slow. I've tried going to ImageByteFormat.rawRgba but doesn't work with this package

murgle2 avatar Aug 30 '23 13:08 murgle2

I think this needs to be labelled as BUG. I couldn't find any reference to the input format. Even if input is PNG it should internally check or convert to it the necessary format.

arnabonetraker avatar Jul 16 '24 11:07 arnabonetraker