下面是编程之家 jb51.cc 通过网络收集整理的代码片段。
编程之家小编现在分享给大家,也给大家做个参考。
在原图之上再画一张图片(drawImage)或者文本文字(drawString),Java SE中提供了 java.awt.image包和javax.imageio包可以实现非常方便的实现 图片水印或者文字水印。
import java.awt.AlphaComposite; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import javax.imageio.ImageIO; import javax.swing.ImageIcon; /** * 生成图片水印 * @author Ricky Fung * */ public class WaterMarkIconTest { /** * @param args */ public static void main(String[] args) { File srcImageFile = new File("D:/test/myblog.png"); File logoImageFile = new File("D:/test/logo.png"); File outputimageFile = new File("D:/test/myblog_water_icon.jpg"); File outputRoateImageFile = new File("D:/test/myblog_water_icon_rotate.jpg"); createWaterMarkByIcon(srcImageFile,logoImageFile,outputimageFile); createWaterMarkByIcon(srcImageFile,outputRoateImageFile,45); } public static void createWaterMarkByIcon(File srcImageFile,File logoImageFile,File outputimageFile) { createWaterMarkByIcon(srcImageFile,outputimageFile,0); } public static void createWaterMarkByIcon(File srcImageFile,File outputimageFile,double degree) { OutputStream os = null; try { Image srcImg = ImageIO.read(srcImageFile); BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),srcImg.getHeight(null),BufferedImage.TYPE_INT_RGB); Graphics2D g = buffImg.createGraphics(); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage( srcImg.getScaledInstance(srcImg.getWidth(null),Image.SCALE_SMOOTH),null); ImageIcon logoImgIcon = new ImageIcon(ImageIO.read(logoImageFile)); Image logoImg = logoImgIcon.getimage(); //旋转 if (degree>0) { g.rotate(Math.toradians(degree),(double) buffImg.getWidth() / 2,(double) buffImg.getWidth() / 2); } float alpha = 0.8f; // 透明度 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha)); //水印 的位置 g.drawImage(logoImg,buffImg.getWidth()/3,buffImg.getHeight()/2,null); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); g.dispose(); os = new FileOutputStream(outputimageFile); // 生成图片 ImageIO.write(buffImg,"JPG",os); } catch (Exception e) { e.printstacktrace(); } finally { try { if (null != os) os.close(); } catch (Exception e) { e.printstacktrace(); } } } }
import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import javax.imageio.ImageIO; /** * 生成文字水印 * @author Ricky Fung * */ public class WaterMarkTextTest { /** * @param args */ public static void main(String[] args) { File srcImgFile = new File("D:/test/myblog.png"); String logoText = "[ 水印文字 电话邦 ]"; File outputimageFile = new File("D:/test/myblog_water_text.jpg"); File outputRotateImageFile = new File("D:/test/myblog_water_text_rotate.jpg"); createWaterMarkByText(srcImgFile,logoText,outputimageFile); createWaterMarkByText(srcImgFile,outputRotateImageFile,45); } public static void createWaterMarkByText(File srcImgFile,String logoText,File outputimageFile) { createWaterMarkByText(srcImgFile,0); } public static void createWaterMarkByText(File srcImgFile,double degree) { OutputStream os = null; try { Image srcImg = ImageIO.read(srcImgFile); BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),null); if (degree>0) { g.rotate(Math.toradians(degree),(double) buffImg.getHeight() / 2); } g.setColor(Color.RED); g.setFont(new Font("宋体",Font.BOLD,36)); float alpha = 0.8f; g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha)); g.drawString(logoText,buffImg.getHeight()/2); g.dispose(); os = new FileOutputStream(outputimageFile); // 生成图片 ImageIO.write(buffImg,os); } catch (Exception e) { e.printstacktrace(); } finally { try { if (null != os) os.close(); } catch (Exception e) { e.printstacktrace(); } } } }
以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。