image_native_resizer 0.1.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 84

image_native_resizer #

pub package

A Flutter plugin that resizes images from native API, while keeping important EXIF attributes.

Installation #

First, add image_native_resizer as a dependency in your pubspec.yaml file.

Quickstart #

import 'package:image_native_resizer/image_native_resizer.dart';

final localImagePath = /*** ***/;
final resizedPath = await ImageNativeResizer.resize(
    imagePath: localImagePath,
    maxWidth: 512,
    maxHeight: 512,
    quality: 50,
);

Inspired by image_picker #

This plugin is almost identitical to the official image_picker plugin. Serveral internal files from the plugin are used to implement the resizing logic, this plugin is just exposing them through a platform channel.

0.1.2 #

  • Add bitmap recycling.
  • Add a lock for ensuring that only one resizing is running at a time.

0.1.1 #

  • Add documentation.
  • Add null checks.

0.1.0 #

  • Initial release.

example/lib/main.dart

import 'dart:io';

import 'package:exif/exif.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:http/http.dart' as http;
import 'package:path/path.dart' as path;

import 'package:flutter/services.dart';
import 'package:image_native_resizer/image_native_resizer.dart';
import 'package:path_provider/path_provider.dart';

void main() {
  runApp(MyApp());
}

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

class _MyAppState extends State<MyApp> {
  String _status = 'Not started';
  File _sampleFile;
  List<String> resized = [];
  List<String> exif = [];

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

  Future<void> downloadSampleImageIfNeeded() async {
    final localDirectory = await getApplicationDocumentsDirectory();
    _sampleFile = File(path.join(localDirectory.path, 'sample.jpeg'));
    if (!await _sampleFile.exists()) {
      final client = http.Client();
      final bytes = await client.readBytes(
          'https://github.com/ianare/exif-samples/raw/master/jpg/gps/DSCN0029.jpg');
      await _sampleFile.writeAsBytes(bytes);
    }

    Map<String, IfdTag> data =
        await readExifFromBytes(await _sampleFile.readAsBytes());
    exif.add(data.entries.map((x) => "${x.key} = ${x.value}").join(' | '));
  }

  Future<void> resize({double maxWidth, double maxHeight, int quality}) async {
    var resizedPath = await ImageNativeResizer.resize(
      imagePath: _sampleFile.path,
      maxWidth: maxWidth,
      maxHeight: maxHeight,
      quality: quality,
    );
    final copyPath = path.join(path.dirname(resizedPath),
        '${resized.length}${path.extension(resizedPath)}');
    await File(resizedPath).copy(copyPath);
    resized.add(copyPath);
    Map<String, IfdTag> data =
        await readExifFromBytes(await File(copyPath).readAsBytes());
    exif.add(data.entries.map((x) => "${x.key} = ${x.value}").join(' | '));
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    await downloadSampleImageIfNeeded();
    if (!mounted) return;
    this.setState(() => _status = 'Sample file downloaded');
    print('_sampleFile: ${_sampleFile?.path}');

    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      await resize(maxWidth: 312);
      if (!mounted) return;
      this.setState(() => _status = 'smallWidth loaded');

      await resize(maxHeight: 312);
      if (!mounted) return;
      this.setState(() => _status = 'smallHeight loaded');

      await resize(quality: 2);
      if (!mounted) return;
      this.setState(() => _status = 'All loaded');
    } on PlatformException {
      _status = 'Failed to call platform plugin.';
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: ListView(
            children: [
              Text('Running on: $_status\n'),
              if (_sampleFile != null) ...[
                Text(exif[0]),
                Image.file(
                  File(_sampleFile.path),
                ),
              ],
              if (resized.isNotEmpty)
                ...List.generate(
                  resized.length,
                  (x) => <Widget>[
                    SizedBox(
                      height: 24,
                    ),
                    Text(exif[x + 1]),
                    Image.file(
                      File(resized[x]),
                    ),
                  ],
                ).expand((e) => e),
            ],
          ),
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  image_native_resizer: ^0.1.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:image_native_resizer/image_native_resizer.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
68
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]
84
Learn more about scoring.

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

  • Dart: 2.8.4
  • pana: 0.13.15
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform Linux

Because:

  • package:image_native_resizer/image_native_resizer.dart that declares support for platforms: Android, iOS

Package does not support Flutter platform Web

Because:

  • package:image_native_resizer/image_native_resizer.dart that declares support for platforms: Android, iOS

Package does not support Flutter platform Windows

Because:

  • package:image_native_resizer/image_native_resizer.dart that declares support for platforms: Android, iOS

Package does not support Flutter platform macOS

Because:

  • package:image_native_resizer/image_native_resizer.dart that declares support for platforms: Android, iOS

Package not compatible with SDK dart

Because:

  • image_native_resizer that is a package requiring null.

Dependencies

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