This article shares the specific code for Java to realize the overlay effect display for your reference. The specific content is as follows
import java.awt.AlphaComposite;import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;public class NewImageUtils { /** * * @Title: Construct the image* @Description: Generate a watermark and return java.awt.image.BufferedImage * @param file * Source file (picture) * @param waterFile * Watermark file (picture) * @param x * X offset from the lower right corner* @param y * Y offset from the lower right corner* @param alpha * Transparency, select the value from 0.0~1.0: Completely transparent~ Completely opaque* @return BufferedImage * @throws IOException */ public static BufferedImage watermark(File file, File waterFile, int x, int y, float alpha) throws IOException { // Get the base map BufferedImage buffImg = ImageIO.read(file); // Get layer map BufferedImage waterImg = ImageIO.read(waterFile); // Create Graphics2D object to draw on the basemap object Graphics2D g2d = buffImg.createGraphics(); int waterImgWidth = waterImg.getWidth();// Get the width of the layer map int waterImgHeight = waterImg.getHeight();// Get the height of the layer map // Implement mixing and transparent effects in graphics and images g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); // Draw g2d.drawImage(waterImg, x, y, waterImgWidth, waterImgHeight, null); g2d.dispose();// Release the system resource used in the graphics context return buffImg; } /** * Output watermark image* * @param buffImg * BufferedImage object after image watermark* @param savePath * Save path after image watermark*/ private void generateWaterFile(BufferedImage buffImg, String savePath) { int temp = savePath.lastIndexOf(".") + 1; try { ImageIO.write(buffImg, savePath.substring(temp), new File(savePath)); } catch (IOException e1) { e1.printStackTrace(); } } /** * * @param args * @throws IOException * IO exception is thrown directly * @author bls */ public static void main(String[] args) throws IOException { String sourceFilePath = "D://img//di.png"; String waterFilePath = "D://img//ceng.png"; String saveFilePath = "D://img//new.png"; NewImageUtils newImageUtils = new NewImageUtils(); // Build the overlay layer BufferedImage buffImg = NewImageUtils.watermark(new File(sourceFilePath), new File(waterFilePath), 0, 0, 1.0f); // Output the watermark image newImageUtils.generateWaterFile(buffImg, saveFilePath); }} The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.