Java Utililty Methods BufferedImage to Gray Scale

List of utility methods to do BufferedImage to Gray Scale

Description

The list of methods to do BufferedImage to Gray Scale are organized into topic(s).

Method

BufferedImageimageToGrayscale(BufferedImage image)
image To Grayscale
final BufferedImage grayscale;
if (image.getType() == BufferedImage.TYPE_BYTE_BINARY) {
    return image;
} else if (image.getType() == BufferedImage.TYPE_BYTE_GRAY) {
    grayscale = image;
} else if (image.getType() == BufferedImage.TYPE_INT_RGB
        || image.getType() == BufferedImage.TYPE_BYTE_INDEXED) {
    grayscale = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
...
BufferedImageimageToGrayscale(BufferedImage img)
image To Grayscale
colorConvert.filter(img, img);
return img;
BufferedImagegray(BufferedImage src)
gray
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);
return grayImage;
BufferedImagegrayImage(BufferedImage image)
gray 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;
voidbyteBufferToGrayBufferedImage(ByteBuffer buffer, BufferedImage bi)
Converts depth values in ShortBuffer to a gray scale BufferedImage.
if (bi.getType() != BufferedImage.TYPE_USHORT_GRAY)
    throw new IllegalArgumentException(
            "Invalid image type. Expect image " + "type BufferedImage.TYPE_USHORT_GRAY.");
int maxDepth = 1 << 16 - 1;
short[] imageArray = ((DataBufferUShort) bi.getRaster().getDataBuffer()).getData();
buffer.rewind();
while (buffer.remaining() > 0) {
    int pos = buffer.position();
...