Java 图片加文字水印和图片水印
1:将字节数组转换为本地图片
/** * 将字节数组转换为本地图片 * @param data * @param path */ public static void byte2image(byte[] data, String path) { if (data.length < 3 || path.equals("")){ return; } try { FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path)); imageOutput.write(data, 0, data.length); imageOutput.close(); System.out.println("生成图片成功:" + path); } catch (Exception ex) { ex.printStackTrace(); } }
2:给图片加图片水印
/** * 为背景图片增加水印图片 * * @param path 水印图片,本地地址,未做网络图片转换 * @param bgPath 背景图片,同上 * @param x 横坐标,左上角0,往右递增 * @param y 纵坐标,左上角0,往下递增 * @param width 水印图片宽度 * @param height 水印图片高度 * @return */ public static byte[] generateImage(String path,String bgPath , int x, int y, int width, int height) { try { BufferedImage watermarkImage = ImageIO.read(new File(path)); BufferedImage bgImage = ImageIO.read(new File(bgPath)); int bgWidth = bgImage.getWidth(null); int bgHeight = bgImage.getHeight(null); BufferedImage image = new BufferedImage(bgWidth, bgHeight, BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); g.drawImage(bgImage, 0, 0, bgWidth, bgHeight, null); width = width == 0 ? watermarkImage.getWidth() : width; height = height == 0 ? watermarkImage.getHeight() : height; g.drawImage(watermarkImage, x, y, width, height, null); g.dispose(); ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write(image, "jpg", out); return out.toByteArray(); } catch (Exception e) { e.printStackTrace(); } return null; }
测试
public static void main(String[] args) { String path = "D:/image/src/main/picture/1218395382.jpg"; String waterPath = "D:/image/src/main/picture/sijiali_s1.jpg"; String imgResult = "D:/image/src/mina/picture/水印.jpg"; byte[] bytes = generateImage(waterPath,path, 250, 10, 80, 100); byte2image(bytes,imgResult); }
效果:
原图
水印图片
结果图片
3:给图片加文字水印
/** * 添加文字水印的方法 * @param pressText 要添加的文字 * @param path 文件路径,这里只处理了了本地文件,未处理网络图片 * @param x 文字在图片的横坐标,左上角为0,往右递增 * @param y 文字在图片的纵坐标,左上角为0,往下递增 * @param fontName 文字的字体名称 * @param fontSize 文字的大小 * @param fontColor 文字的眼神 * @param style 文字的格式,如Font.BOLD++Font.ITALIC表示粗体斜体 * @return */ private static byte[] generateText(String pressText, String path, int x, int y, String fontName, int fontSize, Color fontColor, int style){ try { BufferedImage bgImage= ImageIO.read(new File(path)); int wideth = bgImage.getWidth(null); int height = bgImage.getHeight(null); BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); g.drawImage(bgImage, 0, 0, wideth, height, null); //设置字体大小、颜色等 g.setColor(fontColor); g.setFont(new Font(fontName, style, fontSize)); g.drawString(pressText, x, y); g.dispose(); ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write(image, "jpg", out); return out.toByteArray(); } catch (Exception e) { e.printStackTrace(); } return null; }
测试
public static void main(String[] args) { String path = "D:/image/src/main/picture/1218395382.jpg"; String path2 = "D:/image/src/main/picture/文字水印.jpg"; //Font.BOLD+Font.ITALIC表示加粗且斜体 byte[] bytes = generateText("斯嘉丽", path, 210, 50, "微软雅黑", 30, Color.RED, Font.BOLD+Font.ITALIC); byte2image(bytes,path2); }
结果:
原图
结果图片
至此,图片水印,文字水印完成了!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
2018-11-12 Java基本数据类型转换
2018-11-12 Shiro-Subject 分析