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);
}
}
效果图: