Java:使用透明像素填充BufferedImage [英] Java: Filling a BufferedImage with transparent pixels

查看:211
本文介绍了Java:使用透明像素填充BufferedImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个屏幕外的BufferedImage,使用 BufferedImage.TYPE_INT_ARGB 类型构建。它可以包含任何东西,我正在寻找一种方法(相当有效地)用透明像素完全覆盖图像,从而产生隐形图像。

I have an off-screen BufferedImage, constructed with the type BufferedImage.TYPE_INT_ARGB. It can contain anything, and I'm looking for a way to (fairly efficiently) completely overwrite the image with transparent pixels, resulting in an 'invisible' image.

使用类似这样的事情:

    (bufimg.getGraphics()).setColor(new Color(10, 10, 100, 0));   
    (bufimg.getGraphics()).fillRect (0, 0, x, y);

无效。一种可能的方法可能只是写入BufferedImage中的每个像素,但我不确定这是最好的解决方案。你会怎么做?

Has no effect. One possible method might be just to write over every pixel in the BufferedImage, but I'm not sure this is the best solution. How would you do it?

[edit]

图形文档建议不要将clearRect用于屏幕外图像,但我已尝试过与上面相同的结果。

[edit]
The Graphics documentation advises against using clearRect for off-screen images, but I have tried it with the same results as above.

[edit2]

在尝试使用MeBigFatGuy的代码后(谢谢!),它确实清除了图像。但它也停止了对该图像的进一步绘画(或似乎)。此代码例如:

[edit2]
After experimenting with MeBigFatGuy's code (thanks!), it does clear an image. But it also stops further painting to that image (or appears to). This code for example:

    BufferedImage img = new BufferedImage (600, 600, BufferedImage.TYPE_INT_ARGB);
    Graphics g = img.createGraphics ()    
    g.drawLine (100, 100, 500, 500);
    AlphaComposite composite = AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f);
    g.setComposite(composite);
    g.setColor(new Color(0, 0, 0, 0));
    g.fillRect(0, 0, 600, 600);
    graphicsAI.setColor(new Color (10, 10, 10, 255));
    graphicsAI.drawLine (100, 100, 500, 500);

图像上没有任何结果(我正在将图像绘制到JPanel)。这是否与添加alpha值有关?

Results in nothing seen on the image (I'm drawing the image to a JPanel). Is this something to do with the addition of alpha values?

推荐答案

您可以获取基础 int [ ] BufferedImage 的数组(确保使用兼容的格式:即由 int支持的格式[] )。

You could get the underlying int[] array of your BufferedImage (make sure to use a compatible format: that is, one that is backed by an int[]).

然后使用其alpha值的整数填充 int [] 是0(0会做;)

Then fill the int[] with ints whose alpha value are 0 (0 will do ; )

A System.arraycopy 非常快。

你必须知道直接写在 int [] lot 比使用 setRGB 更快。

You have to know that directly writing in the int[] is a lot faster than using setRGB.

现在 BufferedImage 有点黑Java中的艺术:取决于你正在做什么以及你正在做什么平台/ JVM,你可能失去硬件加速(可能从未在那里进行过无论如何,这是第一个)。除此之外,你可能完全不关心硬件加速,因为你可能没有工作,比如说需要60+ FPS才能玩的游戏等。

Now BufferedImage are a bit of a black art in Java: depending on what you're doing and on which platform/JVM you're doing it, you may lose hardware acceleration (which may never have been there in the first place anyway). In addition to that, you may very well not care at all about hardware acceleration anyway because you may not be working on, say, a game requiring 60+ FPS to be playable etc.

这是一个非常复杂的主题,并且有多种方法可以设置 BufferedImage cat。就我而言,我直接在 int [] 中工作,因为我觉得它在像素级别上很糟糕,因为我觉得它比试图更有意义使用更高级别的绘图原语,我真的不关心硬件加速的潜在损失。

This is a very complicated topic and there's more than one way to skin the BufferedImage cat. As far as I'm concerned I work directly in the int[] when I've got to mess at the pixel level because I think it makes much more sense than trying to use higher-level drawing primitives and I do really don't care about the potential lost of hardware acceleration.

这篇关于Java:使用透明像素填充BufferedImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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