How adjust brightness of bitmap in gradient format in android? - android

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 ?

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

Related

image brightness taking too much time for click

Hi In my application am displaying some themes .In that theme when am pressing any image that image background will turn to give little bit more brighter compared to remaining all the themes.
Now, My issue is while pressing the image it is taking too much work on main thread.How to do while pressing the image fast touch should happens.
Can any one please help me to resolve this issue.
sample.java:
mTick1.setImageBitmap(SetBrightness(BitmapFactory.decodeResource(getResources(), R.drawable.classic),-80));
mTick2.setImageBitmap(SetBrightness(BitmapFactory.decodeResource(getResources(), R.drawable.casual1),-0));
mTick3.setImageBitmap(SetBrightness(BitmapFactory.decodeResource(getResources(), R.drawable.natural),-0));
mTick4.setImageBitmap(SetBrightness(BitmapFactory.decodeResource(getResources(), R.drawable.smart),-0));
mTick5.setImageBitmap(SetBrightness(BitmapFactory.decodeResource(getResources(), R.drawable.meeting),-0));
mTick6.setImageBitmap(SetBrightness(BitmapFactory.decodeResource(getResources(), R.drawable.cool),-0));
mTick1.setVisibility(View.VISIBLE);
mTick2.setVisibility(View.VISIBLE);
mTick3.setVisibility(View.VISIBLE);
mTick4.setVisibility(View.VISIBLE);
mTick5.setVisibility(View.VISIBLE);
mTick6.setVisibility(View.VISIBLE);
mTick6.setVisibility(View.VISIBLE);
Brightness method:
public Bitmap SetBrightness(Bitmap src, int value) {
// 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);
// 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;
}

How to increase brightness of the button?

Hi I have one application in that when am try to touch any button that one should be more brighter compared to remaining all the button.
I have tried one code it is working fine .but,it is taking too much for displaying.
Can any one tell me any other way I can do it?
if (themeClick.equals("6")) {/*cool*/
mTick1.setVisibility(View.VISIBLE);
mTick2.setVisibility(View.VISIBLE);
mTick3.setVisibility(View.VISIBLE);
mTick4.setVisibility(View.VISIBLE);
mTick5.setVisibility(View.VISIBLE);
mTick6.setImageBitmap(SetBrightness(BitmapFactory.decodeResource(getResources(), R.drawable.cool),-80));
mTick1.setImageBitmap(SetBrightness(BitmapFactory.decodeResource(getResources(), R.drawable.classic),-0));
mTick2.setImageBitmap(SetBrightness(BitmapFactory.decodeResource(getResources(), R.drawable.casual1),-0));
mTick3.setImageBitmap(SetBrightness(BitmapFactory.decodeResource(getResources(), R.drawable.natural),-0));
mTick4.setImageBitmap(SetBrightness(BitmapFactory.decodeResource(getResources(), R.drawable.smart),-0));
mTick5.setImageBitmap(SetBrightness(BitmapFactory.decodeResource(getResources(), R.drawable.meeting),-0));
mTick6.setVisibility(View.VISIBLE);
}
public Bitmap SetBrightness(Bitmap src, int value) {
// 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);
// 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;
}

How can we enhance apply Brightness Effect with custom filter?

I am trying to add custom filter and add brightness and contrast, but it takes to much time.
How can i decrease these times?
Can we use other way or add filter over filter?
//main class
private ImageView imgMain;
imgMain = (ImageView) findViewById(R.id.effect_main);
imgMain.setColorFilter(filterClass.setFilter(2));
Bitmap bitmap = ((BitmapDrawable)imgMain.getDrawable()).getBitmap();
bitmap =imgFilter.applyBrightnessEffect(bitmap , 150);
imgMain.setImageBitmap(bitmap);
for custom filter
//filter methords
public ColorMatrixColorFilter catNightFilter() {
float[] matrix = {
89, 61, -5, 0, 0, //red
77, 67, 24, 0, 0, //green
-57, -13, 81, 0, 0, //blue
0, 0, 0, 0, 0 //alpha
};
ColorMatrix cm = new ColorMatrix(matrix);``
return new ColorMatrixColorFilter(cm);
}
This Brightness code takes to much time it convert each pixel.
// scan through all pixels
//britness methord
public Bitmap applyBrightnessEffect(Bitmap src, int value) {
// 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;
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);
// 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;
}

How to programatically apply effects on imageview image in android?

i want to apply various effects on image view image.this image is already sent from main activity.i have the code for effects but don't know the method how to apply it on button click.kindly guide me.
public class PhotoEffect extends Activity implements OnClickListener {
ImageView camphoto;
Bitmap bmp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photoeffect);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Bundle extras=getIntent().getExtras();
//b=(Bitmap)extras.get("img");
byte[] byteArray=extras.getByteArray("img");
bmp=BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
camphoto=(ImageView)findViewById(R.id.camimage);
camphoto.setImageBitmap(bmp);
/*if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}*/
}
#Override
public void onClick(View v)
{
Bitmap bitmap = ((BitmapDrawable)camphoto.getDrawable()).getBitmap();
switch (v.getId())
{
case R.id.btnsepia:
//Bitmap bitmap = ((BitmapDrawable)camphoto.getDrawable()).getBitmap();
camphoto.setImageBitmap(createSepiaToningEffect(bitmap, 2, .5, .6, .59));
break;
case R.id.btnhighlight:
camphoto.setImageBitmap(doHighlightImage(bitmap));
break;
}
}
You must define the createSepiaToningEffect function like this returning a Bitmap after processing.
public static Bitmap createSepiaToningEffect(Bitmap src, int depth, double red, double green, double blue) {
int width = src.getWidth();
int height = src.getHeight();
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
final double GS_RED = 0.3;
final double GS_GREEN = 0.59;
final double GS_BLUE = 0.11;
int A, R, G, B;
int pixel;
for(int x = 0; x < width; ++x) {
for(int y = 0; y < height; ++y) {
pixel = src.getPixel(x, y);
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
B = G = R = (int)(GS_RED * R + GS_GREEN * G + GS_BLUE * B);
// apply intensity level for sepid-toning on each channel
R += (depth * red);
if(R > 255) { R = 255; }
G += (depth * green);
if(G > 255) { G = 255; }
B += (depth * blue);
if(B > 255) { B = 255; }
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
return bmOut;
}

how to make the color image to black and white in Android

I wanted to know the way to convert the color image (which i am downloading from net) to black and white when i am displaying it to the user in android.
can anybody found this requirement in any of your android work. Please let me know.
Thanks
Lakshman
Hi you can make the image black n white using contrast.
See the code..
public static Bitmap createContrast(Bitmap src, double value) {
// 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;
// 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 bmOut;
}
Set the double value to 50 on mathod call. For Example createContrast(Bitmap src, 50)
Use the built-in methods:
public static Bitmap toGrayscale(Bitmap srcImage) {
Bitmap bmpGrayscale = Bitmap.createBitmap(srcImage.getWidth(), srcImage.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(srcImage, 0, 0, paint);
return bmpGrayscale;
}

Categories

Resources