Java BufferedImage Color Set changeColor(BufferedImage image, Color color, Color replacement_color)

Here you can find the source of changeColor(BufferedImage image, Color color, Color replacement_color)

Description

change Color

License

Apache License

Declaration

public static BufferedImage changeColor(BufferedImage image, Color color, Color replacement_color) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.awt.AlphaComposite;

import java.awt.Color;

import java.awt.Graphics2D;

import java.awt.image.BufferedImage;

public class Main {
    public static BufferedImage changeColor(BufferedImage image, Color color, Color replacement_color) {

        BufferedImage dimg = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = dimg.createGraphics();
        g.setComposite(AlphaComposite.Src);
        g.drawImage(image, null, 0, 0);//w  w w  .ja  v a 2  s . co m
        g.dispose();
        for (int i = 0; i < dimg.getHeight(); i++) {
            for (int j = 0; j < dimg.getWidth(); j++) {
                if (dimg.getRGB(j, i) == color.getRGB()) {
                    dimg.setRGB(j, i, replacement_color.getRGB());
                }
            }
        }
        return dimg;
    }

    public static void drawImage(BufferedImage srcImg, BufferedImage img2Draw, int w, int h) {
        if (w == -1)
            w = (int) (srcImg.getWidth() / 2);
        if (h == -1)
            h = (int) (srcImg.getHeight() / 2);
        System.out.println("AWT Image Wt: " + w + " And Ht: " + h);
        Graphics2D g2 = srcImg.createGraphics();
        g2.drawImage(img2Draw, w, h, null);
        g2.dispose();
    }
}

Related

  1. changeBrightness(BufferedImage image, int offset)
  2. changeColor(BufferedImage image, Color replacement)
  3. changeColor(BufferedImage image, String hexval)
  4. changeRGBSaturation(final BufferedImage image, final double s)
  5. changeToTypeIntRGB(BufferedImage image)