Here you can find the source of gray(BufferedImage src)
public static final BufferedImage gray(BufferedImage src)
//package com.java2s; //License from project: Apache License import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class Main { public static final BufferedImage gray(BufferedImage src) { int width = src.getWidth(); int height = src.getHeight(); BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int rgb = src.getRGB(i, j); grayImage.setRGB(i, j, rgb); }/*from w w w . j av a2 s . co m*/ } return grayImage; } public static final BufferedImage gray(File f) throws IOException { BufferedImage src = ImageIO.read(f); return binary(src); } public static final BufferedImage binary(BufferedImage src) { int width = src.getWidth(); int height = src.getHeight(); BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int rgb = src.getRGB(i, j); grayImage.setRGB(i, j, rgb); } } return grayImage; } public static final BufferedImage binary(File f) throws IOException { BufferedImage src = ImageIO.read(f); return binary(src); } }