Java Utililty Methods BufferedImage to Transparent

List of utility methods to do BufferedImage to Transparent

Description

The list of methods to do BufferedImage to Transparent are organized into topic(s).

Method

BufferedImagegetTransparentImage(BufferedImage i)
get Transparent Image
if (i == null)
    return null;
final int t = i.getRGB(0, i.getHeight() - 1) & 0x00FFFFFF;
ImageFilter filter = new RGBImageFilter() {
    @Override
    public int filterRGB(int x, int y, int rgb) {
        if ((rgb & 0x00FFFFFF) == t)
            return t;
...
BufferedImagegetTransperentImage(BufferedImage origImage, BufferedImage compImg)
get Transperent Image
int i = origImage.getWidth();
int j = origImage.getHeight();
BufferedImage compTransImg = new BufferedImage(i, j, BufferedImage.TYPE_INT_ARGB);
for (int k = 0; k < j; k++) {
    for (int m = 0; m < i; m++) {
        int color = origImage.getRGB(m, k);
        int compColor = compImg.getRGB(m, k);
        int alpha = (color >> 24) & 0xff;
...
BufferedImagemakeColorTransparent(BufferedImage im, final Color color)
make Color Transparent
RGBImageFilter filter = new RGBImageFilter() {
    public int markerRGB = color.getRGB() | 0xFF000000;
    public final int filterRGB(int x, int y, int rgb) {
        if ((rgb | 0xFF000000) == markerRGB) {
            return 0x00FFFFFF & rgb;
        } else {
            return rgb;
};
ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
Image image = Toolkit.getDefaultToolkit().createImage(ip);
BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null),
        BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = bufferedImage.createGraphics();
g2.drawImage(image, 0, 0, null);
g2.dispose();
return bufferedImage;
ImagemakeColorTransparent(BufferedImage im, final Color color)
make Color Transparent
ImageFilter filter = new RGBImageFilter() {
    public int markerRGB = color.getRGB() | 0xFF000000;
    public final int filterRGB(int x, int y, int rgb) {
        if ((rgb | 0xFF000000) == markerRGB) {
            return 0x00FFFFFF & rgb;
        } else {
            return rgb;
};
ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
return Toolkit.getDefaultToolkit().createImage(ip);
BufferedImagemakeColorTransparent(BufferedImage image, Color color)
make Color Transparent
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);
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()) {
...
ImagemakeColorTransparent(final BufferedImage im, final Color color)
Make provided image transparent wherever color matches the provided color.
final ImageFilter filter = new RGBImageFilter() {
    public int markerRGB = color.getRGB() | 0xFF000000;
    @Override
    public final int filterRGB(final int x, final int y, final int rgb) {
        if ((rgb | 0xFF000000) == markerRGB) {
            return 0x00FFFFFF & rgb;
        return rgb;
...
ImagemakeColorTransparent(final BufferedImage im, final Color colorToMakeTransparent, final int tolerance)
Credits go to http://stackoverflow.com/a/665483 and http://www.logikdev.com/2011/10/05/make-image-backgrounds-transparent-with-tolerance/
final ImageFilter transparencyfilter = new RGBImageFilter() {
    @Override
    public int filterRGB(int x, int y, int rgb) {
        final Color filterColor = new Color(rgb);
        if (colorsAreSimilar(filterColor, colorToMakeTransparent, tolerance)) {
            return 0x00FFFFFF & rgb;
        } else {
            return rgb;
...
ImagemakeColorTransparent(Image im, final Color color)
make Color Transparent
ImageFilter filter = new RGBImageFilter() {
    public int markerRGB = color.getRGB() | 0xFF000000;
    public final int filterRGB(int x, int y, int rgb) {
        if ((rgb | 0xFF000000) == markerRGB) {
            return 0x00FFFFFF & rgb;
        } else {
            return rgb;
};
ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
return Toolkit.getDefaultToolkit().createImage(ip);
ImagemakeColorTransparent(Image image, final Color transparentColor)

Makes the given color in the given Image transparent.

ImageFilter filter = new RGBImageFilter() {
    @Override
    public final int filterRGB(int x, int y, int rgb) {
        if (rgb == transparentColor.getRGB()) {
            return rgb & 0xFFFFFF; 
        return rgb;
};
ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
return Toolkit.getDefaultToolkit().createImage(ip);
BufferedImagemakeTranslucentImage(BufferedImage image, float transparency)
Le aplica la transparencia seleccionada a una BufferedImage
BufferedImage bi = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TRANSLUCENT);
Graphics2D g = bi.createGraphics();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transparency));
g.drawImage(image, null, 0, 0);
g.dispose();
return bi;