0 Daumen
797 Aufrufe

Wie kann man in Java Bilder vollautomatisiert transparent machen?

Avatar von

1 Antwort

+2 Daumen
 
Beste Antwort
Wie kann man in Java Bilder vollautomatisiert transparent machen?

Das geht mit folgendem Programm:

import java.io.File;
import java.util.List;
import java.awt.Color;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.RGBImageFilter;
import java.awt.image.FilteredImageSource;
/**
* Creates transparent images.
*
* @author André Dalwigk
* @version 1.0 (07.07.18)
*
*/
public class TransparentImage {
    /**
    * Main program.
    *
    * @param args
    * @throws Exception
    */
    public static void main(final String... args) throws Exception {
        final List<String> to_convert = get_image_names();
        for (int i = 0; i < to_convert.size(); i++) {
            System.out.println(to_convert.get(i));
            final BufferedImage source = ImageIO.read(new File(to_convert.get(i)));
            ImageIO.write(to_buffered_image(to_transparent(source, new Color(source.getRGB(0, 0)))), "PNG",
                    new File("c." + to_convert.get(i)));
        }
    }
    /**
    * Converts an image to a buffered image.
    *
    * @param image Image to convert to a buffered image.
    * @return Buffered image.
    */
    private static BufferedImage to_buffered_image(final Image image) {
        final BufferedImage buffered_image = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
        buffered_image.createGraphics().drawImage(image, 0, 0, null);
        buffered_image.createGraphics().dispose();
        return buffered_image;
    }
    /**
    * Makes an image transparent.
    *
    * @param image Image to convert.
    * @param to_transparent Color to make transparent.
    * @return Transparent image.
    */
    public static Image to_transparent(final BufferedImage image, final Color to_transparent) {
        return Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), new RGBImageFilter() {
            public final int filterRGB(final int x_pos, final int y_pos, final int rgb) {
                if ((rgb | 0xFF000000) == (to_transparent.getRGB() | 0xFFFFFFFF)) {
                    // Mark the alpha bits as zero - transparent
                    return 0x00FFFFFF & rgb;
                } else {
                    return rgb;
                }
            }
        }));
    }
    /**
    * Returns a list containing all file names.
    *
    * @return List of PNG file names.
    */
    public static List<String> get_image_names() {
        final File directory = new File(System.getProperty("user.dir"));
        final List<String> result = new ArrayList<>();
        final File[] list_of_files = directory.listFiles();
        for (int i = 0; i < list_of_files.length; i++) {
            // select all PNG files.
            if (list_of_files[i].isFile() && list_of_files[i].getName().contains(".png")) {
                result.add(list_of_files[i].getName());
            }
        }
        return result;
    }
}

Avatar von

Ein anderes Problem?

Stell deine Frage

Ähnliche Fragen

+1 Daumen
1 Antwort
0 Daumen
1 Antwort
Gefragt 9 Okt 2018 von Gast
0 Daumen
1 Antwort

Willkommen bei der Stacklounge! Stell deine Frage einfach und kostenlos

x
Made by a lovely community