Photo Filters In Flutter

Introduction

 
In this article, we will learn how to implement photo filter feature in Flutter application. We will implement an example in which we will, first, capture an image using a camera plugin then apply filters on the captured image and show the ultimate output. There are around 33 filters in the plugin. You can download the source code and check it out.  So let’s start implementing it.
 
Output
 
 
 
Plugin Required
 
camera: ^0.5.4+1
photofilters: ^1.0.5
 

Steps

 
Step 1
 
The first and most basic step is to create a new application in Flutter. If you are a beginner in flutter then you can check my blog Create a first app in Flutter. I have created an app named as “flutter_photo_filter”.
 
Step 2
 
Now, we will configure plugins for photo capture and photo filter in pubspec.yaml file.
  1. dependencies:  
  2.  flutter:  
  3.    sdk: flutter  
  4.  camera: ^0.5.4+1  
  5.  photofilters: ^1.0.5  
Step 3
 
Now, we will implement a logic to capture images using a camera plugin. I have already implemented an app for this one of our previous articles. Take a look at Flutter Camera Overlay. We will start from that app and modify the app to filter the captured image.
 
Step 4
 
In the main.dart file, we will implement a method which will navigate to filter page when a user captures the image using a camera. Following is the implementation of that method.
  1. Future openCamera() async {  
  2.    availableCameras().then((cameras) async {  
  3.      final imagePath = await Navigator.push(  
  4.        context,  
  5.        MaterialPageRoute(  
  6.          builder: (context) => CameraPage(cameras),  
  7.        ),  
  8.      );  
  9.      setState(() {  
  10.        _imagePath = imagePath;  
  11.      });  
  12.      var imageFile = File(imagePath);  
  13.      fileName = path.basename(imageFile.path);  
  14.      var image = imageLib.decodeImage(imageFile.readAsBytesSync());  
  15.      image = imageLib.copyResize(image, width: 600);  
  16.      Map imagefile = await Navigator.push(  
  17.        context,  
  18.        new MaterialPageRoute(  
  19.          builder: (context) => new PhotoFilterSelector(  
  20.            title: Text("Photo Filter Example"),  
  21.            image: image,  
  22.            filters: presetFiltersList,  
  23.            filename: fileName,  
  24.            loader: Center(child: CircularProgressIndicator()),  
  25.            fit: BoxFit.contain,  
  26.          ),  
  27.        ),  
  28.      );  
  29.      if (imagefile != null && imagefile.containsKey('image_filtered')) {  
  30.        setState(() {  
  31.          imageFile = imagefile['image_filtered'];  
  32.          _imagePath = imageFile.path;  
  33.        });  
  34.      }  
  35.    });  
  36.  }  
Hooray…. Run the app and test it on an emulator/simulator or device :)))
 
Download the full source code which is attached for a full working example. You can also refer to my Git repository.
 

Conclusion

 
We have learned how to capture images and apply a filter on it in Flutter application using photo filter plugin.


Similar Articles