All Projects → OpenFlutter → Flutter_image_compress

OpenFlutter / Flutter_image_compress

Licence: mit
flutter image compress

Programming Languages

dart
5743 projects

Projects that are alternatives of or similar to Flutter image compress

CodeProject
Common code for unity project develop.
Stars: ✭ 28 (-91.95%)
Mutual labels:  compress
minizip-asm.js
Minizip in javascript. Work with password. Demo:
Stars: ✭ 38 (-89.08%)
Mutual labels:  compress
PLzmaSDK
PLzmaSDK is (Portable, Patched, Package, cross-P-latform) Lzma SDK.
Stars: ✭ 28 (-91.95%)
Mutual labels:  compress
imgZip
js压缩图片,转blod流.上传
Stars: ✭ 25 (-92.82%)
Mutual labels:  compress
ImageCompress
一个超级强大的一行代码图片压缩工具,支持多个压缩、单个压缩、压缩后的bitmap
Stars: ✭ 73 (-79.02%)
Mutual labels:  compress
pdf-scripts
📑 Scripts to repair, verify, OCR, compress, wrangle, crop (etc.) PDFs
Stars: ✭ 33 (-90.52%)
Mutual labels:  compress
pyas2-lib
AS2 Library for building and parsing Messages and MDNs
Stars: ✭ 33 (-90.52%)
Mutual labels:  compress
Filepicker
🔥🔥🔥Android文件、图片选择器,可按文件夹查找,文件类型查找,支持自定义相机
Stars: ✭ 265 (-23.85%)
Mutual labels:  compress
img-master
An image batch processing tool with multifunctional and unlimited
Stars: ✭ 63 (-81.9%)
Mutual labels:  compress
pylovepdf
ilovepdf.com python API library
Stars: ✭ 52 (-85.06%)
Mutual labels:  compress
compress-pdf-tg-bot
A Telegram bot can compress the size of PDF documents. Useful for digital documentations.
Stars: ✭ 24 (-93.1%)
Mutual labels:  compress
image-compressor
Frontend javascript module for resizing and compressing images
Stars: ✭ 41 (-88.22%)
Mutual labels:  compress
csso-webpack-plugin
CSSO full restructuring minification files to serve your webpack bundles
Stars: ✭ 104 (-70.11%)
Mutual labels:  compress
photo-magician
🎨 provide some common image process apis with canvas
Stars: ✭ 12 (-96.55%)
Mutual labels:  compress
EasyCompressor
⚡ A compression library that implements many compression algorithms such as LZ4, Zstd, LZMA, Snappy, Brotli, GZip, and Deflate. It helps you to improve performance by reducing Memory Usage and Network Traffic for caching.
Stars: ✭ 167 (-52.01%)
Mutual labels:  compress
tinypng-free
Use the upload api of tinypng's homeage to compress images
Stars: ✭ 29 (-91.67%)
Mutual labels:  compress
react-native-compressor
The lightweight library for compress image, video, and audio with an awesome experience
Stars: ✭ 157 (-54.89%)
Mutual labels:  compress
Imageoptim Cli
Make optimisation of images part of your automated build process
Stars: ✭ 3,215 (+823.85%)
Mutual labels:  compress
lzbase62
LZ77(LZSS) based compression algorithm in base62 for JavaScript.
Stars: ✭ 38 (-89.08%)
Mutual labels:  compress
ACCV TinyGAN
BigGAN; Knowledge Distillation; Black-Box; Fast Training; 16x compression
Stars: ✭ 62 (-82.18%)
Mutual labels:  compress

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 don't you use dart to do it

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.

1.0.0

The 1.0.0 is null-safety version.

Please read document for null-safety information in dart or flutter.

Usage

dependencies:
  flutter_image_compress: ^1.0.0-nullsafety
import 'package:flutter_image_compress/flutter_image_compress.dart';

Use as:

See full example

There are several ways to use the library api.

  // 1. compress file and get Uint8List
  Future<Uint8List> 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;
  }

  // 2. compress file and get file.
  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;
  }

  // 3. compress asset and get Uint8List.
  Future<Uint8List> testCompressAsset(String assetName) async {
    var list = await FlutterImageCompress.compressAssetImage(
      assetName,
      minHeight: 1920,
      minWidth: 1080,
      quality: 96,
      rotate: 180,
    );

    return list;
  }

  // 4. compress Uint8List and get another Uint8List.
  Future<Uint8List> testComporessList(Uint8List 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 common 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.

rotate

If you need to rotate the picture, use this parameter.

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.

quality

Quality of target image.

If format is png, the param will be ignored in iOS.

format

Supports jpeg or png, default is jpeg.

The format class sign enum CompressFormat.

Heif and webp Partially supported.

Webp

Support android by the system api (speed very nice).

And support iOS, but However, no system implementation, using third-party libraries used, it is not recommended due to encoding speed. In the future, libwebp by google (c / c ++) may be used to do coding work, bypassing other three-party libraries, but there is no guarantee of implementation time.

HEIF(Heic)

Heif for iOS

Only support iOS 11+.

Heif for Android

Use HeifWriter to implemation.

Only support API 28+.

And may require hardware encoder support, does not guarantee that all devices above API28 are available

inSampleSize

The param is only support android.

For a description of this parameter, see the Android official website.

keepExif

If this parameter is true, EXIF information is saved in the compressed result.

Attention should be paid to the following points:

  1. Default value is false.
  2. Even if set to true, the direction attribute is not included.
  3. Only support jpg format, PNG format does not support.

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 this:

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

Runtime Error

Because of some support issues, all APIs will be compatible with format and system compatibility, and an exception (UnsupportError) may be thrown, so if you insist on using webp and heic formats, please catch the exception yourself and use it on unsupported devices jpeg compression.

Example:

Future<Uint8List> compressAndTryCatch(String path) async {
    Uint8List result;
    try {
      result = await FlutterImageCompress.compressWithFile(path,
          format: CompressFormat.heic);
    } on UnsupportedError catch (e) {
      print(e);
      result = await FlutterImageCompress.compressWithFile(path,
          format: CompressFormat.jpeg);
    }
    return result;
  }

Android

You may need to update Kotlin to version 1.3.72 or higher.

iOS

No problems currently found.

Troubleshooting or common error

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+(recommended 1.3.72).

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 by default.

EXIF information can be retained by setting keepExif to true, but not direction information.

LICENSE

The code under MIT style.

PNG/JPEG encoder

Each using system API.

Webp encoder

Use SDWebImageWebPCoder to encode the UIImage in iOS. (Under MIT)

Android code use the Android system api.

HEIF encoder

Use iOS system api in iOS.

Use HeifWriter(androidx component by Google) to encode in androidP or higher.

About Exif handle code

The iOS code was copied from dvkch/SYPictureMetadata, LICENSE

The android code was copied from flutter/plugin/image_picker and edit some. (BSD 3 style)

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].