如何更改图像的亮度 [英] How to change the brightness of an Image

查看:184
本文介绍了如何更改图像的亮度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题:我希望能够更改资源图像的亮度,并将其三个实例作为ImageIcons。一个亮度为50%(如此暗),另一个亮度为75%(亮度稍高),最后另一个亮度为100%(与原始图像相同)。我也希望保持透明度。

My Question: I want to be able to change the brightness of a resource image and have three instances of it as ImageIcons. One at 50% brightness (so darker), another at 75% brightness (a little brighter), and finally another at 100% brightness (the same as the original image). I also want to preserve transparency.

我尝试过的事情:我已经四处寻找,看起来最好的解决方案是使用< a href =http://docs.oracle.com/javase/6/docs/api/java/awt/image/RescaleOp.html\"rel =noreferrer> RescaleOp ,但我无法理解。我不知道scaleFactor和偏移是什么。这是我尝试过的代码。

What I've tried: I've searched around and it looks like the best solution is using RescaleOp, but I just can't figure it out. I don't know what the scaleFactor and the offset is all about. Here's my code for what I've tried.

public void initialize(String imageLocation, float regularBrightness, float focusedBrightness, float pressedBrightness, String borderTitle) throws IOException {
  BufferedImage bufferedImage = ImageIO.read(ButtonIcon.class.getResource(imageLocation));
  setRegularIcon(getAlteredImageIcon(bufferedImage, regularBrightness));
  setFocusedIcon(getAlteredImageIcon(bufferedImage, focusedBrightness));
  setPressedIcon(getAlteredImageIcon(bufferedImage, pressedBrightness));
  setTitle(borderTitle);
  init();
}

private ImageIcon getAlteredImageIcon(BufferedImage bufferedImage, float brightness) {
  RescaleOp rescaleOp = new RescaleOp(brightness, 0, null);
  return new ImageIcon(rescaleOp.filter(bufferedImage, null));
}

电话会是这样的:

seeATemplateButton.initialize("/resources/templateIcon-regular.png", 100f, 75f, 50f, "See A Template");
//I think my 100f, 75f, 50f variables need to change, but whenever I change them it behaves unexpectedly (changes colors and stuff).

该代码会发生什么:图片看起来看不见我知道它就在那里,因为它在JLabel上,上面有一个鼠标点击事件,并且工作得很好。如果我只是跳过亮度变化部分并说 setRegularIcon(新的ImageIcon(Button.class.getResource(imageLocation)); 它工作正常,但显然它不是更暗。

What happens with that code: The image appears "invisible" I know it's there because it's on a JLabel with a mouse clicked event on it and that works just fine. If I just skip the brightness changing part and say setRegularIcon(new ImageIcon(Button.class.getResource(imageLocation)); it works just fine, but obviously it's not any darker.

我认为我需要什么:有些帮助了解抵消 scaleFactor 过滤器方法表示/ do,以及为亮度变量提供的数字。

What I think I need: Some help understanding what offset, scaleFactor, and the filter method mean/do, and consequently what numbers to give for the brightness variable.

任何帮助都将不胜感激!谢谢!

Any help would be greatly appreciated! Thanks!

推荐答案

该文件说:

重新缩放操作的伪代码如下:

The pseudo code for the rescaling operation is as follows:

for each pixel from Source object {
    for each band/component of the pixel {
        dstElement = (srcElement*scaleFactor) + offset
    }
}

这只是每个像素的线性变换。该变换的参数是 scaleFactor 关闭设置。如果你想要100%的亮度,这个转换必须是一个标识,即 dstElement = srcElement 。设置 scaleFactor = 1 offset = 0 可以解决问题。

It's just a linear transformation on every pixel. The parameters for that transformation are scaleFactor and offset. If you want 100% brightness, this transform must be an identity, i.e. dstElement = srcElement. Setting scaleFactor = 1 and offset = 0 does the trick.

现在假设你想让图像变暗,像你说的那样亮度为75%。这相当于将像素值乘以0.75。你想要: dstElement = 0.75 * srcElement 。所以设置scaleFactor = 0.75 offset = 0 应该可以解决问题。您的值的问题是它们从0到100,您需要使用0到1之间的值。

Now suppose you want to make the image darker, at 75% brightness like you say. That amounts to multiplying the pixel values by 0.75. You want: dstElement = 0.75 * srcElement. So setting scaleFactor = 0.75 and offset = 0 should do the trick. The problem with your values is that they go from 0 to 100, you need to use values between 0 and 1.

这篇关于如何更改图像的亮度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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