Brightening or Darkening an RGB Buffered Image

This example demonstrates how to brighten or darken an RGB buffered image by scaling the red, green, and blue values in the image.
// To create a buffered image, see Creating a Buffered Image

// Brighten the image by 30%
float scaleFactor = 1.3f;
RescaleOp op = new RescaleOp(scaleFactor, 0, null);
bufferedImage = op.filter(bufferedImage, null);

// Darken the image by 10%
scaleFactor = .9f;
op = new RescaleOp(scaleFactor, 0, null);
bufferedImage = op.filter(bufferedImage, null);
If the image is not an RGB image, the following code converts a non-RGB image to an RGB buffered image:
// Get non-RGB image
Image image = new ImageIcon("image.gif").getImage();

// Create an RGB buffered image
BufferedImage bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);

// Copy non-RGB image to the RGB buffered image
Graphics2D g = bimage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();

Comments

26 Jan 2011 - 7:04am by Spadam (not verified)

Thanks for this. Don't yet understand what this code is actually doing but will read up on BufferedImage and Graphics2D. At least my app is now doing what I wanted.

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.