变灰为BufferedImage [英] Graying out a BufferedImage

查看:104
本文介绍了变灰为BufferedImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使BufferedImage变灰(不将其转换为灰度,仅在顶部添加灰色调).现在,我正在通过使用另一张图片,使其半透明,然后将其覆盖在我的原始图片上来进行此操作.这是我现在拥有的代码:

I'm trying to gray out a BufferedImage (not convert it to gray scale, just add a gray tint on top). Right now, I'm doing this by using another image, make it translucent, and then overlay it on top of my original image. This is the code I have for now:

package com.mypkg;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;

import javax.imageio.ImageIO;

import org.imgscalr.Scalr;

public class Overlay {
    public static void main(String args[]){
        URL url = null;
        try{
            //The gray image used for overlay
            url = new URL("https://hoursofidleness.files.wordpress.com/2012/06/gray-card.jpg");
            BufferedImage img1 = ImageIO.read(url);

            //The original image which I want to gray out
            url = new URL("http://www.staywallpaper.com/wp-content/uploads/2016/01/Colorful-Wallpaper-HD-pictures-STAY015.jpg");
            BufferedImage img2 = ImageIO.read(url);
            BufferedImage reImg2 = Scalr.resize(img2, Scalr.Method.BALANCED, Scalr.Mode.FIT_EXACT, 150, 150);

            //Make the gray image, which is used as the overlay, translucent
            BufferedImage transparent = new BufferedImage(img1.getWidth(), img1.getHeight(),BufferedImage.TRANSLUCENT);
            Graphics2D g2d = transparent.createGraphics();
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) 0.50));
            g2d.drawImage(img1, null, 0, 0);
            g2d.dispose();
            BufferedImage reImg1 = Scalr.resize(transparent, Scalr.Method.BALANCED, Scalr.Mode.FIT_EXACT, 150, 150);

            //Merge both images
            BufferedImage result = new BufferedImage(150, 150, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g = result.createGraphics();
            g.drawImage(reImg2, 0, 0, null);
            g.drawImage(reImg1, 0, 0, null);
            g.dispose();
            ImageIO.write(result,"png",new File("/result.png"));
        } catch(Exception e){
            e.printStackTrace();
        }
    }
}

是否有其他方法可以在不使用其他图像进行覆盖的情况下实现这一目标?我可以简单地在原始图像上添加灰色调吗?我尝试了很多其他帖子上看到的建议,但没有一个起作用.

Is there any other way to achieve this without using an additional image for overlay? Can I simply add a gray tint on top of my original image? I've tried many suggestions which I saw on other posts, but none of them work.

谢谢.

推荐答案

您的方法基本上会做什么:

What your method basically does:

  • 加载灰色图像,其中所有像素均具有相同的灰色.
  • 创建新图像.
  • 在该图像上绘制灰色图像,使其半透明.
  • 在要变灰的图像上绘制半透明的灰色图像.

首先,没有必要创建透明图像.您可以在真实图像上直接使用合成绘图.

First, there is no real need to create the transparent image. You can use the composite draw directly over the real image.

第二,全灰色的图像与普通矩形没有什么不同,并且 Graphics2D 类具有 fillRect 方法,该方法绘制了一个填充的矩形,可能很多比绘制图像更快.

Second, an image which is entirely gray is no different than a plain rectangle, and the Graphics2D class has a fillRect method that draws a filled rectangle, probably a lot faster than drawing an image.

因此,在将原始图像加载并缩放为 reImg2 后,您可以使用:

So, after you load and scale your original image into reImg2, you can use:

    Graphics2D g2d = reImg2.createGraphics();
    g2d.setColor(new Color(20,20,20,128));
    g2d.fillRect(0, 0, reImg2.getWidth(), reImg2.getHeight());
    g2d.dispose();

就这样,现在 reImg2 变暗了,您可以将其写入文件了.试一下这些值-将20s更改为较低的值以获得较深的灰色,或将较高的值(最大为255)更改为较浅的灰色.对于较灰暗的图像,将128(50%alpha)更改为较高的值,对于较灰暗的图像,将其更改为较低的值.

That's it, now reImg2 is darkened and you can write it to your file. Play around with the values - change the 20s to lower value for a darker gray or higher value (up to 255) for lighter gray. Change the 128 (50% alpha) to higher value for a more grayed-out image or to lower value for a less grayed-out image.

这篇关于变灰为BufferedImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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