flutter_image_utilities 0.0.4

  • Readme
  • Changelog
  • Example
  • Installing
  • 48

flutter_image_utilities #

Image file related utilities for saving an image as JPEG with the specified quality, size and scale mode and for getting image properties.

Features #

  • Supports Android and iOS.
  • Modern plugin implementation based on Kotlin (Android) and Swift (iOS).
  • Uses background processing to keep UI responsive.
  • Save an image file as JPEG using the specified JPEG quality.
  • Resize saved image to a given size.
  • Supports different scaling modes when resizing an image: fitKeepAspectRatio, fillKeepAspectRatio, fitAnyDirectionKeepAspectRatio, fillAnyDirectionKeepAspectRatio
  • Get image width and height.
  • Get image orientation (Android).

Examples #

Save image as JPEG #

final jpegFile = await FlutterImageUtilities.saveAsJpeg(
  sourceFile: File("source_image_file"),
  destinationFilePath: "path/to/dest/file.jpg",
  quality: 60,
  maxWidth: 1920,
  maxHeight: 1080,
  scaleMode: ScaleMode.fitKeepAspectRatio);

Get image properties #

final imageProperties =
  await FlutterImageUtilities.getImageProperties(File("source_image_file"));

0.0.4 #

[Android] Fixed: any thrown exception caused crash. Now exception is delivered correctly to caller.

0.0.3 #

  • [Android] Improved error handling.

0.0.2 #

  • [iOS] Use background thread to keep UI more responsive.

0.0.1 #

  • Initial release.

example/lib/main.dart

// Copyright (c) 2020 KineApps. All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.

import 'dart:io';

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter_image_utilities/flutter_image_utilities.dart';
import 'package:image_picker/image_picker.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  File _sourceFile;
  File _destinationFile;

  var _scaleMode = ScaleMode.fitKeepAspectRatio;

  var _destinationSize = Size(240, 160);

  ImageProperties _imageProperties;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final sourceFileSize =
        _sourceFile?.existsSync() == true ? _sourceFile?.lengthSync() ?? 0 : 0;

    final destinationFileSize = _destinationFile?.existsSync() == true
        ? _destinationFile?.lengthSync() ?? 0
        : 0;

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('flutter_image_utilities example app'),
        ),
        body: ListView(
          children: <Widget>[
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                RaisedButton(
                  child: Text("Pick image"),
                  onPressed: () async {
                    await _pickImage();
                    await _compressImage();
                  },
                ),
                Text(_sourceFile?.path ?? "No image"),
                Text(
                    "${sourceFileSize} bytes = ${(sourceFileSize / 1024.0).toStringAsFixed(1)} kB"),
                Text(
                    "width=${_imageProperties?.width}, height=${_imageProperties?.height}, orientation=${_imageProperties?.orientation}"),
                DropdownButton<ScaleMode>(
                  value: _scaleMode,
                  onChanged: (value) {
                    _scaleMode = value;
                    _compressImage();
                  },
                  items: ScaleMode.values
                      .map<DropdownMenuItem<ScaleMode>>((ScaleMode value) {
                    return DropdownMenuItem<ScaleMode>(
                      value: value,
                      child: Text(scaleModeToString(value)),
                    );
                  }).toList(),
                ),
                Center(
                  child: Stack(
                    alignment: Alignment.center,
                    children: <Widget>[
                      if (_destinationFile != null)
                        Image.file(
                          _destinationFile,
                        ),
                      Container(
                        width: _destinationSize.width,
                        height: _destinationSize.height,
                        decoration: BoxDecoration(
                          border: Border.all(color: Colors.red, width: 3),
                        ),
                      ),
                      if (_scaleMode ==
                              ScaleMode.fillAnyDirectionKeepAspectRatio ||
                          _scaleMode ==
                              ScaleMode.fitAnyDirectionKeepAspectRatio)
                        Container(
                          width: _destinationSize.height,
                          height: _destinationSize.width,
                          decoration: BoxDecoration(
                            border: Border.all(color: Colors.red, width: 3),
                          ),
                        ),
                    ],
                  ),
                ),
                Text(_destinationFile?.path ?? "N/A"),
                Text(
                    "${destinationFileSize} bytes = ${(destinationFileSize / 1024.0).toStringAsFixed(1)} kB"),
                Text(
                    "Width: ${_destinationSize.width}, height= ${_destinationSize.height}"),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Future _pickImage() async {
    var image = await ImagePicker.pickImage(source: ImageSource.gallery);

    _imageProperties = await FlutterImageUtilities.getImageProperties(image);

    setState(() {
      _sourceFile = image;
    });
  }

  Future _compressImage() async {
    if (_sourceFile == null) {
      return;
    }
    if (_destinationFile?.existsSync() == true) {
      _destinationFile.deleteSync();
    }

    final tempDir = Directory.systemTemp;
    final tempFilePath = tempDir.path +
        "/image" +
        DateTime.now().millisecondsSinceEpoch.toString() +
        ".jpg";

    var image = await FlutterImageUtilities.saveAsJpeg(
        sourceFile: _sourceFile,
        destinationFilePath: tempFilePath,
        quality: 60,
        maxWidth: _destinationSize.width.round(),
        maxHeight: _destinationSize.height.round(),
        scaleMode: _scaleMode);

    imageCache.clear();
    setState(() {
      _destinationFile = null;
    });
    setState(() {
      _destinationFile = image;
    });
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  flutter_image_utilities: ^0.0.4

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_utilities/flutter_image_utilities.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
48
Learn more about scoring.

We analyzed this package on Mar 27, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test