I've written a code that gets a specific RGB color from an image and stores that in a file.
Now I'm trying to change the opacity of each of the colors in the image. For example, decrease the red color gradually until it is not there, and each time the color has decreased, the change that happened will be saved in a file. How can that be done?
Here's the code that I've written to get a specific color
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import java.nio.Buffer;
import javax.imageio.ImageIO;
public class BMPtoArray {
public static void getBlue(BufferedImage image){}
public static void getGreen(BufferedImage image){}
public static void getRed(BufferedImage image){
int blue = 0x000000FF;
int green = 0x0000FF00;
ColorModel cm = image.getColorModel();
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
WritableRaster raster =
image.copyData(image.getRaster().createCompatibleWritableRaster());
BufferedImage newImage = new BufferedImage(cm, raster, isAlphaPremultiplied, null);
for (int row = 0; row < newImage.getHeight(); row ) {
for (int col = 0; col < newImage.getWidth(); col ) {
int color = newImage.getRGB(col, row);
color &= ~green;
color &= ~blue;
newImage.setRGB(col, row, color);
}
}
try {
ImageIO.write(newImage, "bmp",new File("C:\\Users\\Mhamd\\Desktop\\3rd year
first semester\\Analysis and Coding\\labs\\2.2Java\\src\\newIMGRed" ".bmp") );
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void getBlack(BufferedImage image){
int red = 0x00FF0000;
int blue = 0x000000FF;
int green = 0x0000FF00;
ColorModel cm = image.getColorModel();
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
WritableRaster raster =
image.copyData(image.getRaster().createCompatibleWritableRaster());
BufferedImage newImage = new BufferedImage(cm, raster,
isAlphaPremultiplied, null);
for(int count = 1; count<=8;count ) {
try {
ImageIO.write(newImage, "bmp", new
File("C:\\Users\\Mhamd\\Desktop\\3rd year first semester\\Analysis
and Coding\\labs\\2.2Java\\src\\newIMGBlack" count ".bmp"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int row = 0; row < newImage.getHeight(); row ) {
for (int col = 0; col < newImage.getWidth(); col ) {
int color = newImage.getRGB(col, row);
color &= ~blue * count / 8;
color &= ~red * count / 8;
color &= ~green * count / 8;
newImage.setRGB(col, row, color);
}
}
}
}
public static void main(String[] args) throws Exception {
BufferedImage image = ImageIO.read(new File("C:\\Users\\Mhamd\\Desktop\\3rd year
first semester\\Analysis and Coding\\labs\\2.2Java\\src\\circleRGB.bmp"));
getRed(image);
getBlue(image);
getGreen(image);
}
}
The getBlue
and getGreen
methods are the same as the getRed
, just with different colors.
The picture below shows what I want to do. As you can see, the image starts with its colors, and gradually the red, green, and blue are decreasing until they are gone, and each time the color decreases, the change in the image is saved in a file.
CodePudding user response:
Stepping through your method’s logic:
for(int count = 1; count<=8;count ) {
try {
ImageIO.write(newImage, "bmp", new File("C:\\Users\\Mhamd\\Desktop\\3rd year first semester\\Analysis and Coding\\labs\\2.2Java\\src\\newIMGBlack" count ".bmp"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Writing the image before you have made changes will not produce the intended result. You need to write the image after you have modified the pixels. Remove the Image.write call, and remove the try/catch.
for (int row = 0; row < newImage.getHeight(); row ) {
for (int col = 0; col < newImage.getWidth(); col ) {
int color = newImage.getRGB(col, row);
Do not read the pixel from newImage
. Read it from the original image:
int color = image.getRGB(col, row);
Finally, these lines are incorrect:
color &= ~blue * count / 8;
color &= ~red * count / 8;
color &= ~green * count / 8;
~blue * count / 8
is performing math on the bit mask for all blue values in all pixels. It is not performing math on the actual pixel value.
First, isolate the blue value:
int blueValue = color & blue;
Now you have a value on which you can perform math:
blueValue = blueValue * count / 8;
blueValue &= blue;
You can update the color by first clearing the existing blue value:
color &= ~blue;
…and then updating the color with the new blue value:
color |= blueValue;
And of course, you’ll want to do the same with the red and green values.
Finally, after both for-loops are complete and all pixels have been updated, only then should you write your file:
newImage.setRGB(col, row, color);
}
}
}
try {
ImageIO.write(newImage, "bmp", new File("C:\\Users\\Mhamd\\Desktop\\3rd year first semester\\Analysis and Coding\\labs\\2.2Java\\src\\newIMGBlack" count ".bmp"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}