Java图像灰度处理(暂记)


public final class Class {

	public static void main(String[] args) throws IOException {
		String path = "C:\\Users\\Administrator\\Desktop\\picture\\input\\1.jpg";
		System.out.println(path);
		BufferedImage bufferedImage = ImageIO.read(new File(path));
    	int width = bufferedImage.getWidth();  
        int height = bufferedImage.getHeight(); 
		for (int j = 0; j < height; j++) {
		    for (int i = 0; i < width; i++) {
		        int p = bufferedImage.getRGB(i, j);

		        int a = (p >> 24)& 0xff ;
		        int r = (p >> 16)& 0xff;
		        int g = (p >> 8)& 0xff ;
		        int b = p & 0xff;
		        System.out.println("plexl: "+p+", alpha: "+a+",  red:  "+r+", green: " +g+", blue: "+b);
		        int gray = (int) (0.3 * r + 0.59 * g + 0.11 * b);
		        int thought=90;//一个阈值,可以改变大小来看真实的图片变化
		        gray=gray<thought?0:255;
		        p = (a<< 24) | (gray << 16) | (gray<< 8) |gray;
		        bufferedImage.setRGB(i, j, p);
		        
		        
		    }
		}
		String grayPath= "C:\\Users\\Administrator\\Desktop\\picture\\output\\8.jpeg"; 
        File newFile = new File(grayPath);  
        ImageIO.write(bufferedImage,"jpg", newFile);
	}

}

效果图:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值