Skip to content

keshava19/flutter_image_compress

 
 

Repository files navigation

flutter_image_compress

pub package GitHub GitHub stars Awesome

compress image with native code(objc kotlin)

This library can work on android/ios.

why

Q:Dart has image related libraries to compress. Why use native?

A:For reasons unknown, using dart language is not efficient, even in release version, using isolate can not solve the problem.

about android

maybe, you need update your kotlin version to 1.2.71 or higher.

about ios

No problems found at present.

use

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

use:

see whole example code

  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;
  }

about List or Uint8List

you maybe need convert List<int> to 'Uint8List' to display image

use Uint8List need import package to your code like this

var u8 = Uint8List.fromList(list)
ImageProvider provider = MemoryImage(Uint8List.fromList(list));

use in Image Widget

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

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

write to file

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

compress return null

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

For example, use the path_provider plugin to access some application folders , use a permission plugin to request permission to access sdcard 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 the flutter/flutter/issues#21473

you need edit your kotlin version to 1.2.71+

If flutter supports more platforms (windows, mac, linux, other) in the future and you use this library, propose issue / PR

ABOUT EXIF

Using this library, EXIF information will be removed.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Dart 44.0%
  • Objective-C 28.5%
  • Kotlin 18.8%
  • Ruby 8.7%