欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java图片添加水印实例代码分享

程序员文章站 2024-03-07 23:57:52
本文为大家介绍了java图片添加水印实例代码,java实现水印还是非常方便的,水印可以是图片或者文字,具体内容如下 package michael.io.imag...

本文为大家介绍了java图片添加水印实例代码,java实现水印还是非常方便的,水印可以是图片或者文字,具体内容如下

package michael.io.image; 
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.fileinputstream; 
import java.io.fileoutputstream; 
import java.io.inputstream; 
import java.io.outputstream; 
 
import javax.imageio.imageio; 
import javax.swing.imageicon; 
 
import com.sun.image.codec.jpeg.jpegcodec; 
import com.sun.image.codec.jpeg.jpegimagedecoder; 
import com.sun.image.codec.jpeg.jpegimageencoder; 
 
/** 
 * 图片水印 
 * @blog http://sjsky.iteye.com 
 * @author michael 
 */ 
public class imagemarklogobyicon { 
 
 /** 
  * @param args 
  */ 
 public static void main(string[] args) { 
  string srcimgpath = "d:/test/michael/myblog_01.png"; 
  string iconpath = "d:/test/michael/blog_logo.png"; 
  string targerpath = "d:/test/michael/img_mark_icon.jpg"; 
  string targerpath2 = "d:/test/michael/img_mark_icon_rotate.jpg"; 
  // 给图片添加水印 
  imagemarklogobyicon.markimagebyicon(iconpath, srcimgpath, targerpath); 
  // 给图片添加水印,水印旋转-45 
  imagemarklogobyicon.markimagebyicon(iconpath, srcimgpath, targerpath2, 
    -45); 
 
 } 
 
 /** 
  * 给图片添加水印 
  * @param iconpath 水印图片路径 
  * @param srcimgpath 源图片路径 
  * @param targerpath 目标图片路径 
  */ 
 public static void markimagebyicon(string iconpath, string srcimgpath, 
   string targerpath) { 
  markimagebyicon(iconpath, srcimgpath, targerpath, null); 
 } 
 
 /** 
  * 给图片添加水印、可设置水印图片旋转角度 
  * @param iconpath 水印图片路径 
  * @param srcimgpath 源图片路径 
  * @param targerpath 目标图片路径 
  * @param degree 水印图片旋转角度 
  */ 
 public static void markimagebyicon(string iconpath, string srcimgpath, 
   string targerpath, integer degree) { 
  outputstream os = null; 
  try { 
   image srcimg = imageio.read(new file(srcimgpath)); 
 
   bufferedimage buffimg = new bufferedimage(srcimg.getwidth(null), 
     srcimg.getheight(null), bufferedimage.type_int_rgb); 
 
   // 得到画笔对象 
   // graphics g= buffimg.getgraphics(); 
   graphics2d g = buffimg.creategraphics(); 
 
   // 设置对线段的锯齿状边缘处理 
   g.setrenderinghint(renderinghints.key_interpolation, 
     renderinghints.value_interpolation_bilinear); 
 
   g.drawimage(srcimg.getscaledinstance(srcimg.getwidth(null), srcimg 
     .getheight(null), image.scale_smooth), 0, 0, null); 
 
   if (null != degree) { 
    // 设置水印旋转 
    g.rotate(math.toradians(degree), 
      (double) buffimg.getwidth() / 2, (double) buffimg 
        .getheight() / 2); 
   } 
 
   // 水印图象的路径 水印一般为gif或者png的,这样可设置透明度 
   imageicon imgicon = new imageicon(iconpath); 
 
   // 得到image对象。 
   image img = imgicon.getimage(); 
 
   float alpha = 0.5f; // 透明度 
   g.setcomposite(alphacomposite.getinstance(alphacomposite.src_atop, 
     alpha)); 
 
   // 表示水印图片的位置 
   g.drawimage(img, 150, 300, null); 
 
   g.setcomposite(alphacomposite.getinstance(alphacomposite.src_over)); 
 
   g.dispose(); 
 
   os = new fileoutputstream(targerpath); 
 
   // 生成图片 
   imageio.write(buffimg, "jpg", os); 
 
   system.out.println("图片完成添加icon印章。。。。。。"); 
  } catch (exception e) { 
   e.printstacktrace(); 
  } finally { 
   try { 
    if (null != os) 
     os.close(); 
   } catch (exception e) { 
    e.printstacktrace(); 
   } 
  } 
 } 
} 

再为大家分享一例:

import java.awt.color;
import java.awt.font;
import java.awt.graphics;
import java.awt.image;
import java.awt.image.bufferedimage;
import java.io.file;
import java.io.fileoutputstream;
import javax.imageio.imageio;
import com.sun.image.codec.jpeg.jpegcodec;
import com.sun.image.codec.jpeg.jpegimageencoder;
 
public final class imageutils {
 public imageutils() {
 
 }
 
 /**
  * public final static string getpressimgpath() { return applicationcontext
  * .getrealpath("/template/data/util/shuiyin.gif"); }
  */
 
 /**
  * 把图片印刷到图片上
  * 
  * @param pressimg --
  *   水印文件
  * @param targetimg --
  *   目标文件
  * @param x
  *   --x坐标
  * @param y
  *   --y坐标
  */
 public final static void pressimage(string pressimg, string targetimg,
   int x, int y) {
  try {
   //目标文件
   file _file = new file(targetimg);
   image src = imageio.read(_file);
   int wideth = src.getwidth(null);
   int height = src.getheight(null);
   bufferedimage image = new bufferedimage(wideth, height,
     bufferedimage.type_int_rgb);
   graphics g = image.creategraphics();
   g.drawimage(src, 0, 0, wideth, height, null);
 
   //水印文件
   file _filebiao = new file(pressimg);
   image src_biao = imageio.read(_filebiao);
   int wideth_biao = src_biao.getwidth(null);
   int height_biao = src_biao.getheight(null);
   g.drawimage(src_biao, (wideth - wideth_biao) / 2,
     (height - height_biao) / 2, wideth_biao, height_biao, null);
   //水印文件结束
   g.dispose();
   fileoutputstream out = new fileoutputstream(targetimg);
   jpegimageencoder encoder = jpegcodec.createjpegencoder(out);
   encoder.encode(image);
   out.close();
  } catch (exception e) {
   e.printstacktrace();
  }
 }
 
 /**
  * 打印文字水印图片
  * 
  * @param presstext
  *   --文字
  * @param targetimg --
  *   目标图片
  * @param fontname --
  *   字体名
  * @param fontstyle --
  *   字体样式
  * @param color --
  *   字体颜色
  * @param fontsize --
  *   字体大小
  * @param x --
  *   偏移量
  * @param y
  */
 
 public static void presstext(string presstext, string targetimg,
   string fontname, int fontstyle, int color, int fontsize, int x,
   int y) {
  try {
   file _file = new file(targetimg);
   image src = imageio.read(_file);
   int wideth = src.getwidth(null);
   int height = src.getheight(null);
   bufferedimage image = new bufferedimage(wideth, height,
     bufferedimage.type_int_rgb);
   graphics g = image.creategraphics();
   g.drawimage(src, 0, 0, wideth, height, null);
    
   g.setcolor(color.red);
   g.setfont(new font(fontname, fontstyle, fontsize));
 
   g.drawstring(presstext, wideth - fontsize - x, height - fontsize
     / 2 - y);
   g.dispose();
   fileoutputstream out = new fileoutputstream(targetimg);
   jpegimageencoder encoder = jpegcodec.createjpegencoder(out);
   encoder.encode(image);
   out.close();
  } catch (exception e) {
   system.out.println(e);
  }
 }
 
 public static void main(string[] args) {
  pressimage("f:/logo.png",   "f:/123.jpg", 0, 0);
 }
}

希望本文所述对大家用java语言实现图片添加水印有所帮助。