在BufferedImage上执行setRGB会将像素更改为黑色而不是颜色 [英] Performing setRGB on BufferedImage changes pixel to black instead of color

查看:919
本文介绍了在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();

然后,我读取了要从文件更改为空白的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).

**试试这个:

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会将像素更改为黑色而不是颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆