会将像素更改为黑色而不是颜色

会将像素更改为黑色而不是颜色

本文介绍了在 BufferedImage 上执行 setRGB 会将像素更改为黑色而不是颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

** 重要更新,见下文!**

** Important update, see below! **

我正在创建一个程序,当该像素满足 Java 中的一组条件时,该程序会将 BufferedImage 的像素更改为某种颜色.但是,当我将图像写入磁盘时,应该着色的像素变为黑色.

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

首先我定义颜色,使用 RGB 代码:

First I define the color, using RGB codes:

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

然后我将要从 File 更改为名为blank"的 BufferedImage 的图像读取:

Then I read the image I want to alter from a File into a BufferedImage called "blank":

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

现在,遍历像素,当位置 (x, y) 处的像素符合条件时,将其颜色更改为紫色:

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

blank.setRGB(x, y, PURPLE);

现在,将空白"写入磁盘.

Now, write "blank" to the disk.

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

生成的文件应该是带有一些紫色像素的空白",但有问题的像素是黑色的.我知道问题在于 setRGB 而不是任何导入或导出功能,因为空白"本身是一个彩色图像,并因此被写入文件.我四处阅读并看到很多帖子建议我使用 Graphics2D 并避免 setRGB,但没有讨论逐像素颜色更改.

The resulting file should be "blank" with some purple pixels, but the pixels in question are instead black. I know for a fact that the issue is with setRGB and NOT any import or export functions, because "blank" itself is a color image, and gets written to file as such. I read around and saw a lot of posts recommending that I use Graphics2D and to avoid setRGB, but with no discussion of pixel-by-pixel color changing.

我也尝试过直接位操作,如下所示:

I also tried direct bit manipulation, like this:

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

我可能做错了,但如果我正确地输入它并不重要,因为当我这样做时像素被设置为透明(不管数字怎么说,这很奇怪,至少可以说).

I'm probably doing that wrong, but if I put it in correctly it wouldn't matter, because the pixels are getting set to transparent when I do this (regardless of what the numbers say, which is very strange, to say the least).

** 当我尝试这个时:

** When I try this:

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

我的输出文件是灰度的,所以这意味着 setRGB 实际上是在灰度中修改我的图片.我认为这实际上是一个相当简单的问题,但我无法找到解决方案.

My output file is grayscale, so that means setRGB is, in fact, modifying my picture in grayscale. I think this is actually a rather simple issue, but the solution eludes me.

推荐答案

基于 https://stackoverflow.com/a/中的见解21981173 你发现自己......(发布问题几分钟后)......似乎加载后直接将图像转换为ARGB就足够了:

Based on the insights in https://stackoverflow.com/a/21981173 that you found yourself ... (a few minutes after posting the question) ... it seems that it should be sufficient to simply convert the image into ARGB directly after it was loaded:

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;
}

这篇关于在 BufferedImage 上执行 setRGB 会将像素更改为黑色而不是颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 12:08