简体   繁体   中英

How to compress a PNG image in Flutter?

I'm new to Fluuter. I am trying to compress or resize image in Flutter, but I always got unsupported error when the image is in PNG format:

Invalid image: ExifInterface got an unsupported image format file(ExifInterface supports JPEG and some RAW image formats only) or a corrupted JPEG file to ExifInterface.
W/ExifInterface(23209): java.io.IOException: Invalid byte order: ffff8950
W/ExifInterface(23209):     at android.media.ExifInterface.readByteOrder(ExifInterface.java:3121)
W/ExifInterface(23209):     at android.media.ExifInterface.isOrfFormat(ExifInterface.java:2437)
W/ExifInterface(23209):     at android.media.ExifInterface.getMimeType(ExifInterface.java:2315)
W/ExifInterface(23209):     at android.media.ExifInterface.loadAttributes(ExifInterface.java:1753)
W/ExifInterface(23209):     at android.media.ExifInterface.initForFilename(ExifInterface.java:2297)
W/ExifInterface(23209):     at android.media.ExifInterface.<init>(ExifInterface.java:1384)
W/ExifInterface(23209):     at io.flutter.plugins.imagepicker.ExifDataCopier.copyExif(ExifDataCopier.java:15)
W/ExifInterface(23209):     at io.flutter.plugins.imagepicker.ImageResizer.resizeImageIfNeeded(ImageResizer.java:41)
W/ExifInterface(23209):     at io.flutter.plugins.imagepicker.ImagePickerDelegate.handleImageResult(ImagePickerDelegate.java:530)

I've tried using image_picker and flutter_image_compress.dart but neither works well with PNG format. It seems these packages only support JPG format. How can I compress the PNG image or somehow convert JPG to PNG?

Here is my code: By uisng image_picker

import 'package:image_picker/image_picker.dart';

    var image = await ImagePicker.pickImage(
        source: ImageSource.gallery,
        maxHeight: 250, // it says this params have compression function but only for JPEG
        maxWidth: 250,
        imageQuality: 90
    );

By uisng flutter_image_compress

import 'package:flutter_image_compress/flutter_image_compress.dart';

//if the origin image is PNG, it doesn't work
    var compressImageData = await FlutterImageCompress.compressWithFile( //returns Future<List<int>>
        image_path,
        minWidth: 50,
        minHeight: 50,
        quality: 100,
        format: CompressFormat.png //e.g. compress to PNG
    );

Thanks in advance.

Just using new Plugin name : File_support url : https://pub.dev/packages/file_support

This file support plugin is good when I used first time.It drastically reduce file size 2 mb to 70 kb.

 File compressimage = await FileSupport().compressImage(<Your image file>);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM