Image filters in android

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

Источник

Image filters in android

some android image filters

in some filter, I use NDK to implement to make it more efficient

  • Install Android NDK and properly configure it: http://goo.gl/koTCb
  • Get a clean clone of this project, import the library in Android Studio
  • then Clean and Build the hold project to regenerate the library
  • Then just add library module as a dependency to your existing project.

It is dead simple, you can see magic in the following code:

and there are some options for the filter, you can go to see the demo to see how to use this options to customize your filter effect

You can see all filters in file BitmapFilter.java, currently contains totally 19 kinds of filters now, here is the list of filters and their options(show by code):

maskSize is a integer, to indicate the average blur mask’s size

neon_color_R , neon_color_G , neon_color_B are the R,G,B component are integer

pixelSize is a integer, the pixel size for this filter

light_center_x , light_center_y are integer, indicate the center of the light spot, the origin in the left-upper side, and the light_radius is indicate the radius of light spot, in pixel

roundRadius is a double, the black round’s radius in the effect

sigma is a double, the sigma value in Gaussian Blur, the bigger of sigma, the smoother in the result image

sigma is a double, the same as sigma in Gaussian Blur, indicate the sigma value in the process of Gaussian Blur for Soft Glow

xSpeed and ySpeed are both integer, indicate the speed in x-axis and y-axis, the origin in the left-upper side

PS: all options have defalut values, so you can just select the effect and pass nothing, like this:

If you have any question, please open an issue and show your code and the program ouput, thanks!

The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the «Software»), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED «AS IS», WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Источник

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 ).

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 :

Читайте также:  Viper fx аналоги андроид

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.

Источник

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