android实现图片二值化的主要计算类:
class ImageHelper {
public static Bitmap BinaryImage(Bitmap graymap,int value){
int width=graymap.getWidth();
int height=graymap.getHeight();
//创建二值化图像
Bitmap binarymap=null;
binarymap=graymap.copy(Bitmap.Config.ARGB_8888,true);
//依此循环 对像素进行处理
for(int i=0;i<width;i++){
for(int j=0;j<height;j++){
//得到当前像素值
int col=binarymap.getPixel(i,j);
//得到alpha通道的值
int alpha= col &0xFF000000;
//得到像素RGB的值
int red=(col & 0x00FF0000) >> 16;
int green=(col & 0x0000FF00) >> 8;
int blue = (col & 0x000000FF);
//用公式x=0.3*r+0.59*g+0.11*b计算出x代替原来的rgb
int gray = (int)((float)red*0.3+(float)green*0.59+(float)blue*0.11);
//对图像进行二值化处理
if(gray <= value){
gray=0;
}else{
gray=255;
}
//新的RGB
int newColor=alpha|(gray<<16)|(gray<<8)|gray;
//设置新图像的当前像素值
binarymap.setPixel(i,j,newColor);
}
}
return binarymap;
}
}