Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 84
Registered: ‎07-17-2008
My Device: Not Specified

Change Contrast and Brightness of IMAGE programatically

[ Edited ]

Hello All,

 

I am developing  an application in BlackBerry in which I am showing an image on the screen, I want to provide some control(field) to change the brightness and contrast of underlying image.

 

Is it possible to change brightness and contrast of any Image/Bitmap/EncodedImage programatically?. If anyone have any idea about the same then please share it with me.

 

Thanks and appreciate your response,

Hemant

Message Edited by hemant on 09-02-2009 03:30 PM
o(-_-)o
Developer
Posts: 84
Registered: ‎07-17-2008
My Device: Not Specified

Re: Change Contrast and Brightness of IMAGE programatically

Is anybody having any idea, whether it is possible to change Contrast/brightness of image on screen?.

 

Thanks,

Hemant

o(-_-)o
Developer
Posts: 266
Registered: ‎04-24-2009
My Device: Not Specified

Re: Change Contrast and Brightness of IMAGE programatically

Did you ever figure this one out?

Developer
Posts: 132
Registered: ‎11-20-2009
My Device: Not Specified

Re: Change Contrast and Brightness of IMAGE programatically

Im wondering this as well. Anyone???

Developer
Posts: 112
Registered: ‎09-23-2009
My Device: Not Specified

Re: Change Contrast and Brightness of IMAGE programatically

Hi,

I've done this before. It's pretty easy tough.

Here is the function:

 

 

public static Bitmap makeContrastBitmap(Bitmap original, int contrast) {
		Bitmap greyScale = new Bitmap(original.getWidth(), original.getHeight());

		int[] argb = new int[original.getWidth() * original.getHeight()];

		original.getARGB(argb, 0, original.getWidth(), 0, 0, original
				.getWidth(), original.getHeight());
		for (int i = argb.length - 1; i >= 0; --i) {
			int alpha = argb[i] >> 24;
			int red = (argb[i] >> 16) & 0xFF;
			int green = (argb[i] >> 8) & 0xFF;
			int blue = argb[i] & 0xFF;
			
			int grey = (int) (0.3 * red + 0.59 * green + 0.11 * blue);
			
			int red2 = red * contrast/10;
			if (red2>0xFF) red2 = 0xFF;
			if (red2<0) red2 = 0;
			int green2 = green * contrast/10;
			if (green2>0xFF) green2 = 0xFF;
			if (green2<0) green2 = 0;
			int blue2 = blue * contrast/10;
			if (blue2>0xFF) blue2 = 0xFF;
			if (blue2<0) blue2 = 0;
			
			int composite = (alpha << 24) | (red2 << 16) | (green2 << 8) | blue;
			argb[i] = composite;
		}

		greyScale.setARGB(argb, 0, original.getWidth(), 0, 0, original
				.getWidth(), original.getHeight());

		return greyScale;
	}

 

 

 

 

 

Developer
Posts: 132
Registered: ‎11-20-2009
My Device: Not Specified

Re: Change Contrast and Brightness of IMAGE programatically

amir thanks a lot for the code sample. What is a good value to use for

 

int contrast to increment the contrast by?  Thanks

Developer
Posts: 112
Registered: ‎09-23-2009
My Device: Not Specified

Re: Change Contrast and Brightness of IMAGE programatically

The contrast value is between 0 and 10. You could increment by 1. If you need to implement more smooth incrementing contrast, you could customize the code. 

 

Just try the code, then you will realize how to customize the code. It's pretty simple.

 

By the way, if you find this usefull, please kudo this. Maybe it's my first kudo. I hope. :smileyhappy: 

Developer
Posts: 132
Registered: ‎11-20-2009
My Device: Not Specified

Re: Change Contrast and Brightness of IMAGE programatically

Sorry I totally forgot about Kudos.. Thank you very much KUDOS to you!!!

Developer
Posts: 266
Registered: ‎04-24-2009
My Device: Not Specified

Re: Change Contrast and Brightness of IMAGE programatically

Brightness/Darkness is just the result of adding or substracting and equivalent values to Red, Green, and Blue.  However, I am experienceing something very odd with my code.  Either that or I made a stupid mistake and cannot seem to catch it.

 

My test picture doesn't brighten properly.  I starting monitoring the  Red, Green, and Blue componenets, the Red and Green appears to change properly but Blue does not change.  It maintains its original value. 

 

I narrowed down the problem to one line of code.

        NewBitmap.setARGB(argb, 0, original.getWidth(), 0, 0, original.getWidth(), original.getHeight());

 

To test the problem watch blue = argb[i] & 0xFF; I picked one particular pixle to watch to keep it simple.


blue gets incremented properly and the argb pixle I watch looks OK for the following setARGB command


        NewBitmap.setARGB(argb, 0, original.getWidth(), 0, 0, original.getWidth(), original.getHeight());

 

But if I add the following statement immediately after the above statement, argb[i] & 0xFF retains its original value and does not increment with Red and Green. I am stumped.


        NewBitmap.getARGB(argb, 0, original.getWidth(), 0, 0, original.getWidth(), original.getHeight());
       

 

Following is the Brightness/Darkness code.

 

 public static Bitmap makeBrightnessBitmap(Bitmap original, int brightness){
        Bitmap NewBitmap = new Bitmap(original.getWidth(), original.getHeight());
        int[] argb = new int[original.getWidth() * original.getHeight()];

        original.getARGB(argb, 0, original.getWidth(), 0, 0, original.getWidth(), original.getHeight());
        for (int i = argb.length - 1; i >= 0; --i) {
            int alpha = argb[i] >> 24;
            int red = (argb[i] >> 16) & 0xFF;
            int green = (argb[i] >> 8) & 0xFF;
            int blue = argb[i] & 0xFF;
            
            //int grey = (int) (0.3 * red + 0.59 * green + 0.11 * blue);
            
            int red2 = red + brightness;
            if (red2>0xFF) red2 = 0xFF;
            if (red2<0) red2 = 0;
            int green2 = green + brightness;
            if (green2>0xFF) green2 = 0xFF;
            if (green2<0) green2 = 0;
            int blue2 = blue + brightness;
            if (blue2>0xFF) blue2 = 0xFF;
            if (blue2<0) blue2 = 0;
                    
            int composite = (alpha << 24) | (red2 << 16) | (green2 << 8) | blue2;
            argb[i] = composite;
        }

        NewBitmap.setARGB(argb, 0, original.getWidth(), 0, 0, original.getWidth(), original.getHeight());

 

       //The following line is the test statement for the problem I experience and is not required
        NewBitmap.getARGB(argb, 0, original.getWidth(), 0, 0, original.getWidth(), original.getHeight());


        return NewBitmap;
    }

 

Does anyone else experience the same problem?