图片加水印2

  1. package com.ucmed.wap.home;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics;
  5. import java.awt.Image;
  6. import java.awt.image.BufferedImage;
  7. import java.io.File;
  8. import java.io.FileOutputStream;
  9. import javax.imageio.ImageIO;
  10. import com.sun.image.codec.jpeg.JPEGCodec;
  11. import com.sun.image.codec.jpeg.JPEGImageEncoder;
  12. public final class ImageUtils {
  13. public ImageUtils() {
  14. }
  15. /**//*
  16. * public final static String getPressImgPath() { return ApplicationContext
  17. * .getRealPath("/template/data/util/shuiyin.gif"); }
  18. */
  19. /** *//**
  20. * 把图片印刷到图片上
  21. *
  22. * @param pressImg --
  23. * 水印文件
  24. * @param targetImg --
  25. * 目标文件
  26. * @param x
  27. * --x坐标
  28. * @param y
  29. * --y坐标
  30. */
  31. public final static void pressImage(String pressImg, String targetImg,
  32. int x, int y) {
  33. try {
  34. //目标文件
  35. File _file = new File(targetImg);
  36. Image src = ImageIO.read(_file);
  37. int wideth = src.getWidth(null);
  38. int height = src.getHeight(null);
  39. BufferedImage image = new BufferedImage(wideth, height,
  40. BufferedImage.TYPE_INT_RGB);
  41. Graphics g = image.createGraphics();
  42. g.drawImage(src, 0, 0, wideth, height, null);
  43. //水印文件
  44. File _filebiao = new File(pressImg);
  45. Image src_biao = ImageIO.read(_filebiao);
  46. int wideth_biao = src_biao.getWidth(null);
  47. int height_biao = src_biao.getHeight(null);
  48. g.drawImage(src_biao, x,
  49. y, wideth_biao, height_biao, null);
  50. //水印文件结束
  51. g.dispose();
  52. FileOutputStream out = new FileOutputStream(targetImg);
  53. JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
  54. encoder.encode(image);
  55. out.close();
  56. } catch (Exception e) {
  57. e.printStackTrace();
  58. }
  59. }
  60. /** *//**
  61. * 打印文字水印图片
  62. *
  63. * @param pressText
  64. * --文字
  65. * @param targetImg --
  66. * 目标图片
  67. * @param fontName --
  68. * 字体名
  69. * @param fontStyle --
  70. * 字体样式
  71. * @param color --
  72. * 字体颜色
  73. * @param fontSize --
  74. * 字体大小
  75. * @param x --
  76. * 偏移量
  77. * @param y
  78. */
  79. public static void pressText(String pressText, String targetImg,
  80. String fontName, int fontStyle, int color, int fontSize, int x,
  81. int y) {
  82. try {
  83. File _file = new File(targetImg);
  84. Image src = ImageIO.read(_file);
  85. int wideth = src.getWidth(null);
  86. int height = src.getHeight(null);
  87. BufferedImage image = new BufferedImage(wideth, height,
  88. BufferedImage.TYPE_INT_RGB);
  89. Graphics g = image.createGraphics();
  90. g.drawImage(src, 0, 0, wideth, height, null);
  91. // String s="www.qhd.com.cn";
  92. g.setColor(Color.RED);
  93. g.setFont(new Font(fontName, fontStyle, fontSize));
  94. g.drawString(pressText, x, y);
  95. g.dispose();
  96. FileOutputStream out = new FileOutputStream(targetImg);
  97. JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
  98. encoder.encode(image);
  99. out.close();
  100. } catch (Exception e) {
  101. System.out.println(e);
  102. }
  103. }
  104. public static void main(String[] args) {
  105. pressImage("E:\\work\\temp\\cxzcms\\iconyy1.png", "E:\\work\\temp\\cxzcms\\grzxtop.png", 0, 0);
  106. pressText("你呀", "E:\\work\\temp\\cxzcms\\grzxtop.png",
  107. "黑体", 25, 245, 12, 0,
  108. 50) ;
  109. }
  110. }