Coder Social home page Coder Social logo

flutter_native_image's Introduction

flutter_native_image

Native Flutter Image tools

This plugin aims to have native tools to resize images and reduce their quality by compression. The code is somewhat hacky (especially the iOS part), but it works for my needs and hasn't crashed on me. Feel free to improve it if you want to.

Right now there is only a compression function available.

Usage

Install

Add the following lines to your pubspec.yaml under dependencies

flutter_native_image:
  git: https://github.com/btastic/flutter_native_image.git

Compress an image

File compressedFile = await FlutterNativeImage.compressImage(file.path,
    quality: quality, percentage: percentage);

You have to give it a file from the file system and optionally provide a quality (1-100) and a resizing percentage (1-100). Each platform will use it's proper tools to handle the resizing.

To resize the image to the certain size, use following code:

ImageProperties properties = await FlutterNativeImage.getImageProperties(file.path);
File compressedFile = await FlutterNativeImage.compressImage(file.path, quality: 80, 
    targetWidth: 600, targetHeight: 300);

Keep aspect ratio of the file:

ImageProperties properties = await FlutterNativeImage.getImageProperties(file.path);
File compressedFile = await FlutterNativeImage.compressImage(file.path, quality: 80, 
    targetWidth: 600, 
    targetHeight: (properties.height * 600 / properties.width).round());

Get image properties

ImageProperties properties = await FlutterNativeImage.getImageProperties(file.path);

It returns an ImageProperties object containing the width and the height of the image.

Crop an image

File croppedFile = await FlutterNativeImage.cropImage(file.path, originX, originY, width, height);

Returns a file containing the image cropped with the given dimensions.

Contributions

Alexis Leblond (a-leblond) the image properties feature.

Eugene Strokin the resize to target height/width feature

Credits

Shoutouts to Trevor from Vocaro.com. He had the fitting algorithm for resizing images in Objective-C.

Source: http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

For preserving exif information, I took the code from googles image_picker github (https://github.com/flutter/plugins/tree/master/packages/image_picker)

flutter_native_image's People

Contributors

btastic avatar a-leblond avatar strokine avatar lschmierer avatar stevenspiel avatar acidjazz avatar aryzhov avatar ytakzk avatar yunyu avatar alphayiuhk avatar mazzonem avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.