现在想判定一个jpg图片是不是灰度的,只看颜色通道是不行的
见代码,分析每个像素 rgb是否近似相等
/**
* 判断一个点是否为灰色,
* @param R
* @param G
* @param B
* @param H
* @param S
* @param V
* @return 是否为灰色
*/
private static boolean isGrey_1(int R, int G, int B, int H, int S, int V)
{
// C4C9C8
//F7EEEA
//225 6 97
//if (((H >= 0 && H < 180) && (S >= 0 && S < 43) && (V >= 46 && V <= 255))//白色
int x = (R+G+B)/3;
int TotalGrey = Math.abs(R-x)+Math.abs(G-x)+Math.abs(B-x);
if (((H >= 0 && H <= 255) && (S >= 0 && S < 43) && (V >= 46 && V <= 255))//白色
|| ((H >= 0 && H <= 226) && (S >= 0 && S < 43) && (V >= 46 && V < 220))//灰色
|| ((H >= 0 && H < 180) && (S >= 0 && S < 60) && (V >= 46 && V < 230))//灰色
)
{
return true;
}
//不能全黑
if(TotalGrey<30&&x>5)
{
return true;
}
return false;
}
private static double isDocument( Bitmap src,int level ) {
//用于检测灰色比例显示
double MyRedColorProportionWithWhiteColor=0;
int w=src.getWidth();
int h=src.getHeight();//红头文件必须上半部有红字
int[]pixels=new int[w*h];
src.getPixels(pixels,0,w,0,0,w,h);
int H = 0, S = 0, V =0;
int R =0, G = 0, B = 0;
double goodRedPoints = 0;
double goodWhitePoints = 1;
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
int mColor=pixels[i*w+j];
float[] hsv = new float[3];//定义一个长度为3的数组
Color.colorToHSV(mColor, hsv);//转化hsv
R=(mColor>>16)&0xff;
G=(mColor>>8)&0xff;
B=(mColor)&0xff;
H=(int)hsv[0];
S=(int)(hsv[1]*100);
V=(int)(hsv[2]*100);
if (isGrey_1(R, G, B, H, S, V)==true)//灰色
{
goodWhitePoints++;
}
}
}
double whiteRate= (double)((double)(goodWhitePoints)/(w*h));
return whiteRate;
}
如果rgb 与平均值 偏移较大则认为是彩色的了。