如何将 BufferedImage 转换为某种颜色?

发布于 2024-11-27 05:13:14 字数 528 浏览 1 评论 0原文

具体来说,我的图像都是透明的纯黑色。我想在绘制图像时为图像分配任意颜色,以便黑色区域更改为新颜色。

我尝试使用 RGBImageFilter ,它只返回我想要的颜色,但出了问题并且根本没有绘制任何内容。 (ColourFilter 扩展了 RGBImageFilter 并且仅返回其 filterRGB() 方法中设置的颜色。)

BufferedImage tileImage = _tiles.get( tileID );
ColourFilter cFilt = new ColourFilter();
cFilt.setColour( colour );
FilteredImageSource filteredSrc = new FilteredImageSource( tileImage.getSource(), cFilt );
Image finalImage = Toolkit.getDefaultToolkit().createImage(filteredSrc);
bufferGraphics2D.drawImage(finalImage.....

Speicifically I have images that are all solid black on transparent. I want to assign an abritrary colour to the images when they are drawn so that the black areas are changed to the new colour.

I tried using a RGBImageFilter that just returned the colour I want but something's going wrong and nothing is being drawn at all. (ColourFilter extends RGBImageFilter and just returns the set colour in it's filterRGB() method.)

BufferedImage tileImage = _tiles.get( tileID );
ColourFilter cFilt = new ColourFilter();
cFilt.setColour( colour );
FilteredImageSource filteredSrc = new FilteredImageSource( tileImage.getSource(), cFilt );
Image finalImage = Toolkit.getDefaultToolkit().createImage(filteredSrc);
bufferGraphics2D.drawImage(finalImage.....

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

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

发布评论

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

评论(2

断爱 2024-12-04 05:13:14

查看 AlphaComposites,特别是 DST_IN:

BufferedImage original = ... // dimensions width x height, black on transparent

// create red image
BufferedImage redVersion = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) redVersion.getGraphics();
g.setColor(Color.red);
g.fillRect(0, 0, width, height);

// paint original with composite
g.setComposite(AlphaComposite.DstIn);
g.drawImage(image, 0, 0, width, height, 0, 0, width, height, null);

// redVersion is now a red version of original

Look at AlphaComposites, particularly DST_IN:

BufferedImage original = ... // dimensions width x height, black on transparent

// create red image
BufferedImage redVersion = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) redVersion.getGraphics();
g.setColor(Color.red);
g.fillRect(0, 0, width, height);

// paint original with composite
g.setComposite(AlphaComposite.DstIn);
g.drawImage(image, 0, 0, width, height, 0, 0, width, height, null);

// redVersion is now a red version of original
感悟人生的甜 2024-12-04 05:13:14

我不是 100% 确定你想要做什么,但是图像过滤器应该能够做我认为你想要做的事情。例如,

import java.awt.*;
import java.awt.image.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

public class ColorSwap {
   public static void main(String[] args) {
      final String mapUrlPath = "http://upload.wikimedia.org/"
               + "wikipedia/commons/c/c4/Maps-for-free_Sierra_Nevada.png";

      try {
         URL mapUrl = new URL(mapUrlPath);
         BufferedImage mapImage = ImageIO.read(mapUrl);
         Image newMapImage = Toolkit.getDefaultToolkit().createImage(
                           new FilteredImageSource(mapImage.getSource(),
                                    new XorFilter()));

         Image grayImage = Toolkit.getDefaultToolkit().createImage(
                  new FilteredImageSource(mapImage.getSource(),
                           new MyGrayFilter()));

         Image grayToColorImage = Toolkit.getDefaultToolkit().createImage(
                  new FilteredImageSource(grayImage.getSource(),
                           new GrayToColorFilter(Color.red)));

         ImageIcon mapIcon = new ImageIcon(mapImage);
         ImageIcon newMapIcon = new ImageIcon(newMapImage);
         ImageIcon newGrayIcon = new ImageIcon(grayImage);

         ImageIcon grayToColorIcon = new ImageIcon(grayToColorImage);

         JPanel imagePanel = new JPanel(new GridLayout(2, 2));
         imagePanel.add(new JLabel(mapIcon));
         imagePanel.add(new JLabel(newMapIcon));
         imagePanel.add(new JLabel(newGrayIcon));
         imagePanel.add(new JLabel(grayToColorIcon));

         JOptionPane.showMessageDialog(null, imagePanel, "Image Color Swap",
                  JOptionPane.PLAIN_MESSAGE);

      } catch (MalformedURLException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

class RedBlueSwapFilter extends RGBImageFilter {
   public int filterRGB(int x, int y, int rgb) {
      return ((rgb & 0xff00ff00) | ((rgb & 0xff0000) >> 16) | ((rgb & 0xff) << 16));
   }
}

class XorFilter extends RGBImageFilter {
   public int filterRGB(int x, int y, int argb) {
      return ((argb & 0xff000000) | (argb ^ 0x00ffffff));
   }
}

class MyGrayFilter extends RGBImageFilter {
   public int filterRGB(int x, int y, int argb) {
      int r = (argb & 0x00ff0000) >> 0x10;
      int g = (argb & 0x0000ff00) >> 0x08;
      int b = (argb & 0x000000ff);
      int ave = (r + g + b) / 3;

      return ((argb & 0xff000000) | (ave << 0x10 | ave << 0x08 | ave));
   }
}

class GrayToColorFilter extends RGBImageFilter {
   private Color c;

   public GrayToColorFilter(Color c) {
      this.c = c;
   }

   public int filterRGB(int x, int y, int argb) {
      return (argb | c.getRGB());
   }

}

I'm not 100% sure what you're trying to do, but image filters and should be able to do what I think you are trying to do. For e.g.,

import java.awt.*;
import java.awt.image.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

public class ColorSwap {
   public static void main(String[] args) {
      final String mapUrlPath = "http://upload.wikimedia.org/"
               + "wikipedia/commons/c/c4/Maps-for-free_Sierra_Nevada.png";

      try {
         URL mapUrl = new URL(mapUrlPath);
         BufferedImage mapImage = ImageIO.read(mapUrl);
         Image newMapImage = Toolkit.getDefaultToolkit().createImage(
                           new FilteredImageSource(mapImage.getSource(),
                                    new XorFilter()));

         Image grayImage = Toolkit.getDefaultToolkit().createImage(
                  new FilteredImageSource(mapImage.getSource(),
                           new MyGrayFilter()));

         Image grayToColorImage = Toolkit.getDefaultToolkit().createImage(
                  new FilteredImageSource(grayImage.getSource(),
                           new GrayToColorFilter(Color.red)));

         ImageIcon mapIcon = new ImageIcon(mapImage);
         ImageIcon newMapIcon = new ImageIcon(newMapImage);
         ImageIcon newGrayIcon = new ImageIcon(grayImage);

         ImageIcon grayToColorIcon = new ImageIcon(grayToColorImage);

         JPanel imagePanel = new JPanel(new GridLayout(2, 2));
         imagePanel.add(new JLabel(mapIcon));
         imagePanel.add(new JLabel(newMapIcon));
         imagePanel.add(new JLabel(newGrayIcon));
         imagePanel.add(new JLabel(grayToColorIcon));

         JOptionPane.showMessageDialog(null, imagePanel, "Image Color Swap",
                  JOptionPane.PLAIN_MESSAGE);

      } catch (MalformedURLException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

class RedBlueSwapFilter extends RGBImageFilter {
   public int filterRGB(int x, int y, int rgb) {
      return ((rgb & 0xff00ff00) | ((rgb & 0xff0000) >> 16) | ((rgb & 0xff) << 16));
   }
}

class XorFilter extends RGBImageFilter {
   public int filterRGB(int x, int y, int argb) {
      return ((argb & 0xff000000) | (argb ^ 0x00ffffff));
   }
}

class MyGrayFilter extends RGBImageFilter {
   public int filterRGB(int x, int y, int argb) {
      int r = (argb & 0x00ff0000) >> 0x10;
      int g = (argb & 0x0000ff00) >> 0x08;
      int b = (argb & 0x000000ff);
      int ave = (r + g + b) / 3;

      return ((argb & 0xff000000) | (ave << 0x10 | ave << 0x08 | ave));
   }
}

class GrayToColorFilter extends RGBImageFilter {
   private Color c;

   public GrayToColorFilter(Color c) {
      this.c = c;
   }

   public int filterRGB(int x, int y, int argb) {
      return (argb | c.getRGB());
   }

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