1.转灰度
方法一:
//copy from web(灰度图)
public Bitmap toGrayscale(Bitmap bmpOriginal) {
int width, height;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
//Set the matrix to affect the saturation of colors.
//A value of 0 maps the color to gray-scale.
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bmpOriginal, 0, 0, paint);
return bmpGrayscale;
}
方法二:
//copy from web
public Bitmap convertToBlackWhite(Bitmap bmp) {
int width = bmp.getWidth(); // 获取位图的宽
int height = bmp.getHeight(); // 获取位图的高
int[] pixels = new int[width * height]; // 通过位图的大小创建像素点数组
bmp.getPixels(pixels, 0, width, 0, 0, width, height);
int alpha = 0xFF << 24;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int grey = pixels[width * i + j];
//分离三原色
int red = ((grey & 0x00FF0000) >> 16);
int green = ((grey & 0x0000FF00) >> 8);
int blue = (grey & 0x000000FF);
//转化成灰度像素
grey = (int) (red * 0.3 + green * 0.59 + blue * 0.11);
grey = alpha | (grey << 16) | (grey << 8) | grey;
pixels[width * i + j] = grey;
}
}
//新建图片
Bitmap newBmp = Bitmap.createBitmap(width, height, Config.RGB_565);
//设置图片数据
newBmp.setPixels(pixels, 0, width, 0, 0, width, height);
//居中
// Bitmap resizeBmp = ThumbnailUtils.extractThumbnail(newBmp, 380, 460);
return newBmp;
}
方法三:
private void bitmap2Gray(String filePath){
// File file = new File(SAVEDIR, filename + "_1.jpg");
Bitmap bitmapSrc =null;
Bitmap grayBitmap = null;
try {
bitmapSrc = BitmapFactory.decodeFile(filePath);
grayBitmap = Bitmap.createBitmap(bitmapSrc.getWidth(), bitmapSrc.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(grayBitmap);
Paint paint = new Paint();
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);
ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(colorMatrix);
paint.setColorFilter(colorMatrixFilter);
canvas.drawBitmap(bitmapSrc, 0, 0, paint);
canvas.save();
OutputStream outStream = null;
outStream = new FileOutputStream(new File(filePath));
grayBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (bitmapSrc!=null && !bitmapSrc.isRecycled()){
bitmapSrc.recycle();
}
if (grayBitmap!=null && !grayBitmap.isRecycled()){
grayBitmap.recycle();
}
}
}
2.转黑白:
public Bitmap switchColor(Bitmap switchBitmap){
int width=switchBitmap.getWidth();
int height=switchBitmap.getHeight();
// Turn the picture black and white
// Bitmap newBitmap=Bitmap.createBitmap(width,height,Bitmap.Config.RGB_565);
Bitmap newBitmap=Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
Canvas canvas=new Canvas(newBitmap);
canvas.drawBitmap(switchBitmap, new Matrix(), new Paint());
int current_color,red,green,blue,alpha,avg=0;
for (int i=0;i<width;i++){
for (int j=0;j<height;j++){
//获得每一个位点的颜色
current_color=switchBitmap.getPixel(i, j);
//获得三原色
red=Color.red(current_color);
green=Color.green(current_color);
blue=Color.blue(current_color);
alpha=Color.alpha(current_color);
avg=(red+green+blue)/3;// RGB average
if (avg>=210){ //亮度:avg>=126
//设置颜色
newBitmap.setPixel(i, j, Color.argb(alpha, 255, 255, 255));// white
} else if (avg<210 && avg>=80){ //avg<126 && avg>=115
newBitmap.setPixel(i, j, Color.argb(alpha, 108,108,108));//grey
}else{
newBitmap.setPixel(i, j, Color.argb(alpha, 0, 0, 0));// black
}
}
}
return newBitmap;
}
最终项目代码:
public Bitmap switchBlackNWhiteColor(Bitmap switchBitmap){
int width=switchBitmap.getWidth();
int height=switchBitmap.getHeight();
// Turn the picture black and white
Bitmap newBitmap=Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
Canvas canvas=new Canvas(newBitmap);
canvas.drawBitmap(switchBitmap, new Matrix(), new Paint());
int current_color,red,green,blue,alpha,avg=0;
for (int i=0;i<width;i++){
for (int j=0;j<height;j++){
//get the color of each bit
current_color=switchBitmap.getPixel(i, j);
//achieve three-primary color
red=Color.red(current_color);
green=Color.green(current_color);
blue=Color.blue(current_color);
alpha=Color.alpha(current_color);
avg=(red+green+blue)/3;// RGB average
if (avg>=126){
newBitmap.setPixel(i, j, Color.argb(alpha, 255, 255, 255));// white
} else if (avg<126 && avg>=115){
newBitmap.setPixel(i, j, Color.argb(alpha, 108 ,108,108));//grey
} else{
newBitmap.setPixel(i, j, Color.argb(alpha, 0, 0, 0));// black
}
}
}
return newBitmap;
}