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 r
Solution 1:
Hi you can make the image black n white using contrast.
See the code..
publicstatic Bitmap createContrast(Bitmap src, double value) {
// image sizeintwidth= src.getWidth();
intheight= src.getHeight();
// create output bitmapBitmapbmOut= Bitmap.createBitmap(width, height, src.getConfig());
// color informationint A, R, G, B;
int pixel;
// get contrast valuedoublecontrast= Math.pow((100 + value) / 100, 2);
// scan through all pixelsfor(intx=0; x < width; ++x) {
for(inty=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; }
elseif(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; }
elseif(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; }
elseif(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)
Solution 2:
Use the built-in methods:
publicstatic Bitmap toGrayscale(Bitmap srcImage) {
BitmapbmpGrayscale= Bitmap.createBitmap(srcImage.getWidth(), srcImage.getHeight(), Bitmap.Config.ARGB_8888);
Canvascanvas=newCanvas(bmpGrayscale);
Paintpaint=newPaint();
ColorMatrixcm=newColorMatrix();
cm.setSaturation(0);
paint.setColorFilter(newColorMatrixColorFilter(cm));
canvas.drawBitmap(srcImage, 0, 0, paint);
return bmpGrayscale;
}
Post a Comment for "How To Make The Color Image To Black And White In Android"