EN
Java - create negative image
10 points
In this short article, we would like to show how to create negative image using Java.
To find create negative image it is good to calculate the following operation on the image 255 - A = B
.
Operation visualization:

Practical example:
xxxxxxxxxx
1
package com.example;
2
3
import java.awt.Color;
4
import java.awt.image.BufferedImage;
5
import java.io.File;
6
import java.io.IOException;
7
8
import javax.imageio.ImageIO;
9
10
public class Program {
11
12
public static void main(String[] args) throws IOException {
13
BufferedImage aImage = ImageIO.read(new File("a.png")); // A image (input image)
14
int aWidth = aImage.getWidth();
15
int aHeight = aImage.getHeight();
16
BufferedImage bImage = new BufferedImage(aWidth, aHeight, BufferedImage.TYPE_INT_RGB);
17
for (int y = 0; y < aHeight; ++y) {
18
for (int x = 0; x < aWidth; ++x) {
19
Color aColor = new Color(aImage.getRGB(x, y));
20
Color bColor = new Color(255 - aColor.getRed(), 255 - aColor.getGreen(), 255 - aColor.getBlue());
21
bImage.setRGB(x, y, bColor.getRGB());
22
}
23
}
24
ImageIO.write(bImage, "PNG", new File("b.png")); // B image (output image)
25
}
26
}
a.png
file: