Java BufferedImage to Gray Scale grayImage(BufferedImage image)

Here you can find the source of grayImage(BufferedImage image)

Description

gray Image

License

Apache License

Declaration

public static BufferedImage grayImage(BufferedImage image) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

public class Main {

    public static BufferedImage grayImage(String filename) {
        return grayImage(new File(filename));
    }//from  w w  w.  j  av  a2 s .  c om

    public static BufferedImage grayImage(File file) {
        try {
            BufferedImage image = ImageIO.read(file);
            return grayImage(image);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static BufferedImage grayImage(BufferedImage image) {
        int width = image.getWidth();
        int height = image.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 = image.getRGB(i, j);
                grayImage.setRGB(i, j, rgb + 100);
            }
        }
        return grayImage;
    }
}

Related

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