简体   繁体   English

java-如何使用setRGB制作图像?

[英]java - how to make an image using setRGB?

i have 2D array to keep color component value : 我有2D数组以保持颜色分量值:

p[pixel_value][red]
p[pixel_value][green]
p[pixel_value][blue]

i just dont know how to use them to make an image. 我只是不知道如何使用它们来制作图像。

i read about setRGB , what i understand is i should mix all of them to become a RGBArray. 我读了有关setRGB的文章 ,我了解的是我应该将所有这些混合在一起成为RGBArray。 then how to make it? 那怎么做呢?

is it any better way to make an image without setRGB ? 没有setRGB可以制作图像吗? i need some explanation. 我需要一些解释。

The method setRGB() can be used to set the color of a pixel of an already existing image. setRGB()方法可用于设置现有图像的像素颜色。 You can open an already existing image and set all the pixels of it, using the values stored in your 2D array. 您可以使用2D数组中存储的值打开一个已经存在的图像并设置其所有像素。 You can do it like this: 您可以这样做:

BufferedImage img = ImageIO.read(new File("image which you want to change the pixels"));
for(int width=0; width < img.getWidth(); width++)
{
    for(int height=0; height < img.getHeight(); height++)
    {
          Color temp = new Color(p[pixel_value][red], p[pixel_value][green], p[pixel_value][blue]);
          img.setRGB(width, height, temp.getRGB());
    }
}
ImageIO.write(img, "jpg", new File("where you want to store this new image"));

Like this, you can iterate over all the pixels and set their color accordingly. 这样,您可以遍历所有像素并相应地设置其颜色。

NOTE: By doing this, you will lose your original image. 注意:这样,您将丢失原始图像。 This is just a way which I know. 这只是我所知道的一种方式。

What you need is a BufferedImage . 您需要的是BufferedImage Create a BufferedImage of type TYPE_3BYTE_BGR , if RGB is what you want, with a specified width and height. 如果要使用RGB,请创建一个TYPE_3BYTE_BGR类型的BufferedImage ,并指定宽度和高度。

QuickFact: 速览:
The BufferedImage subclass describes an Image with an accessible buffer of image data. BufferedImage子类描述了具有图像数据可访问缓冲区的Image。

Then, call the getRaster() method to get a WritableRaster 然后,调用getRaster()方法以获取WritableRaster

QuickFact: 速览:
This class extends Raster to provide pixel writing capabilities. 此类扩展了Raster以提供像素写入功能。

Then, use the setPixel(int x, int y, double[] dArray) method to set the pixels. 然后,使用setPixel(int x, int y, double[] dArray)方法设置像素。

Update: 更新:

If all you want is to read an image, use the ImageIO.read(File f) method. 如果您只想读取图像,请使用ImageIO.read(File f)方法。 It will allow you to read an image file in just one method call. 它使您仅需一个方法调用即可读取图像文件。

Somewhat SSCCE: SSCCE有点:

BufferedImage img = null;
try {
    img = ImageIO.read(new File("strawberry.jpg"));
} catch (IOException e) {
}

You want to manually set RGB values? 您要手动设置RGB值吗?

You need to know that since an int is 32bit it contains all 4 rgb values (1 for the transparency). 您需要知道,由于int是32位的,因此它包含所有4个rgb值(透明性为1)。

xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
^Alpha     ^red    ^green    ^blue

You can accomplish using 4 int values by the use of binary arithmetic: 您可以通过使用二进制算术来使用4个int值:

int rgb = (red << 16) && () (green << 8) & (blue);
bufferedImage.setRGB(x, y, rgb);

IN the above you can add Alpha as well if needed. 在上面,您还可以根据需要添加Alpha You just "push" the binary codes into the right places. 您只需将二进制代码“推送”到正确的位置。

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

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