1

I have seen so many links and posts of to adjust birightness of bitmap in android, but i want to adjust brightness with gradient, like adjust brightness of only center of the bitmap. Anybody have any idea how to do this ?

mvts himm
  • 117
  • 1
  • 4
  • 16

1 Answers1

0

You can generate a bitmap and draw a gradient:

How to draw a smooth/dithered gradient on a canvas in Android

Then use each pixel from gradient bitmap and get the brightness and pass it to the function that adjust brightness.

look here Formula to determine brightness of RGB color

public Bitmap SetBrightness(Bitmap src, Bitmap gradient) {
    // original image size
    int width = src.getWidth();
    int height = src.getHeight();
    // create output bitmap
    Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
    // color information
    int A, R, G, B;
    int pixel;

    // 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);
            R = Color.red(pixel);
            G = Color.green(pixel);
            B = Color.blue(pixel);

            int gradientPixel = gradient.getPixel(x, y);
            int value = getLuminance(gradientPixel);                

            // increase/decrease each channel
            R += value;
            if (R > 255) {
                R = 255;
            }
            else if (R < 0) {
                R = 0;
            }

            G += value;
            if (G > 255) {
                G = 255;
            }
            else if (G < 0) {
                G = 0;
            }

            B += value;
            if (B > 255) {
                B = 255;
            }
            else if (B < 0) {
                B = 0;
            }

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

    // return final image
    return bmOut;
}

public int getLuminance(int pixel) {
    int A, R, G, B;
    A = Color.alpha(pixel);
    R = Color.red(pixel);
    G = Color.green(pixel);
    B = Color.blue(pixel);

    return (0.2126 * R) + (0.7152 * G) + (0.0722 * B);
}
Community
  • 1
  • 1
Seraphim's
  • 12,559
  • 20
  • 88
  • 129
  • thanx bro for help ....but i have already do this thing ...actually my question is how to adjust brightness in gradient form..... ?For example brightness should be adjust from only center of the screen ...hope you got what i want to do..... – mvts himm Oct 29 '13 at 11:06
  • I updated my answer that use a bitmap as gradient brigtness. If it does not fit your need I'll delete my answer. – Seraphim's Oct 29 '13 at 11:08
  • still not change any thing...bro – mvts himm Oct 29 '13 at 11:26