fluttercandies / flutter_image_compress

flutter image compress
MIT License
633 stars 214 forks source link

[Discussions] Input format? #257

Open lukehutch opened 1 year ago

lukehutch commented 1 year ago

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.

SoftWyer commented 1 year ago

jpeg and png mostly. More details in the docs.

lukehutch commented 1 year ago

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.

SoftWyer commented 1 year ago

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?

lukehutch commented 1 year ago

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).

murgle2 commented 1 year ago

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

arnabonetraker commented 1 month ago

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.