Altering Contrast and Brightness

Adjusting the brightness and contrast of an image can be done by increasing or decreasing the color values.

This code will double the intensity of each color channel, which affects both the brightness and contrast in an image as illustrated in Figure 3-15

ColorMatrix cm = new ColorMatrix(); float contrast = 2; cm.set(new float[] {

contrast, 0, 0, 0, 0, 0, contrast, 0, 0, 0, 0, 0, contrast, 0, 0, 0, 0, 0, 1, 0 }); paint.setColorFilter(new ColorMatrixColorFilter(cm));

[sIHD Affile 12:06PM

[sIHD Affile 12:06PM

Figure 3-15. ColorMatrix with each color's intensity doubled, increasing brightness and contrast

In this example, both effects are linked. If we simply want to increase contrast without increasing brightness, we actually have to reduce brightness to compensate for the increase in color intensity.

Generally, when adjusting brightness, it is easier to just use the final column in the matrix for each color. This is the amount that is simply added to the value of the color without any multiplication of the existing color values.

Therefore, to reduce the brightness, we would use code for a matrix as follows.

ColorMatrix cm = new ColorMatrix(); float brightness = -25; cm.set(new float[] {

1, 0, 0, 0, brightness, 0, 1, 0, 0, brightness, 0, 0, 1, 0, brightness, 0, 0, 0, 1, 0 }); paint.setColorFilter(new ColorMatrixColorFilter(cm));

Putting these two transformations together would yield the following.

ColorMatrix cm = new ColorMatrix();

float contrast = 2; float brightness = -25; cm.set(new float[] {

contrast, 0, 0, 0, brightness,

0, contrast, 0, 0, brightness, 0, 0, contrast, 0, brightness, 0, 0, 0, contrast, 0 }); paint.setColorFilter(new ColorMatrixColorFilter(cm));

The result of this operation is shown in Figure 3-16.

Figure 3-16. ColorMatrix with each color's intensity doubled but brightness decreased to affect contrast without changing brightness

Fortunately, we don't need to know the formula for each operation we might want to accomplish. For instance, the ColorMatrix has a method for changing saturation built in.

ColorMatrix cm = new ColorMatrix(); cm.setSaturation(.5f);

paint.setColorFilter(new ColorMatrixColorFilter(cm));

Passing in a number greater than 1 will increase saturation, and a number between 0 and 1 decreases saturation. A value of 0 will yield a grayscale image.

(3HO

Www 3ho Org

Was this article helpful?

+1 -3

Responses

  • Veli
    How to increase brightness of image in android with code?
    8 years ago
  • Rikki
    How to increase contrast on android?
    8 years ago
  • corey
    How do I adjust brightness and contrast android images?
    8 years ago
  • Stefan
    How to increase contrast of image in android?
    7 years ago
  • sue
    How to change brightness of image in android?
    7 years ago
  • Eetu
    How to increase or decrease image contrast in android?
    7 years ago
  • gundolpho
    How to decrise contrast on android?
    7 years ago

Post a comment