Here you can find the source of transparentBG(BufferedImage image, Boolean transparent)
Parameter | Description |
---|---|
image | The original image |
transparent | True in case we want the output image to have transparent background or false if we want it white |
public static BufferedImage transparentBG(BufferedImage image, Boolean transparent)
//package com.java2s; /**//w w w .ja v a2 s . com * monimix - monochrome image mix: it combines monochrome images to one * Copyright (C) 2012 Periklis Ntanasis <pntanasis@gmail.com> * * 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/>. */ import java.awt.Color; import java.awt.image.BufferedImage; public class Main { /** * Returns the same image with transparent background instead of white or * vice versa. Creates a temp image to surpass the transparency issues. * * @param image The original image * @param transparent True in case we want the output image to have * transparent background or false if we want it white * @return The converted image */ public static BufferedImage transparentBG(BufferedImage image, Boolean transparent) { BufferedImage tmpImg = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_4BYTE_ABGR); for (int w = 0; w < image.getWidth(); w++) { for (int h = 0; h < image.getHeight(); h++) { if (image.getRGB(w, h) == Color.WHITE.getRGB() && transparent) { tmpImg.setRGB(w, h, 0); } else if (image.getRGB(w, h) == 0 && !transparent) { tmpImg.setRGB(w, h, Color.WHITE.getRGB()); } else { tmpImg.setRGB(w, h, image.getRGB(w, h)); } } } return tmpImg; } }