更改图像Java中每个像素的颜色 [英] Change the color of each pixel in an image java

查看:52
本文介绍了更改图像Java中每个像素的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将不同的像素更改为不同的颜色.基本上,将像素的一部分更改为透明.

I wanna change different pixel to different color. Basically, change part of pixel to transparent.

for(int i = 0; i < image.getWidth();i++)
        for(int j = 0; j < image.getHeight(); j ++)
        {
            image.setRGB(i,j , 0);
        }

//I也将第三个参数0更改为另一个属性.但它仍然无法正常工作.全部显示为黑色.你有什么想法吗?

//I aslo change the third parameter 0 to another attribute. but it still does not work. it all show black. do you have some ideas?

阴.谢谢

class ImagePanel extends JPanel {

    private BufferedImage image;

    public ImagePanel(int width, int height, BufferedImage image) {
        this.image = image;
        image = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_ARGB);
        repaint();
    }

    /**
     * Draws the image.
     */

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (int i = 0; i < image.getWidth(); i++) {
            for (int j = 0; j < image.getHeight(); j++) {
                image.setRGB(i, j, 0);
            }
        }
        g.drawImage(image, 0, 0, getWidth(), getHeight(), this);

    }

}

推荐答案

第三个参数是32位的ARGB值.这是按位格式布置的:

The third parameter is ARGB value in 32 bits. This is laid out in bit form as:

AAAAAAAA|RRRRRRRR|GGGGGGGG|BBBBBBBBB

有关Java文档,请参见

See the javadoc for BufferedImage.setRGB (assuming your using BufferedImage, your question doesn't actually say...)

将此BufferedImage中的像素设置为指定的RGB值.这假定像素为默认RGB颜色模型TYPE_INT_ARGB,和默认的sRGB颜色空间.对于具有IndexColorModel的图像,选择颜色最接近的索引

Sets a pixel in this BufferedImage to the specified RGB value. The pixel is assumed to be in the default RGB color model, TYPE_INT_ARGB, and default sRGB color space. For images with an IndexColorModel, the index with the nearest color is chosen

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