Java BufferedImage Transparent ApplyTransparency(BufferedImage image, Image mask)

Here you can find the source of ApplyTransparency(BufferedImage image, Image mask)

Description

Apply a mask (alpha channel on a image)

License

Open Source License

Parameter

Parameter Description
image a parameter
mask a parameter

Return

The transformed image

Declaration

public static BufferedImage ApplyTransparency(BufferedImage image, Image mask) 

Method Source Code


//package com.java2s;
/*// ww  w. j a  v  a2s .  co m
 * This file is part of the PdfTrick project.
 * Copyright: (C) 2014
 * Author: Gian Luca Mori
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * For more information, please contact Gian Luca Mori at this
 * address: giansluca@gmail.com
 */

import java.awt.AlphaComposite;
import java.awt.Graphics2D;

import java.awt.Image;

import java.awt.image.BufferedImage;

public class Main {
    /**
     * Apply a mask (alpha channel on a image)
     * @param image
     * @param mask
     * @return The transformed image
     */
    public static BufferedImage ApplyTransparency(BufferedImage image, Image mask) {
        BufferedImage dest = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = dest.createGraphics();
        g2.drawImage(image, 0, 0, null);
        AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.DST_IN, 1.0F);
        g2.setComposite(ac);
        g2.drawImage(mask, 0, 0, null);
        g2.dispose();
        return dest;
    }
}

Related

  1. applyTransparency(BufferedImage src, float alpha)
  2. bordersNonTransparentPixel(BufferedImage data, int wid, int hei, boolean[] traced, int x, int y)
  3. ConvToTransparentImage(BufferedImage src, float alpha)
  4. cutTransparentBorder(BufferedImage src)