Running setRGB on a BufferedImage changes the pixel to black instead of color

** Important update, see below! **

I am creating a program that changes the pixels of a BufferedImage to a specific color when that pixel fulfills a set of conditions in Java. However, when I write the image to disk, the pixels that need to be painted are black.

First, I define the color using RGB codes:

Color purple = new Color(82, 0, 99);
int PURPLE = purple.getRGB();

Then I read the image I want to change from a file in BufferedImage called "blank":

BufferedImage blank = ImageIO.read(new File("some path"));

Now scroll through the pixels, and when the pixel at the location (x, y) matches the criteria, change its color to purple:

blank.setRGB(x, y, PURPLE);

Now write "empty" to disk.

File output = new File("some other path");
ImageIO.write(blankIn, "png", output); // try-catch blocks intentionally left out

"" , , , . , setRGB , "" . , Graphics2D setRGB, .

, :

blank.setRGB(x, y, ((82 << 16) + (0 << 8) + 99));

, , , , , , ( , , , ).

** :

blank.setRGB(x, y, Color.RED.getRGB());

, , setRGB, , . , , .

+3
2

fooobar.com/questions/2112567/..., ... ( )... , ARGB :

public static BufferedImage convertToARGB(BufferedImage image)
{
    BufferedImage newImage = new BufferedImage(
        image.getWidth(), image.getHeight(),
        BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = newImage.createGraphics();
    g.drawImage(image, 0, 0, null);
    g.dispose();
    return newImage;
}
+3

, Java, , , Java BufferedImage, BufferedImage. , , Java .

, , , .

: BufferedImage ARGB :

BufferedImage blank2 = blank;
    // Create temporary copy of blank
blank = new BufferedImage(blank.getWidth(), blank.getHeight(), BufferedImage.TYPE_INT_ARGB);
    // Recreate blank as an ARGB BufferedImage
ColorConvertOp convert = new ColorConvertOp(null);
    // Now create a ColorConvertOp object
convert.filter(blank2, blank);
    // Convert blank2 to the blank color scheme and hold it in blank

blank = ImageIO.read(new File("some path")).

+1

All Articles