java区别黑白和彩色照片原理:通过读照片流,获取每个点位的色彩灰度,当灰度点数大于60个点位或者更多的时候则认定其为黑白照片。
代码如下:
public static boolean execote(File file){BufferedImage src;try {src = ImageIO.read(file);int height =src.getHeight();int width=src.getWidth();//长宽 140pxint [] rgb=new int[4];int o=0;int x;if(height>140 && width>140){for(int i=0;i<width;i++){for (int j=0;j<height;j++){int piexl=src.getRGB(i, j);//获取像素点点数为 height*widthrgb[1]=(piexl & 0xff0000)>>16;//抽取红色的值rgb[2]=(piexl & 0xff00)>>8;//抽取绿色rgb[3]=(piexl & 0xff);//抽取蓝色//获取相差最大值x=Math.max(Math.abs(rgb[1]-rgb[2]),(Math.max(Math.abs(rgb[1]-rgb[3]),Math.abs(rgb[2]-rgb[3]))));if(x>50){//灰度大于50 像素点数量大于60点o+=1;if(o>=60){return true;}}}}}} catch (Exception e) {src=null;System.out.println(file.getName());//e.printStackTrace();}finally{}return false;}