flutter_image_compress 0.5.2

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 98

flutter_image_compress #

ImageCompress pub package GitHub GitHub stars Awesome

Compresses image as native plugin (Obj-C/Kotlin)

This library can works on Android and iOS.

Why #

Q:Dart already has image compression libraries. Why use native?

A:For unknown reasons, image compression in Dart language is not efficient, even in release version. Using isolate does not solve the problem.

About version #

flutter sdk versionplugin version
1.5.9+use git ref
1.5.8 or low0.5.1

For reasons in this issue, version 0.5.0 is not compatible with pub and cannot be searched on pub. If you want to support flutter 1.6.0 use git ref.

Incompatibility between flutter SDK.

dependencies:
  flutter_image_compress:
    git:
      url: https://github.com/OpenFlutter/flutter_image_compress.git
      ref: 5a545f7e5409d091cd18f5891a8b07426f390729

Breaking change at version 0.5.1 #

Because #46 and subsequent changes have caused the image Angle to be automatically corrected based on Exif" this default behavior conflicts with the previous argument 'rotate' to rotate the Angle based on Exif information, so I think this is a destructive change.

About params #

minWidth and minHeight #

minWidth and minHeight are constraints on image scaling.

For example, a 4000*2000 image, minWidth set to 1920, minHeight set to 1080, the calculation is as follows:

// Using dart as an example, the actual implementation is Kotlin or OC.
import 'dart:math' as math;

void main() {
  var scale = calcScale(
    srcWidth: 4000,
    srcHeight: 2000,
    minWidth: 1920,
    minHeight: 1080,
  );

  print("scale = $scale"); // scale = 1.8518518518518519
  print("target width = ${4000 / scale}, height = ${2000 / scale}"); // target width = 2160.0, height = 1080.0
}

double calcScale({
  double srcWidth,
  double srcHeight,
  double minWidth,
  double minHeight,
}) {
  var scaleW = srcWidth / minWidth;
  var scaleH = srcHeight / minHeight;

  var scale = math.max(1.0, math.min(scaleW, scaleH));

  return scale;
}

If your image width is smaller than minWidth or height samller than minHeight, scale will be 1, that is, the size will not change.

autoCorrectionAngle #

This property only exists in the version after 0.5.0.

And for historical reasons, there may be conflicts with rotate attributes, which need to be self-corrected.

Modify rotate to 0 or autoCorrectionAngle to false.

Android #

You may need to update Kotlin to version 1.2.71(Recommend 1.3.21) or higher.

iOS #

No problems currently found.

Usage #

dependencies:
  flutter_image_compress: ^0.5.1
import 'package:flutter_image_compress/flutter_image_compress.dart';

Use as:

See full example

  Future<List<int>> testCompressFile(File file) async {
    var result = await FlutterImageCompress.compressWithFile(
      file.absolute.path,
      minWidth: 2300,
      minHeight: 1500,
      quality: 94,
      rotate: 90,
    );
    print(file.lengthSync());
    print(result.length);
    return result;
  }

  Future<File> testCompressAndGetFile(File file, String targetPath) async {
    var result = await FlutterImageCompress.compressAndGetFile(
        file.absolute.path, targetPath,
        quality: 88,
        rotate: 180,
      );

    print(file.lengthSync());
    print(result.lengthSync());

    return result;
  }

  Future<List<int>> testCompressAsset(String assetName) async {
    var list = await FlutterImageCompress.compressAssetImage(
      assetName,
      minHeight: 1920,
      minWidth: 1080,
      quality: 96,
      rotate: 180,
    );

    return list;
  }

  Future<List<int>> testComporessList(List<int> list) async {
    var result = await FlutterImageCompress.compressWithList(
      list,
      minHeight: 1920,
      minWidth: 1080,
      quality: 96,
      rotate: 135,
    );
    print(list.length);
    print(result.length);
    return result;
  }

Result #

The result of returning a List collection will not have null, but will always be an empty array.

The returned file may be null. In addition, please decide for yourself whether the file exists.

About List<int> and Uint8List

You may need to convert List<int> to Uint8List to display images.

To use Uint8List, you need import package to your code like so:

img

final image = Uint8List.fromList(imageList)
ImageProvider provider = MemoryImage(Uint8List.fromList(imageList));

Usage in Image Widget:

List<int> image = await testCompressFile(file);
ImageProvider provider = MemoryImage(Uint8List.fromList(image));

Image(
  image: provider ?? AssetImage("img/img.jpg"),
),

Write to file usage:

void writeToFile(List<int> image, String filePath) {
  final file = File(filePath);
  file.writeAsBytes(image, flush: true, mode: FileMode.write);
}

Troubleshooting #

Compressing returns null

Sometimes, compressing will return null. You should check if you can read/write the file, and the parent folder of the target file must exist.

For example, use the path_provider plugin to access some application folders, and use a permission plugin to request permission to access SD cards on Android/iOS.

Android build error #

Caused by: org.gradle.internal.event.ListenerNotificationException: Failed to notify project evaluation listener.
        at org.gradle.internal.event.AbstractBroadcastDispatch.dispatch(AbstractBroadcastDispatch.java:86)
        ...
Caused by: java.lang.AbstractMethodError
        at org.jetbrains.kotlin.gradle.plugin.KotlinPluginKt.resolveSubpluginArtifacts(KotlinPlugin.kt:776)
        ...

See flutter/flutter/issues#21473

You need to upgrade your Kotlin version to 1.2.71+.

If Flutter supports more platforms (Windows, Mac, Linux, etc) in the future and you use this library, propose an issue or PR!

About EXIF information #

Using this library, EXIF information will be removed.

Although it will not be retained, over version 0.5.0, there will be the option of "automatic angle correction", which will be turned on by default.

CHANGELOG #

0.5.2 #

Fix:

  • #49: A problem of reading Exif information.

0.5.1 #

Change reportError with flutter stable version.

Breaking Change: The autoCorrectionAngle parameter causes a number of situations to behave differently than 0.4.0. See readme for details.

0.5.0 #

(don't use)

Breaking Change: Because FlutterError.reportError method's param context type changed. So this library will add the constraints of flutter SDK so that users before 1.5.9 will not use version 0.5.0 incorrectly.

0.4.0 #

Some code has been added to ensure that parameters that do not pass in native do not trigger crash.

0.3.1 #

Fix:

  • Android close file output stream.

0.3.0 #

Fix:

  • optimize compress scale.

0.2.4 #

Updated Kotlin version

Breaking change. Migrate from the deprecated original Android Support Library to AndroidX. This shouldn't result in any functional changes, but it requires any Android apps using this plugin to also migrate if they're using the original support library.

0.2.3 #

change iOS return type

0.2.2 #

add some dart doc

0.2.1 #

update readme

0.2.0 #

The version number is updated so that people who can use the higher version of gradle can use it. see pr #8

if android run error, you must update your kotlin'version to 1.2.71+

0.1.4 #

add optional params rotate

fix bug

update example

0.1.3 #

fix the ios flutter.h bug

0.1.1 #

update readme

0.1.0 #

first version

example/README.md

flutter_image_compress_example #

Demonstrates how to use the flutter_image_compress plugin.

Getting Started #

For help getting started with Flutter, view our online documentation.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_image_compress: ^0.5.2

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_image_compress/flutter_image_compress.dart';
  
Version Uploaded Documentation Archive
0.5.2 May 23, 2019 Go to the documentation of flutter_image_compress 0.5.2 Download flutter_image_compress 0.5.2 archive
0.5.1 May 20, 2019 Go to the documentation of flutter_image_compress 0.5.1 Download flutter_image_compress 0.5.1 archive
0.5.0 May 19, 2019 Go to the documentation of flutter_image_compress 0.5.0 Download flutter_image_compress 0.5.0 archive
0.4.0 Apr 17, 2019 Go to the documentation of flutter_image_compress 0.4.0 Download flutter_image_compress 0.4.0 archive
0.3.1 Apr 4, 2019 Go to the documentation of flutter_image_compress 0.3.1 Download flutter_image_compress 0.3.1 archive
0.3.0 Apr 4, 2019 Go to the documentation of flutter_image_compress 0.3.0 Download flutter_image_compress 0.3.0 archive
0.2.4 Jan 27, 2019 Go to the documentation of flutter_image_compress 0.2.4 Download flutter_image_compress 0.2.4 archive
0.2.3 Dec 18, 2018 Go to the documentation of flutter_image_compress 0.2.3 Download flutter_image_compress 0.2.3 archive
0.2.2 Nov 28, 2018 Go to the documentation of flutter_image_compress 0.2.2 Download flutter_image_compress 0.2.2 archive
0.2.1 Nov 6, 2018 Go to the documentation of flutter_image_compress 0.2.1 Download flutter_image_compress 0.2.1 archive

All 16 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
96
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
98
Learn more about scoring.

We analyzed this package on Jun 12, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.3.1
  • pana: 0.12.17
  • Flutter: 1.5.4-hotfix.2

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.28.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11
meta 1.1.6 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8