Java BufferedImage Transparent makeTranspBufferedImage(Image image)

Here you can find the source of makeTranspBufferedImage(Image image)

Description

make Transp Buffered Image

License

Open Source License

Declaration

public static BufferedImage makeTranspBufferedImage(Image image) 

Method Source Code

//package com.java2s;
/* /*from  ww  w.ja va2  s .c o m*/
 * Proview   Open Source Process Control.
 * Copyright (C) 2005-2014 SSAB EMEA AB.
 *
 * This file is part of Proview.
 *
 * 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 2 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 Proview. If not, see <http://www.gnu.org/licenses/>
 *
 * Linking Proview statically or dynamically with other modules is
 * making a combined work based on Proview. Thus, the terms and 
 * conditions of the GNU General Public License cover the whole 
 * combination.
 *
 * In addition, as a special exception, the copyright holders of
 * Proview give you permission to, from the build function in the
 * Proview Configurator, combine Proview with modules generated by the
 * Proview PLC Editor to a PLC program, regardless of the license
 * terms of these modules. You may copy and distribute the resulting
 * combined work under the terms of your choice, provided that every 
 * copy of the combined work is accompanied by a complete copy of 
 * the source code of Proview (the version used to produce the 
 * combined work), being distributed under the terms of the GNU 
 * General Public License plus this exception.
 */

import java.awt.*;

import java.awt.image.*;

public class Main {
    private static final Component sComponent = new Component() {
    };
    private static final MediaTracker sTracker = new MediaTracker(
            sComponent);
    private static int sID = 0;

    public static BufferedImage makeTranspBufferedImage(Image image) {
        if (waitForImage(image) == false)
            return null;
        BufferedImage bufferedImage = new BufferedImage(
                image.getWidth(null), image.getHeight(null),
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = bufferedImage.createGraphics();

        g2.drawImage(image, null, null);

        // Make image transparent
        int[] TRANSPARENT_COLOR = { 255, 255, 255, 0 };
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixel = new int[4];
        for (int y = 0; y < bufferedImage.getHeight(); y++)
            for (int x = 0; x < bufferedImage.getWidth(); x++) {
                pixel = raster.getPixel(x, y, pixel);
                if (pixel[0] == TRANSPARENT_COLOR[0]
                        && pixel[1] == TRANSPARENT_COLOR[1]
                        && pixel[2] == TRANSPARENT_COLOR[2]) {
                    raster.setPixel(x, y, TRANSPARENT_COLOR);
                }
            }

        return bufferedImage;
    }

    public static boolean waitForImage(Image image) {
        int id;
        synchronized (sComponent) {
            id = sID++;
        }
        sTracker.addImage(image, id);
        try {
            sTracker.waitForID(id);
        } catch (InterruptedException ie) {
            return false;
        }

        if (sTracker.isErrorID(id))
            return false;
        return true;
    }
}

Related

  1. bordersNonTransparentPixel(BufferedImage data, int wid, int hei, boolean[] traced, int x, int y)
  2. ConvToTransparentImage(BufferedImage src, float alpha)
  3. cutTransparentBorder(BufferedImage src)
  4. fixTransparency(String var0, BufferedImage var1)
  5. makeTransparency(BufferedImage image, int color)
  6. nonTransparentPixels(BufferedImage image)
  7. pickBestTransparency(BufferedImage image)
  8. setTranformImagePixels(BufferedImage origImg, Color paramColor, int colorRangePerc, Vector transPixel, Shape cutoutShape)
  9. transparentBG(BufferedImage image, Boolean transparent)