Search code examples
androidoptimizationandroid-imageview

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

Solution

  • I have got a work around for this issue i made the image brighter and then shown it in an imageview. The code i used is given below:

    foto.setColorFilter(brightIt(100));//foto is my ImageView
    //and below is the brightIt func
    public static ColorMatrixColorFilter brightIt(int fb) {
    ColorMatrix cmB = new ColorMatrix();
    cmB.set(new float[] { 
        1, 0, 0, 0, fb,
        0, 1, 0, 0, fb,
        0, 0, 1, 0, fb,
        0, 0, 0, 1, 0   });
    
    ColorMatrix colorMatrix = new ColorMatrix();
    colorMatrix.set(cmB);
    //Canvas c = new Canvas(b2);
    //Paint paint = new Paint();
    ColorMatrixColorFilter f = new ColorMatrixColorFilter(colorMatrix);
    //paint.setColorFilter(f);   
    return f;
    }