BufferedImage to Opaque - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Color

Description

BufferedImage to Opaque

Demo Code

/*//w ww  . j a v  a 2  s.  c om
 * Image conversion utilities.
 * 
 * Copyright (c) 2006 Jean-Sebastien Senecal (js@drone.ws)
 * 
 * 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
 * this program; if not, write to the Free Software Foundation, Inc., 675 Mass
 * Ave, Cambridge, MA 02139, USA.
 */
//package com.java2s;

import java.awt.Transparency;
import java.awt.image.BufferedImage;

public class Main {
    public static BufferedImage toOpaque(BufferedImage src) {
        int w = src.getWidth();
        int h = src.getHeight();
        BufferedImage dest = new BufferedImage(w, h,
                BufferedImage.TYPE_INT_ARGB);
        int[] alpha = new int[w * h];
        for (int i = 0; i < alpha.length; ++i)
            alpha[i] = Transparency.OPAQUE;
        int buf[] = new int[w];
        for (int y = 0; y < h; y++) {
            src.getRGB(0, y, w, 1, buf, 0, w);
            dest.setRGB(0, y, w, 1, buf, 0, w);
        }
        dest.getAlphaRaster().setPixels(0, 0, w, h, alpha);
        return dest;
    }
}

Related Tutorials