java 判断图片是否是彩色

public class DetermineTheColor {
/**

* @param imagePath 下载图片的路径
* @param fileTmpPath 临时目录 以/结尾
* @return  返回true是彩色图片 返回false是黑灰图片
* @throws Exception
*/
public static Boolean execote(String imagePath,String fileTmpPath) throws Exception {
//下载图片之后的名字
String fileName = String.format("%1$tY%1$tm%1$td%1$tH%1$tM%1$tS%1$tL",new Date())+Utils.randomNum(5) +".jpg";

//下载图片
downLoadFromUrl(imagePath,fileName,fileTmpPath);
 
BufferedImage src = ImageIO.read(new File(fileTmpPath+fileName));
         int height = src.getHeight();  
         int width  = src.getWidth();   
         int[] rgb = new int [4];  
         int o = 0;
         for(int i =0;i<width;i++){  
             for(int j =0;j<height;j++){  
              int pixel = src.getRGB(i, j);  
               rgb[1] = (pixel & 0xff0000) >> 16;  
               rgb[2] = (pixel & 0xff00) >> 8;  
               rgb[3] = (pixel & 0xff);   
               //如果像素点不相等的数量超过50个 就判断为彩色图片
               if(rgb[1] != rgb[2] &&  rgb[2] != rgb[3] && rgb[3] != rgb[1]){
                o+=1;
                if(o>=50){
                return true;
                }
               }
             }   
         }  
 
return false;
 
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值