flutter_image_utilities 1.0.0 copy "flutter_image_utilities: ^1.0.0" to clipboard
flutter_image_utilities: ^1.0.0 copied to clipboard

PlatformAndroidiOS
outdated

Utility methods for saving an image as JPEG with the specified quality, size and scale mode and for getting image properties.

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:async';
import 'dart:io';

import 'package:flutter/material.dart';
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;

  ScaleMode _scaleMode = ScaleMode.fitKeepAspectRatio;

  final _destinationSize = const 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>[
                ElevatedButton(
                  onPressed: () async {
                    await _pickImage();
                    await _compressImage();
                  },
                  child: const Text("Pick image"),
                ),
                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) {
                    if (value != null) {
                      _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 {
    final pickedFile =
        await ImagePicker().getImage(source: ImageSource.gallery);
    if (pickedFile != null) {
      final imageFile = File(pickedFile.path);

      _imageProperties =
          await FlutterImageUtilities.getImageProperties(imageFile);

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

  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}.jpg";

    final 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;
    });
  }
}
14
likes
160
points
319
downloads

Publisher

verified publisherkineapps.com

Weekly Downloads

Utility methods for saving an image as JPEG with the specified quality, size and scale mode and for getting image properties.

Repository (GitHub)

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

flutter

More

Packages that depend on flutter_image_utilities