Java BufferedImage to Transparent makeColorTransparent(Image im, final Color color)

Here you can find the source of makeColorTransparent(Image im, final Color color)

Description

make Color Transparent

License

Apache License

Declaration

public static Image makeColorTransparent(Image im, final Color color) 

Method Source Code

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

import java.awt.Color;

import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;

import java.awt.Toolkit;
import java.awt.Transparency;

import java.awt.image.BufferedImage;

import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;

import java.awt.image.RGBImageFilter;

public class Main {
    public static Image makeColorTransparent(Image im, final Color color) {
        ImageFilter filter = new RGBImageFilter() {
            // the color we are looking for... Alpha bits are set to opaque
            public int markerRGB = color.getRGB() | 0xFF000000;

            public final int filterRGB(int x, int y, int rgb) {
                if ((rgb | 0xFF000000) == markerRGB) {
                    // Mark the alpha bits as zero - transparent
                    return 0x00FFFFFF & rgb;
                } else {
                    // nothing to do
                    return rgb;
                }// w w w .  j  ava2  s.c o m
            }
        };

        ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
        return Toolkit.getDefaultToolkit().createImage(ip);
    }

    public static BufferedImage createImage(int imgWt, int imgHt, boolean hasAlpha) {
        return createImage(imgWt, imgHt, hasAlpha, false);
    }

    public static BufferedImage createImage(int imgWt, int imgHt, boolean hasAlpha, boolean translucent) {
        // Create a buffered image with a format that's compatible with the
        // screen
        BufferedImage bimage = null;
        try {
            // Determine the type of transparency of the new buffered image
            int transparency = Transparency.OPAQUE;
            if (hasAlpha)
                transparency = Transparency.BITMASK;
            if (translucent)
                transparency = Transparency.TRANSLUCENT;
            // Create the buffered image
            GraphicsConfiguration gc = getDefaultConfiguration();
            bimage = gc.createCompatibleImage(imgWt, imgHt, transparency);
            // RGPTLogger.logToFile("Created Image using GraphicsConfiguration");
        } catch (HeadlessException e) {
            e.printStackTrace();
        }

        if (bimage == null) {
            // Create a buffered image using the default color model
            int type = BufferedImage.TYPE_INT_RGB;
            if (hasAlpha) {
                type = BufferedImage.TYPE_INT_ARGB;
            }
            bimage = new BufferedImage(imgWt, imgHt, type);
        }
        return bimage;
    }

    public static GraphicsConfiguration getDefaultConfiguration() {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        return gd.getDefaultConfiguration();
    }
}

Related

  1. makeColorTransparent(BufferedImage im, final Color color)
  2. makeColorTransparent(BufferedImage im, final Color color)
  3. makeColorTransparent(BufferedImage image, Color color)
  4. makeColorTransparent(final BufferedImage im, final Color color)
  5. makeColorTransparent(final BufferedImage im, final Color colorToMakeTransparent, final int tolerance)
  6. makeColorTransparent(Image image, final Color transparentColor)
  7. makeTranslucentImage(BufferedImage image, float transparency)
  8. makeTransparent(BufferedImage image, int x, int y)
  9. makeTransparent(BufferedImage img, Color trColor)