Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enhancing ImageView brightness programmatically

I have an android app in which I am increasing brightness of image with the code below. But this is very slow so does anyone knows a fast way to enhance image brightness of an imageview in android. Keep in mind this is improving imageview brightness not screen brightness

 public static Bitmap doBrightness(Bitmap src, int value) {
    //Log.e("Brightness", "Changing brightnhjh");

    int width = src.getWidth();
    int height = src.getHeight();
    Bitmap bmout = Bitmap.createBitmap(width, height, src.getConfig());
    int A, R, G, B;
    int pixel;
    for (int i = 0; i < width; i=i++) {
        for (int j = 0; j < height; j=j++) {
            pixel = src.getPixel(i, j);
            A = Color.alpha(pixel);
            R = Color.red(pixel);
            G = Color.green(pixel);
            B = Color.blue(pixel);
            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;
            }
            bmout.setPixel(i, j, Color.argb(A, R, G, B));
        }
    }
    return bmout;

}

this is the imageview

imageview.setImageBitmap(doBrightness(image, 40));
like image 325
Syed Wajahat Ali Avatar asked Dec 02 '22 18:12

Syed Wajahat Ali


1 Answers

You can use the following code to enhance the image :

public static Bitmap enhanceImage(Bitmap mBitmap, float contrast, float brightness) {
    ColorMatrix cm = new ColorMatrix(new float[]
        {
            contrast, 0, 0, 0, brightness,
            0, contrast, 0, 0, brightness,
            0, 0, contrast, 0, brightness,
            0, 0, 0, 1, 0
        });
    Bitmap mEnhancedBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), mBitmap
        .getConfig());
    Canvas canvas = new Canvas(mEnhancedBitmap);
    Paint paint = new Paint();
    paint.setColorFilter(new ColorMatrixColorFilter(cm));
    canvas.drawBitmap(mBitmap, 0, 0, paint);
    return mEnhancedBitmap;
}

note :
contrast : 0 to 10 brightness : -255 to 255

like image 130
Jaspinder Kaur Avatar answered Dec 05 '22 08:12

Jaspinder Kaur