Handling Images in Java is fun. Here are some often used image manipulation methods.
- Negating colors of an image
- Change all pixels in BufferedImage to one color
- Changing opacity of image
- Adding two BufferedImages (with given opacity)
- Saving BufferedImage to a file
- Loading a BufferedImage from a file
Most of these methods come from my java heatmap creating class so if you want to download them get them there.
Negating colors of an image
/**
* returns a negated version of this image.
*
* @param img buffer to negate
* @return negated buffer
*/
private BufferedImage negateImage(BufferedImage img) {
int width = img.getWidth();
int height = img.getHeight();
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int rGB = img.getRGB(x, y); //Swaps values
//i.e. 255, 255, 255 (white)
//becomes 0, 0, 0 (black)
int r = Math.abs(((rGB >>> 16) & 0xff) - 255); // red inverted
int g = Math.abs(((rGB >>> 8) & 0xff) - 255); // green inverted
int b = Math.abs((rGB & 0xff) - 255); // blue inverted
// transform back to pixel value and set it
img.setRGB(x, y, (r << 16) | (g << 8) | b);
}
}
return img;
}
UPDATE: a much faster way to negate the image (result differs a bit from version above, but generally it should not make much of a difference):
private BufferedImage negateImage(BufferedImage img) {
RescaleOp op = new RescaleOp(-1.0f, 255f, null);
BufferedImage negative = op.filter(img, null);
return negative;
}
Change all pixels in BufferedImage to one color
/**
* changes all pixel in the buffer to the provided color.
*
* @param buff buffer
* @param color color
*/
private void paintInColor(BufferedImage buff, Color color) {
Graphics2D g2 = buff.createGraphics();
g2.setColor(color);
g2.fillRect(0, 0, buff.getWidth(), buff.getHeight());
g2.dispose();
}
Changing opacity of image
/**
* changes the opacity of the image.
*
* @param buff1 buffer to change opacity
* @param opaque new opacity
*/
private void makeTransparent(BufferedImage buff1, float opaque) {
Graphics2D g2d = buff1.createGraphics();
g2d.setComposite(
AlphaComposite.getInstance(AlphaComposite.SRC, opaque));
g2d.drawImage(buff1, 0, 0, null);
g2d.dispose();
}
Adding two BufferedImages (with given opacity)
/**
* prints the contents of buff2 on buff1 with the given opaque value
* starting at position 0, 0.
*
* @param buff1 buffer
* @param buff2 buffer to add to buff1
* @param opaque opacity
*/
private void addImage(
BufferedImage buff1, BufferedImage buff2, float opaque) {
addImage(buff1, buff2, opaque, 0, 0);
}
/**
* prints the contents of buff2 on buff1 with the given opaque value.
*
* @param buff1 buffer
* @param buff2 buffer
* @param opaque how opaque the second buffer should be drawn
* @param x x position where the second buffer should be drawn
* @param y y position where the second buffer should be drawn
*/
private void addImage(BufferedImage buff1, BufferedImage buff2,
float opaque, int x, int y) {
Graphics2D g2d = buff1.createGraphics();
g2d.setComposite(
AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opaque));
g2d.drawImage(buff2, x, y, null);
g2d.dispose();
}
Saving BufferedImage to a file
/**
* writes the image in the provided buffer to the destination file.
*
* @param buff buffer to be saved
* @param dest destination to save at
*/
private void saveImage(BufferedImage buff, String dest) {
try {
File outputfile = new File(dest);
ImageIO.write(buff, "png", outputfile);
} catch (IOException e) {
print("error saving the image: " + dest + ": " + e);
}
}
Loading a BufferedImage from a file
/**
* returns a BufferedImage from the Image provided.
*
* @param ref path to image
* @return loaded image
*/
private BufferedImage loadImage(String ref) {
BufferedImage b1 = null;
try {
b1 = ImageIO.read(new File(ref));
} catch (IOException e) {
System.out.println("error loading the image: " + ref + " : " + e);
}
return b1;
}