在Java中将BufferedImage设置为颜色

发布于 2024-08-05 12:13:08 字数 230 浏览 12 评论 0原文

我需要创建一个具有指定背景颜色的矩形 BufferedImage,在背景上绘制一些图案并将其保存到文件中。我不知道如何创建背景。

我正在使用嵌套循环:

BufferedImage b_img = ...
for every row
for every column
setRGB(r,g,b);

但是当图像很大时它非常慢。

如何更有效地设置颜色?

I need to create a rectangular BufferedImage with a specified background color, draw some pattern on the background and save it to file. I don't know how to create the background.

I am using a nested loop:

BufferedImage b_img = ...
for every row
for every column
setRGB(r,g,b);

But it's very slow when the image is large.

How to set the color in a more efficient way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不交电费瞎发啥光 2024-08-12 12:13:08

获取图像的图形对象,将当前绘制设置为所需的颜色,然后调用 fillRect(0,0,width,height)。

BufferedImage b_img = ...
Graphics2D    graphics = b_img.createGraphics();

graphics.setPaint ( new Color ( r, g, b ) );
graphics.fillRect ( 0, 0, b_img.getWidth(), b_img.getHeight() );

Get the graphics object for the image, set the current paint to the desired colour, then call fillRect(0,0,width,height).

BufferedImage b_img = ...
Graphics2D    graphics = b_img.createGraphics();

graphics.setPaint ( new Color ( r, g, b ) );
graphics.fillRect ( 0, 0, b_img.getWidth(), b_img.getHeight() );
嘿咻 2024-08-12 12:13:08

大概是这样的:

BufferedImage image = new BufferedImage(...);
Graphics2D g2d = image.createGraphics();
g2d.setColor(...);
g2d.fillRect(...);

Probably something like:

BufferedImage image = new BufferedImage(...);
Graphics2D g2d = image.createGraphics();
g2d.setColor(...);
g2d.fillRect(...);
川水往事 2024-08-12 12:13:08

使用这个:

BufferedImage bi = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_ARGB);
Graphics2D ig2 = bi.createGraphics();

ig2.setBackground(Color.WHITE);
ig2.clearRect(0, 0, width, height);

Use this:

BufferedImage bi = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_ARGB);
Graphics2D ig2 = bi.createGraphics();

ig2.setBackground(Color.WHITE);
ig2.clearRect(0, 0, width, height);
來不及說愛妳 2024-08-12 12:13:08
BufferedImage image = new BufferedImage(width,height, BufferedImage.TYPE_INT_ARGB);
int[]data=((DataBufferInt) image.getRaster().getDataBuffer()).getData();
Arrays.fill(data,color.getRGB());
BufferedImage image = new BufferedImage(width,height, BufferedImage.TYPE_INT_ARGB);
int[]data=((DataBufferInt) image.getRaster().getDataBuffer()).getData();
Arrays.fill(data,color.getRGB());
睡美人的小仙女 2024-08-12 12:13:08

对于还想将创建的图像保存到文件的人,我已经使用了之前的答案并添加了文件保存部分:

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

// Create the image
BufferedImage bi = new BufferedImage(80, 40, ColorSpace.TYPE_RGB);
Graphics2D graphics = bi.createGraphics();

// Fill the background with gray color
Color rgb = new Color(50, 50, 50);
graphics.setColor (rgb);
graphics.fillRect ( 0, 0, bi.getWidth(), bi.getHeight());

// Save the file in PNG format
File outFile = new File("output.png");
ImageIO.write(bi, "png", outFile);

您还可以将图像保存为其他格式,如 bmp、jpg 等...

For who want also to save the created image to a file, I have used previous answers and added the file saving part:

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

// Create the image
BufferedImage bi = new BufferedImage(80, 40, ColorSpace.TYPE_RGB);
Graphics2D graphics = bi.createGraphics();

// Fill the background with gray color
Color rgb = new Color(50, 50, 50);
graphics.setColor (rgb);
graphics.fillRect ( 0, 0, bi.getWidth(), bi.getHeight());

// Save the file in PNG format
File outFile = new File("output.png");
ImageIO.write(bi, "png", outFile);

You can also save the image in other formats like bmp, jpg, etc...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文