Before Contrast
After Black/White Contrast
![]() |
| Contrast 50 |
![]() |
| Contrast 100 |
After Color Contrast
![]() |
| Contrast 100 |
Create new Android Project
Project Name: PlayingwithBitmaps
Build Target: Android 2.3.3 //or greater than that
Application Name: PlayingwithBitmaps
Package Name: com.hamad.playingwithbitmaps
Create Activity: Main
Min SDK: 10 // or greater than that
- One image view to display the image.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:id="@+id/rlMain" >
<ImageView
android:id="@+id/imViewAndroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/beautiful" />
</RelativeLayout>
2. code of main activity:
package com.shaikhhamadali.blogspot.playingwithbitmaps;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.widget.ImageView;
public class Main extends Activity {
ImageView imViewAndroid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imViewAndroid = (ImageView) findViewById(R.id.imViewAndroid);
imViewAndroid.setImageBitmap(takeContrast(BitmapFactory.decodeResource(getResources(), R.drawable.android_droid),100));
}
public Bitmap takeContrast(Bitmap src, double value) {
// src image size
int width = src.getWidth();
int height = src.getHeight();
// create output bitmap with original size
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
// 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.red(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.red(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 final image
return bmOut;
}
public Bitmap takeColorContrast(Bitmap src, double value) {
// src image size
int width = src.getWidth();
int height = src.getHeight();
// create output bitmap with original size
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
// 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 final image
return bmOut;
}
}
Steps:
- Calculate contrast value: contrast = ((value + 100) / 100) ^ 2 (square by 2, or power 2).
- Repeat this for each color channel:
- Take pixel color divided by 255
- Minus 0.5
- Multiply contrast value calculated above.
- Plus 0.5
- Multiply by 255
3. note that:
- Refer to these Wiki pages on Contrast images to get the concepts and ideas:Contrast(Vision) | Image Editing – Contrast.
- With the help of this,takeContrast() method you can set the B/W Contrast of image on click,on action_down etc
- With the help of this,takeColorContrast() method you can set the Color Contrast of image on click,on action_down etc
4. conclusion:
- Some deep information about b/w contrast and color contrast.
- Know how to set b/w contrast and color contrast of an image bitmap from drawables.
5. About the post:
- The code seems to explain itself due to comments, and is very easy to understand.
- Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
- Hope you enjoy it!
6. Source Code:
you can download the source code here
Cheers,
you can download the source code here
Hamad Ali Shaikh



