图片叠加效果Java代码实现
程序员文章站
2022-06-19 23:40:57
本文实例为大家分享了java实现图片叠加效果展示的具体代码,供大家参考,具体内容如下
import java.awt.alphacomposite;
impor...
本文实例为大家分享了java实现图片叠加效果展示的具体代码,供大家参考,具体内容如下
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: 构造图片 * @description: 生成水印并返回java.awt.image.bufferedimage * @param file * 源文件(图片) * @param waterfile * 水印文件(图片) * @param x * 距离右下角的x偏移量 * @param y * 距离右下角的y偏移量 * @param alpha * 透明度, 选择值从0.0~1.0: 完全透明~完全不透明 * @return bufferedimage * @throws ioexception */ public static bufferedimage watermark(file file, file waterfile, int x, int y, float alpha) throws ioexception { // 获取底图 bufferedimage buffimg = imageio.read(file); // 获取层图 bufferedimage waterimg = imageio.read(waterfile); // 创建graphics2d对象,用在底图对象上绘图 graphics2d g2d = buffimg.creategraphics(); int waterimgwidth = waterimg.getwidth();// 获取层图的宽度 int waterimgheight = waterimg.getheight();// 获取层图的高度 // 在图形和图像中实现混合和透明效果 g2d.setcomposite(alphacomposite.getinstance(alphacomposite.src_atop, alpha)); // 绘制 g2d.drawimage(waterimg, x, y, waterimgwidth, waterimgheight, null); g2d.dispose();// 释放图形上下文使用的系统资源 return buffimg; } /** * 输出水印图片 * * @param buffimg * 图像加水印之后的bufferedimage对象 * @param savepath * 图像加水印之后的保存路径 */ 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异常直接抛出了 * @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(); // 构建叠加层 bufferedimage buffimg = newimageutils.watermark(new file(sourcefilepath), new file(waterfilepath), 0, 0, 1.0f); // 输出水印图片 newimageutils.generatewaterfile(buffimg, savefilepath); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。