image

Get and set pixels on a Buffered Image

In this tutorial we are going to show you how to set and get the RGB value of each pixel of a Buffered Image. This is particularly useful when you want to perform several operation on images based on the RGB values of each individual image. Or if you want your UI to interact in some way according to the values of the pixel that the user points to.

In short in order to flip an image one should take the following steps:

  • Load an image from a source using Toolkit.getDefaultToolkit().getImage method
  • Use an ImageObserver to monitor the loading of the image. When the image is fully load the user will be notified
  • Create a buffed image from the source image with a format more close to the custom display environment using GraphicsEnvironmentGraphicsDevice and GraphicsConfiguration to perform several image configurations
  • Use Image.getRGB(x,y) to get the RGB value of a specific pixel and Image.setRGB(x, y, rgbValue) to set the RGB value of the pixel.
  • And simply paint the buffered image in a new Frame

Let’s take a look at the code snippet that follows

package com.javacodegeeks.snippets.desktop;

import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;

public class BufferedImagePixels {

  static boolean imageLoaded = false;

  public static void main(String[] args) {

// The ImageObserver implementation to observe loading of the image

ImageObserver myImageObserver = new ImageObserver() {

  public boolean imageUpdate(Image image, int flags, int x, int y, int width, int height) {

    if ((flags & ALLBITS) != 0) {

imageLoaded = true;

System.out.println("Image loading finished!");

return false;

    }

    return true;

  }

};

// The image URL - change to where your image file is located!

String imageURL = "image.png";

/**

 * This call returns immediately and pixels are loaded in the background

 * We use an ImageObserver to be notified when the loading of the image

 * is complete

 */

Image sourceImage = Toolkit.getDefaultToolkit().getImage(imageURL);

sourceImage.getWidth(myImageObserver);

// We wait until the image is fully loaded

while (!imageLoaded) {

    try {

  Thread.sleep(100);

    } catch (InterruptedException e) {

    }

}

// Create a buffered image from the source image with a format that's compatible with the screen

GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();

GraphicsDevice graphicsDevice = graphicsEnvironment.getDefaultScreenDevice();

GraphicsConfiguration graphicsConfiguration = graphicsDevice.getDefaultConfiguration();

// If the source image has no alpha info use Transparency.OPAQUE instead

BufferedImage image = graphicsConfiguration.createCompatibleImage(sourceImage.getWidth(null), sourceImage.getHeight(null), Transparency.BITMASK);

// Copy image to buffered image

Graphics2D graphics = image.createGraphics();

// Paint the image onto the buffered image

graphics.drawImage(sourceImage, 0, 0, null);

graphics.dispose();

int x = 10;

int y = 10;

// Get a pixel

int rgb = image.getRGB(x, y);

System.out.println("Pixel at [" + x + "," + y + "] RGB : " + rgb);

// Get all the pixels

int w = image.getWidth(null);

int h = image.getHeight(null);

int[] rgbs = new int[w*h];

image.getRGB(0, 0, w, h, rgbs, 0, w);

// Set a pixel

rgb = 0xFF00FF00; // green

image.setRGB(x, y, rgb);

  }

}

Example Output:

Image loading finished! Pixel at [10,10] RGB : -16294578

 
This was an example on how to Set and Get the pixels of a Buffed Image.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button