Android Bitmap Color Change adjustedContrast(Bitmap src, double value)

Here you can find the source of adjustedContrast(Bitmap src, double value)

Description

Adjust contrast of bitmap.

Parameter

Parameter Description
src the src
value the value

Return

the bitmap

Declaration

public static Bitmap adjustedContrast(Bitmap src, double value) 

Method Source Code

//package com.java2s;

import android.graphics.Bitmap;

import android.graphics.Canvas;
import android.graphics.Color;

import android.graphics.Paint;

public class Main {
    /**/*  ww w. ja v a 2s. com*/
     * Adjust contrast of bitmap.
     * 
     * @param src
     *            the src
     * @param value
     *            the value
     * @return the bitmap
     * @author siddhesh
     */
    public static Bitmap adjustedContrast(Bitmap src, double value) {
        // image size
        int width = src.getWidth();
        int height = src.getHeight();
        // create output bitmap

        // create a mutable empty bitmap
        Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());

        // create a canvas so that we can draw the bmOut Bitmap from source
        // bitmap
        Canvas c = new Canvas();
        c.setBitmap(bmOut);

        // draw bitmap to bmOut from src bitmap so we can modify it
        c.drawBitmap(src, 0, 0, new Paint(Color.BLACK));

        // color information
        int A, R, G, B;
        int pixel;
        // get contrast value
        double contrast = Math.pow((100 + value) / 100, 2);

        // scan through all pixels
        for (int x = 0; x < width; ++x) {
            for (int y = 0; y < height; ++y) {
                // get pixel color
                pixel = src.getPixel(x, y);
                A = Color.alpha(pixel);
                // apply filter contrast for every channel R, G, B
                R = Color.red(pixel);
                R = (int) (((((R / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
                if (R < 0) {
                    R = 0;
                } else if (R > 255) {
                    R = 255;
                }

                G = Color.green(pixel);
                G = (int) (((((G / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
                if (G < 0) {
                    G = 0;
                } else if (G > 255) {
                    G = 255;
                }

                B = Color.blue(pixel);
                B = (int) (((((B / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
                if (B < 0) {
                    B = 0;
                } else if (B > 255) {
                    B = 255;
                }

                // set new pixel color to output bitmap
                bmOut.setPixel(x, y, Color.argb(A, R, G, B));
            }
        }
        return bmOut;
    }
}

Related

  1. boost(Bitmap src)
  2. brightness(Bitmap src, int value)
  3. changeHue(Bitmap bmp, int hue, int width, int height)
  4. createAnaglyphBitmap(Bitmap bitmap)