一: 添加文字水印:
/** * Embeds a textual watermark over a source image to produce * a watermarked one. * @param text The text to be embedded as watermark. * @param sourceImageFile The source image file. * @param destImageFile The output image file. */ static void addTextWatermark(String text, File sourceImageFile, File destImageFile) { try { BufferedImage sourceImage = ImageIO.read(sourceImageFile); Graphics2D g2d = (Graphics2D) sourceImage.getGraphics(); // initializes necessary graphic properties AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f); g2d.setComposite(alphaChannel); g2d.setColor(Color.BLUE); g2d.setFont(new Font("Arial", Font.BOLD, 64)); FontMetrics fontMetrics = g2d.getFontMetrics(); Rectangle2D rect = fontMetrics.getStringBounds(text, g2d); // calculates the coordinate where the String is painted int centerX = (sourceImage.getWidth() - (int) rect.getWidth()) / 2; int centerY = sourceImage.getHeight() / 2; // paints the textual watermark g2d.drawString(text, centerX, centerY); ImageIO.write(sourceImage, "png", destImageFile); g2d.dispose(); System.out.println("The tex watermark is added to the image."); } catch (IOException ex) { System.err.println(ex); } }
二:加图片水印:
/** * Embeds an image watermark over a source image to produce * a watermarked one. * @param watermarkImageFile The image file used as the watermark. * @param sourceImageFile The source image file. * @param destImageFile The output image file. */ static void addImageWatermark(File watermarkImageFile, File sourceImageFile, File destImageFile) { try { BufferedImage sourceImage = ImageIO.read(sourceImageFile); BufferedImage watermarkImage = ImageIO.read(watermarkImageFile); // initializes necessary graphic properties Graphics2D g2d = (Graphics2D) sourceImage.getGraphics(); AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f); g2d.setComposite(alphaChannel); // calculates the coordinate where the image is painted int topLeftX = (sourceImage.getWidth() - watermarkImage.getWidth()) / 2; int topLeftY = (sourceImage.getHeight() - watermarkImage.getHeight()) / 2; // paints the image watermark g2d.drawImage(watermarkImage, topLeftX, topLeftY, null); ImageIO.write(sourceImage, "png", destImageFile); g2d.dispose(); System.out.println("The image watermark is added to the image."); } catch (IOException ex) { System.err.println(ex); } }
相关推荐
java 图片添加水印 图片叠加文字类 图片生成方法,如果需要在已有的图片叠加文字
java给图片添加水印,网站开发过程中可能需要对上传的图片添加属于本网站的水印,这是一个工具类,能实现添加文字水印和添加图片水印。
开发一个项目,需要用到图片加水印的功能,这里考虑使用后台加水印,在上传的时候或者回显到页面时候,取图片文件的流数据,进行加水印在返回流数据就行了,非常简单,不懂可以问我
struts2 上传 java 图片水印 改变图片大小
util实现Java图片水印添加功能,有添加图片水印和文字水印,可以设置水印位置,透明度、设置对线段锯齿状边缘处理、水印图片的路径,水印一般格式是gif,png,这种图片可以设置透明度、水印旋转等,可以参考代码加以...
Java图片加水印,支持旋转和透明度设置.rar
java实现图片加水印 工具类!!!以后不用手动敲代码了
Java为图片添加水印功能,这个水印类的操作比较实用,不但可以旋转水印图像,还可设置其水印图片的透明度,使其与原图融为一体,给图片添加水印,可以设置图片水印的角度,设置对线段的锯齿状边缘处理,第一参数->...
java 水印(水印适应图片大小,但个别图片太小不支持)
Java图片添加水印的模块含源代码
很好的logo水印 utils
基于Java给图片添加文字或图片水印 大学生课程设计 基于Java的课程设计 自己大二写的课程设计
添加文字水印的工具类,自己去下JAR包,MAVEN的网站http://mvnrepository.com/
Java图片加水印,支持旋转和透明度设置.zip
java实现图片上传并添加水印效果(文字水印,蒙版,logo图片),自动进行文字大小行数位置匹配,文字自动换行
java源码:Java图片加水印,支持旋转和透明度设置.zip
图片操作类源代码,该类主要用于给图片进行缩放、加水印和得到图片的高、宽及类型。 具体操作请自己参照方法说明自己测试改写。
java调用ffmpeg添加水印并且转换视频,代码中并没有判断哪些格式支持,那些不支持,mpg,mp4,avi,wmv基本都可以(传入格式),转换为flv avi mp4起码都没问题,其它的没事过ffmpeg是开源的支持的挺多的 补充:时间算错了,...
Java图片加水印,支持旋转和透明度设置.7z