Working with images in Java
The image BufferedImage
is given. It should be converted to black-and-white format by averaging the values of the three channels. How do I get these three channels?
4
1 answers
You can get RGB colors from an image via:
Color c = new Color(image.getRGB());
int red = c.getRed();
int green = c.getGreen();
int blue = c.getBlue();
The image pixel is represented as a 32-bit Integer, so RGB can still be obtained as:
int rgb = getRGB(...);
int red = (rgb >> 16) & 0x000000FF;
int green = (rgb >>8 ) & 0x000000FF;
int blue = (rgb) & 0x000000FF;
After going through the entire image in a loop pixel by pixel:
BufferedImage image = ...
for(int i=0; i<image.getWidth(); i++) {
for(int j=0; j<image.getHeight(); j++) {
...
}
}
As a result, you create a new image, go through the source loop, get rgb colors from each pixel, convert them and write them to a new BufferedImage
.
5
Author: Denis, 2017-05-23 12:39:13