Skip to content

Instantly share code, notes, and snippets.

@Aloento
Last active June 10, 2021 04:15
Show Gist options
  • Save Aloento/8fc78e5428fa268f72e8885ac3662302 to your computer and use it in GitHub Desktop.
Save Aloento/8fc78e5428fa268f72e8885ac3662302 to your computer and use it in GitHub Desktop.
Merge Alpha Mask and RGB Image with Java
package com.QYun.Test;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
public class MergeAlpha {
public static void main(String[] args) {
try {
var image = ImageIO.read(new File("src/main/resources/rgb.png"));
var alpha = ImageIO.read(new File("src/main/resources/alpha.png"));
var skadi = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB_PRE);
for (int h = 0; h < image.getHeight(); h++) {
for (int w = 0; w < image.getWidth(); w++) {
skadi.setRGB(w, h,
(((alpha.getRGB(w, h)) & 0xFF) << 24) |
(image.getRGB(w, h) & 0xFFFFFF));
}
}
ImageIO.write(skadi, "png", new File("skadi.png"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment