Flutter Image Compression

4,883

you can use below plugin, same working fine without any issue, even faster and what i expected

https://github.com/btastic/flutter_native_image.git

steps and method available in above link.

Share:
4,883
Martin Seubert
Author by

Martin Seubert

Updated on December 10, 2022

Comments

  • Martin Seubert
    Martin Seubert over 1 year

    I wanted to do some image compression before uploading to my Firestore and ran into following thread: Flutter & Firebase: Compression before upload image

    The uploading works totally fine, but I cannot recognize any compression regarding the file size... Even if I decrease the quality from 85 to 1, the file still has the same size. Same thing if I upload the image without calling the compression method at all. Here is the code snippet:

    void compressImage() async {
        print('starting compression');
        final tempDir = await getTemporaryDirectory();
        final path = tempDir.path;
        int rand = new Math.Random().nextInt(10000);
    
        Im.Image image = Im.decodeImage(file.readAsBytesSync());
        Im.copyResize(image, 500);
    
    //    image.format = Im.Image.RGBA;
    //    Im.Image newim = Im.remapColors(image, alpha: Im.LUMINANCE);
    
        Im.Image smallerImage = Im.copyResize(image, 500); // choose the size here, it will maintain aspect ratio
    
    
        var newim2 = new File('$path/img_$rand.jpg')
          ..writeAsBytesSync(Im.encodeJpg(smallerImage, quality: 85));
    
        setState(() {
          file = newim2;
        });
        print('done');
      }
    

    Any idea what I have to change, to make the compression working?

    Best regards!