Image filtering in android

Manipulating images and Drawables with Android’s ColorFilter

Tinting, custom effects and reusability for visual Android app resources

Image and Drawable resources are an integral part of any Android app. Since day (i.e. API level) 1, the Android framework has provided a means of manipulating the colors of these assets on a per-pixel level, as they are drawn to screen, with the ColorFilter class.

The documentation for the base abstract class stipulates that “a color filter can be used with a Paint to modify the color of each pixel drawn with that paint”. This is a powerful tool that can be used for:

  • Applying effects and adjustments to images
  • Tinting of icons and logos

Ultimately this encourages the good practice of reusing resources for scenarios that require different color variations, resulting in reduced app size.

Note: ColorFilter is an abstract class that should never be used directly, and the constructor was deprecated in API level 26. Only the subclasses should be used, which we will discuss further down.

Where can this be used? 🤔

Before we explore the existing subclasses and their capabilities, we need to discuss where we can actually use ColorFilter .

Canvas

As hinted at in the documentation description, we can use ColorFilter when drawing to Canvas in a custom View . Specifically, a ColorFilter is set on a Paint which then affects everything that is drawn with it:

Drawable

A ColorFilter can be applied to a Drawable , which will affect how it is rendered in View s and layouts:

In addition to this, there exists a Drawable convenience function to apply a PorterDuffColorFilter (more on that later):

Lastly, it is important to note that a tint can be applied to a Drawable . This is separate and will be overridden if a ColorFilter is set via either one of the above functions.

ImageView

A ColorFilter can be applied to an ImageView , which will affect how its current image will be rendered:

As with Drawable , convenience functions exist for applying a PorterDuffColorFilter :

Introducing our sample image 🖼️

We are now going to dive into the three subclasses of ColorFilter ( PorterDuffColorFilter , LightingColorFilter and ColorMatrixColorFilter ). In order to demonstrate this, we will make use of a visual aid in the form of a sample image:

It has photographic detail while also having “shape” as a result of the transparent areas. This will allow all of the subclasses to be demonstrated.

PorterDuffColorFilter 1️⃣

We have already touched on this briefly. This is a color filter that accepts a single color that is applied to all source pixels along with a Porter-Duff composite mode. There are many modes that are suitable to different scenarios. Typically, this is used to apply a “blanket” color (eg. To tint an icon).

LightingColorFilter 2️⃣

This color filter can be used to simulate lighting effects on an image. The constructor accepts two parameters, the first to multiply the source color ( colorMultiply ) and the second to add to the source color ( colorAdd ).

Читайте также:  Аналоги acdsee для андроид

While the source color alpha channel is not affected, the R, G and B channels are computed like so:

Note: The above is using Android KTX Color extension functions for accessing the red, blue and green channels (alpha is also available).

ColorMatrixColorFilter 3️⃣

This is arguably the most flexible (but also the most complex) color filter.

It is quite similar to LightingColorFilter , in that we can tweak each pixel’s color channels using values that are multiplicative and additive. However, a ColorMatrixColorFilter is constructed with a ColorMatrix , which is essentially a wrapper class for a 4×5 matrix. This gives us 20 values, used in a certain way, that allow us to transform colors using all of the existing channels, including alpha.

Before we dive into using matrices, let’s first take a look at some of the convenience functions offered by ColorMatrix :

Fear not if some of these color concepts do not make sense! The point here is that ColorMatrix offers a lot of flexibility, and these convenience functions are simply matrix operations under the hood.

We know that ColorMatrix wraps a 4×5 matrix. Given this matrix, how do we arrive at our resultant color channels? They are computed like so:

The 4 rows in fact represent the resultant R, G, B and A channels. For each channel, the 5 columns allow you to combine the existing R, G, B, and A channels and a wildcard value in a plethora of ways.

This provides us with a lot of possibilities. We can adjust the brightness/contrast of images, ignore some channels, invert an image or even create basic filters and effects.

Limitations and other options 🛠️

While ColorFilter is powerful and flexible, image manipulation is a vast field and it simply cannot cover everything.

For example, the current pixel value that is being passed through the filter would be handy to know, but is not available. Additionally, you are limited to the three subclasses that ColorFilter currently provides. It appears as if you cannot create a custom subclass, due to native code restrictions.

In instances like this, what other options do we have?

The graphics framework has other useful classes such as Shader and MaskFilter . We could turn to RenderScript , which offers Bitmap utilities that still keep us mostly within traditional Android graphics territory. There is also OpenGL, which is perhaps the most extreme power vs. complexity tradeoff, but opens up all of the possibilities of custom GLSL shaders.

Overall, ColorFilter is still a fantastic tool for working with app resources.

I hope this post has provided some insight into ColorFilter and the flexibility it provides when working with images and Drawable s. If you have any questions, thoughts or suggestions then I’d love to hear from you!

Источник

How to Build Image Filters like Instagram in Android?

Now a day many social media apps provide so many filters that we can use to make our image inside the app more beautiful and attractive. This type of feature is generally seen in Instagram, Snapchat, and many socials media apps. In this article, we will take a look at the implementation of this Instagram-like filter section in Android.

What we are going to build in this article?

We will be building a simple application in which we will be displaying an ImageView and we will provide different types of image filtering options below. After clicking on each image filter that filter will be applied to our original image. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.

Читайте также:  Не создается андроид проект

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio . Note that select Java as the programming language.

Step 2: Add dependency and JitPack Repository

Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.

Add the JitPack repository to your build file. Add it to your root build.gradle at the end of repositories inside the allprojects < >section.

After adding this dependency sync your project and now we will move towards its implementation.

Источник

Image filtering in android

Android Photo Filters

This library is forked from here. All credits goes to respective owners.

An example article demonstrating Instagram Like Interface can be found here

How to use the Library

  1. Include the Filters library in build.gradle
  1. Load the native library in your activity
  1. Get the pre defined Filter Pack to generate the thumbnails. Refer FilterPack.java to generate your own filters.
  1. Apply desired filter on to bitmap image.

The Filter Pack

Currently the below filters are available in filer pack. More will be added in future.

Struck Clarendon OldMan
Mars Rise April
Amazon Starlit Whisper
Lime Haan Bluemess
Adele Cruz Metropolis
Audrey

(This is the older documentation from original page)

PhotoFiltersSDK processes filter on any Image within fraction of second since processing logic is in NDK. At present following image filters are included:

  • ToneCurveSubfilter : With this powerful filter you can change RGB channels of any image to create great results.
  • SaturationSubfitler : Used for changing color saturation of an image.
  • ColorOverlaySubfilter : As name suggests you can overlay any image with color of your choice.
  • ContrastSubfilter : Used for changing contrast value of image.
  • BrightnessSubfilter : To change brightness levels.
  • VignetteSubfilter : To apply vignette effect on image.

Above code snippet will give you outputImage with increased brightness and contrast. You can further refer example project.

Although there are few inbuilt filters already present, you may want to create and customize one specific to your need and show your creativity. For that you would require to know how all the Subfilters can be used. Let me take you through all of them.

This is most awesome filter present in this library which differentiates PhotoFiltersSDK from other image processing libraries out there. ToneCurveSubFilter applies the changed RGB Channel curve to create effect on image.

Here is the code snippet the apply the above RGB curve on an image :

The results are nearly same as we would see in photoshop and other tools. We can also specify knots for Red, Green and Blue channels (in the ToneCurveSubfilter’s constructor).

This fitler can be used to tweak color saturation of an image. Here is the example :

SaturationSubfilter takes float as an argument and has no effect for value 1.

Increases the specified red, green and blue values for each pixel in an image.

To change the contrast levels of an image use this filter :

ContrastSubfilter takes float as an argument where value 1 has no effect on the image.

As the name suggest, this filter is used for changing brightness levels :

BrightnessSubfilter takes int as an argument where value 0 has no effect. Negative values can be used to decrease brightness of the image.

Читайте также:  Android start app with adb

This filter can be used to put vignette effect on the image.

VignetteSubfilter takes int as an argument whoes value ranges from 0-255, which defines intesity of the vignette effect.

Источник

Android Image Filters Algorithm

May 13, 2018 · 5 min read

Image filtering allows you to apply various effects on photos. Since digital image processing has very wide applications and almost all of the technical fields are impacted by DIP. Digital Image processing is not just limited to adjust the spatial resolution of the everyday images captured by the camera. It is not just limited to increase the brightness of the photo, e.t.c. Rather it is far more than that.

The type of image filtering described here uses a 2D filter similar to the one included in Paint Shop Pro as User Defined Filter and in Photoshop as Custom Filter.

I m age is divided into pixels or say 2D Array(Matrix) or can be defined as the mathematical function f(x,y) where x and y are the two co-ordinates horizontally and vertically.So, to understand this post you must know concept of Matrices programs. Suppose a image as below:

Image is of resolution 300×180 then each pixel you can treat as cell in 2D array of Rows-300 and Columns-180creating a mesh and each pixel(cell) is consist of 3 colors, called primary colors:

Now are filters are actually made due to new manipulated values of each pixel and then replaced the old pixel values with the new values, Each filter has its own Algorithm or Formula to apply filters. Here we are going to discuss few basic filters and rest you can get from Github project.

Algorithm for pixel manipulation

Now, the basic code common for all is to get original pixel from image as Bitmap apply some manipulation in values of (R,G,B) on that pixel and set new values to it. In context with Android:

So above code is used to get pixel values and set new pixel values, so the main thing to add in between is the manipulation, which varies with filters. Now some filter algorithm are fixed but you can apply your own algorithm i.e just make any random changes or apply condition with it to set new pixel values.Here are some filters

GREY Filter:

Here we have calculated the INTENSITY of each pixel thru formula and set this value as in (R,G,B)

SEPIA Filter:

Here we have some constants to applies and calculated as below of each pixel set this value as in (R,G,B)

My Customized Filter- Sepia Diagonal:

Here we have some concept as above in Sepia but there is minute change i.e I have put condition that if its below diagonal of matrix and above diagonal matrix only then apply SEPIA values to pixel else set the original values.

Sketch Filter:

Here we have some random SCALE FACTOR=100 and checked if INTENSITY > SCALE FACTOR then apply color WHITE(255,255,255) else apply color BLACK(0,0,0) as below of each pixel set this value as in (R,G,B)

Hence, with the help of above code you can make your own filters and who knows that filter might get trending in Social network sites.I will add some more content to it like Filter for Blur,Cartoon etc.

For more understanding visit Youtube Tutorial and FORK Github Project

Источник

Оцените статью