Java BufferedImage to Gray Scale gray(BufferedImage src)

Here you can find the source of gray(BufferedImage src)

Description

gray

License

Apache License

Declaration

public static final BufferedImage gray(BufferedImage src) 

Method Source Code

//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);
    }
}

Related

  1. imageToGrayscale(BufferedImage image)
  2. imageToGrayscale(BufferedImage img)
  3. grayImage(BufferedImage image)
  4. byteBufferToGrayBufferedImage(ByteBuffer buffer, BufferedImage bi)