Java BufferedImage to Transparent getTransperentImage(BufferedImage origImage, BufferedImage compImg)

Here you can find the source of getTransperentImage(BufferedImage origImage, BufferedImage compImg)

Description

get Transperent Image

License

Apache License

Declaration

public static BufferedImage getTransperentImage(BufferedImage origImage, BufferedImage compImg) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.awt.BorderLayout;

import java.awt.Image;

import java.awt.image.BufferedImage;

import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Main {
    public static BufferedImage getTransperentImage(BufferedImage origImage, BufferedImage compImg) {
        int i = origImage.getWidth();
        int j = origImage.getHeight();
        // int[][][] origPixels = BufferedImagetoPixel3DRGB(origImage);
        BufferedImage compTransImg = new BufferedImage(i, j, BufferedImage.TYPE_INT_ARGB);

        for (int k = 0; k < j; k++) {
            for (int m = 0; m < i; m++) {
                int color = origImage.getRGB(m, k);
                int compColor = compImg.getRGB(m, k);
                // The result is also a value ranging from 0 (completely
                // transparent) to 255 (completely opaque).
                int alpha = (color >> 24) & 0xff;
                if (alpha == 0) {
                    byte alpha_byte = 0; // Range will 0 to 255
                    setAlphaValue(compTransImg, k, m, 0.0D);
                } else
                    compTransImg.setRGB(m, k, compColor);
            }/*from  ww w.  jav  a  2s. c  o  m*/
        }
        displayImage(compTransImg, "Test");
        return compTransImg;
    }

    public static void setAlphaValue(BufferedImage paramBufferedImage, int row, int col, double paramDouble) {
        int i = -1;
        try {
            i = paramBufferedImage.getRGB(col, row); // Get the ARGB Color Value
        } catch (Throwable localThrowable) {
            System.out.println("Row: " + row + " Col: " + col);
            return;
        }
        int j = i >> 24 & 0xFF; // Alpha
        int k = i >> 16 & 0xFF; // Red
        int m = i >> 8 & 0xFF; // Green
        int n = i & 0xFF; // blue
        // j = (int)(j * paramDouble);
        j = (int) Math.round(paramDouble * j);

        // i = j << 24 & 0xFF000000 | k << 16 & 0xFF0000 | m << 8 & 0xFF00 | n &
        // 0xFF;
        i = ((j << 24) & 0xFF000000) | ((k << 16) & 0x00FF0000) | ((m << 8) & 0x0000FF00) | ((n) & 0x000000FF);

        // System.out.println("Setting Alpha Value: "+row+"x"+col+" = "+i);
        paramBufferedImage.setRGB(col, row, i);
    }

    public static int setAlphaValue(int argb, double alpha) {
        int a = argb >> 24 & 0xFF; // Alpha
        int r = argb >> 16 & 0xFF; // Red
        int g = argb >> 8 & 0xFF; // Green
        int b = argb & 0xFF; // blue
        a = (int) Math.round(alpha * a);

        // argb = a << 24 & 0xFF000000 | r << 16 & 0xFF0000 | g << 8 & 0xFF00 |
        // b & 0xFF;
        argb = ((a << 24) & 0xFF000000) | ((r << 16) & 0x00FF0000) | ((g << 8) & 0x0000FF00) | ((b) & 0x000000FF);
        return argb;
    }

    public static void displayImage(Image img, String mesg) {
        JDialog imgBox = displayImage(img, mesg, 100, 100, 500, 500);
        imgBox.setVisible(true);
    }

    public static JDialog displayImage(Image img, String mesg, int locx, int locy, int sizex, int sizey) {
        JDialog imgBox = new JDialog(new java.awt.Frame(), mesg, false);
        JPanel imgPanel = new JPanel(new BorderLayout());
        imgBox.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        imgBox.setLocation(locx, locy);
        imgBox.setSize(sizex, sizey);
        imgBox.setContentPane(imgPanel);
        ImageIcon imgIcon = new ImageIcon(img, "");
        JLabel imgLabel = new JLabel(imgIcon);
        imgBox.add(imgLabel, BorderLayout.CENTER);
        return imgBox;
    }

    public static JDialog displayImage(URL resourcePath, String mesg, int locx, int locy, int sizex, int sizey) {
        JDialog imgBox = new JDialog(new java.awt.Frame(), mesg, true);
        JPanel imgPanel = new JPanel(new BorderLayout());
        imgBox.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        imgBox.setLocation(locx, locy);
        imgBox.setSize(sizex, sizey);
        imgBox.setContentPane(imgPanel);
        ImageIcon imgIcon = new ImageIcon(resourcePath, mesg);
        JLabel imgLabel = new JLabel(imgIcon);
        imgBox.add(imgLabel, BorderLayout.CENTER);
        return imgBox;
    }
}

Related

  1. getTransparentImage(BufferedImage i)
  2. makeColorTransparent(BufferedImage im, final Color color)
  3. makeColorTransparent(BufferedImage im, final Color color)
  4. makeColorTransparent(BufferedImage image, Color color)
  5. makeColorTransparent(final BufferedImage im, final Color color)