有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将BuffereImage转换为特定颜色?

特别地,我的图片都是纯黑色的,透明的。我想在绘制图像时为其指定一种辅助颜色,以便将黑色区域更改为新颜色

我尝试使用RGBImageFilter,它只返回我想要的颜色,但出现了一些问题,根本没有绘制任何内容。(ColorFilter扩展了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.....

共 (2) 个答案

  1. # 1 楼答案

    看看AlphaComposites,尤其是DST_:

    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
    
  2. # 2 楼答案

    我不是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());
       }
    
    }