更改图像中每个像素的颜色java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27071351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change the color of each pixel in an image java
提问by Huazhe Yin
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 aslo change the third parameter 0 to another attribute. but it still does not work. it all show black. do you have some ideas?
//我也将第三个参数 0 更改为另一个属性。但它仍然不起作用。它都显示黑色。你有什么想法吗?
yin. thanks
阴。谢谢
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);
}
}
采纳答案by Adam
The third parameter is ARGB value in 32 bits. This is laid out in bit form as:
第三个参数是 32 位的 ARGB 值。这以位形式布置为:
AAAAAAAA|RRRRRRRR|GGGGGGGG|BBBBBBBBB
See the javadoc for BufferedImage.setRGB(assuming your using BufferedImage, your question doesn't actually say...)
请参阅BufferedImage.setRGB的 javadoc (假设您使用 BufferedImage,您的问题实际上并没有说...)
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
将此 BufferedImage 中的像素设置为指定的 RGB 值。假定像素处于默认 RGB 颜色模型、TYPE_INT_ARGB 和默认 sRGB 颜色空间。对于带有 IndexColorModel 的图像,选择颜色最接近的索引
- If you're using an image type that supports transparency it is important you set alpha 255 means fully opaque, 0 is fully transparent.
- 如果您使用支持透明度的图像类型,重要的是您设置 alpha 255 表示完全不透明,0 表示完全透明。
You can create such a value using bit shifting.
您可以使用位移来创建这样的值。
int alpha = 255;
int red = 0;
int green = 255;
int blue = 0;
int argb = alpha << 24 + red << 16 + green << 8 + blue
image.setRGB(i, j, argb);
Luckily there is a getRGB() method on java.awt.Colorinstances, so you could use
幸运的是java.awt.Color实例上有一个 getRGB() 方法,因此您可以使用
image.setRGB(i, j, Color.green.getRGB());
Here's a full working example, perhaps you can compare to your code:
这是一个完整的工作示例,也许您可以与您的代码进行比较:
public class StackOverflow27071351 {
private static 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();
}
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, new Color(255, 0, 0, 127).getRGB());
}
}
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
int width = 640;
int height = 480;
frame.setSize(width, height);
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(new ImagePanel(width, height, image));
frame.setVisible(true);
}
}
回答by striving_coder
Well, 3rd parameter is the color in RGB, so it will be black if you set it to 0.
嗯,第三个参数是RGB中的颜色,所以如果你把它设置为0,它就会是黑色的。
回答by Ammar
here is a sample code:
这是一个示例代码:
private int colorToRGB(int alpha, int red, int green, int blue) {
int newPixel = 0;
newPixel += alpha;
newPixel = newPixel << 8;
newPixel += red;
newPixel = newPixel << 8;
newPixel += green;
newPixel = newPixel << 8;
newPixel += blue;
return newPixel;
}
then
然后
image.setRGB(i, j, colorToRGB(alpha, 0, 0, 0))
回答by Phil Freihofner
I use the following form:
我使用以下表格:
int[] pixel = new int[4];
// the following four ints must range 0..255
pixel[0] = redValue;
pixel[1] = greenValue;
pixel[2] = bluleValue;
pixel[3] = alphaValue;
raster.setPixel(x, y, pixel);
To get a raster for a BufferedImage, I do this:
要获得 BufferedImage 的光栅,我这样做:
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
WritableRaster raster = image.getRaster();
I've done some performance testing, and have not found that stuffing all the bytes of color values into a single number to make much of a difference.
我已经做了一些性能测试,并没有发现将颜色值的所有字节填充到一个数字中会产生很大的不同。
It is also good to know the technique where one can draw an opaque image (e.g., RGB rather than an ARGB) with an alpha value.
了解可以使用 alpha 值绘制不透明图像(例如,RGB 而不是 ARGB)的技术也很有好处。
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) (yourAlpha)));
g2d.drawImage(...);