简体   繁体   English

如何停止Java将BufferedImage设置为完全透明?

[英]How do I stop java from setting my BufferedImage to fully transparent?

I am trying to create an image editor in java, but when I run the code, the output image is fully transparent. 我试图在Java中创建图像编辑器,但是当我运行代码时,输​​出图像是完全透明的。

Here is my code for Main.java: 这是我的Main.java代码:

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Main {

    public static void main(String[] args) {

        BufferedImage image = null;
        try {
            image = ImageIO.read(new File("strawberry.png"));
        } catch (IOException e) {
            System.out.println(e);
        }

        new Negative(image);

        File outputfile = new File("saved.png");

        try {
            ImageIO.write(image, "png", outputfile);
        } catch (IOException e) {
            System.out.println(e);
        }

    }

}

And here is my code for Negative.java: 这是我的Negative.java代码:

import java.awt.image.BufferedImage;

public class Negative {

    public Negative(BufferedImage img) {

          for (int x = 0; x < img.getWidth(); ++x) {
                for (int y = 0; y < img.getHeight(); ++y) {

                    int rgb = img.getRGB(x, y);
                    int r = (rgb >> 16) & 0xFF;
                    int g = (rgb >> 8) & 0xFF;
                    int b = (rgb & 0xFF);

                    r = 255 - r;
                    g = 255 - g;
                    b = 255 - b;

                    int newColour = (r << 16) + (g << 8) + (b << 4); 
                    img.setRGB(x, y, newColour);

                }
          }

    }

}

If anyone could help I would be very grateful. 如果有人能帮助我,我将非常感激。

Problem 问题

What they call RGB color is in fact ARGB, 8 bits for each. 他们所谓的RGB颜色实际上是ARGB,每个8位。 Alpha is given in the highest 8 bits, 0 for transparent to 255 for fully opaque. Alpha以最高的8位给出,0表示透明,而255表示完全不透明。

This is what TYPE_INT_ARGB means in the javadoc for BufferedImage.setRGB() : 这就是TYPE_INT_ARGBBufferedImage.setRGB()的javadoc中的含义:

The pixel is assumed to be in the default RGB color model, TYPE_INT_ARGB, and default sRGB color space. 假定像素处于默认RGB颜色模型TYPE_INT_ARGB和默认sRGB颜色空间中。

Solution

For a fully opaque image, add a 255 alpha value: 对于完全不透明的图像,请添加255 alpha值:

int newColour = (0xff << 24) + (r << 16) + (g << 8) + (b << 4); 

Alternatively, you can take the original alpha of your image if you extract it too: 另外,也可以提取图像的原始Alpha:

int rgb = img.getRGB(x, y);
int alpha = (rgb >>> 24);
int r = (rgb >> 16) & 0xFF;
int g = (rgb >> 8) & 0xFF;
int b = (rgb & 0xFF);

int newColour = (alpha << 24) + (r << 16) + (g << 8) + (b << 4);

There is one more component of a color: alpha channel. 颜色还有一个组成部分:Alpha通道。 It is stored in 24-31 bits of a number. 它以24-31位数字存储。 If it is set to 0, the image is transparent. 如果将其设置为0,则图像是透明的。 So you need to set 24-31 bits of newColor to 1 to make it opaque. 因此,您需要将newColor的24-31位设置为1,以使其不透明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM