当前位置: 移动技术网 > IT编程>开发语言>Java > java实现倾斜水印铺满整张图

java实现倾斜水印铺满整张图

2019年07月22日  | 移动技术网IT编程  | 我要评论

红夜,墙体保温材料,as089.tk

今天遇到一个需求对上传的图铺满水印,在网上找了半天都是在指定位置设置水印,下面代码是我通过在网上找的代码,然后改造而成的。我们先看一下效果图

 

下面讲实现方法:
第一步:使用ps或美图软件设计水印图片,比如:

 

第二步:把下面代码放入类中:

package org.platform.framework.commons.util;

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.inputstream;
import java.io.outputstream;

import javax.imageio.imageio;
import javax.swing.imageicon;

import com.founder.cms.watermark.util.imagemarklogoutil;

/**
 * 
 * 生成水印
 * 
 */
public class imagemarkutil {

 /** 水印透明度 */
 private static float alpha = 0.5f;
 /** 水印图片旋转角度 */
 private static double degree = 0f;
 private static int interval = 0;

 /**
 * 设置水印参数,不设置就使用默认值
 * 
 * @param alpha
 *   水印透明度
 * @param degree
 *   水印图片旋转角度 *
 * @param interval
 *   水印图片间隔
 */
 public static void setimagemarkoptions(float alpha, int degree,
 int interval) {
 if (alpha != 0.0f) {
 imagemarkutil.alpha = alpha;
 }
 if (degree != 0f) {
 imagemarkutil.degree = degree;
 }
 if (interval != 0f) {
 imagemarkutil.interval = interval;
 }

 }

 /**
 * 给图片添加水印图片
 * 
 * @param waterimgpath
 *   水印图片路径
 * @param srcimgpath
 *   源图片路径
 * @param targerpath
 *   目标图片路径
 */
 public static void watermarkbyimg(string waterimgpath, string srcimgpath,
 string targerpath) throws exception {
 watermarkbyimg(waterimgpath, srcimgpath, targerpath, 0);
 }

 /**
 * 给图片添加水印图片
 * 
 * @param waterimgpath
 *   水印图片路径
 * @param srcimgpath
 *   源图片路径
 * @param targerpath
 *   目标图片路径
 */
 public static void watermarkbyimg(string waterimgpath, string srcimgpath) {
 try {
 watermarkbyimg(waterimgpath, srcimgpath, srcimgpath, 0);
 } catch (exception e) {
 // todo auto-generated catch block
 e.printstacktrace();
 }
 }

 /**
 * 给图片添加水印图片、可设置水印图片旋转角度
 * 
 * @param waterimgpath
 *   水印图片路径
 * @param srcimgpath
 *   源图片路径
 * @param targerpath
 *   目标图片路径
 * @param degree
 *   水印图片旋转角度
 */
 public static void watermarkbyimg(string waterimgpath, string srcimgpath,
 string targerpath, double degree) throws exception {
 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);

 // 1、得到画笔对象
 graphics2d g = buffimg.creategraphics();

 // 2、设置对线段的锯齿状边缘处理
 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);
 // 3、设置水印旋转
 if (0 != degree) {
 g.rotate(math.toradians(degree),
  (double) buffimg.getwidth() / 2, (double) buffimg
  .getheight() / 2);
 }

 // 4、水印图片的路径 水印图片一般为gif或者png的,这样可设置透明度
 imageicon imgicon = new imageicon(waterimgpath);

 // 5、得到image对象。
 image img = imgicon.getimage();

 g.setcomposite(alphacomposite.getinstance(alphacomposite.src_atop,
  alpha));

 // 6、水印图片的位置
 for (int height = interval + imgicon.geticonheight(); height < buffimg
  .getheight(); height = height +interval+ imgicon.geticonheight()) {
 for (int weight = interval + imgicon.geticonwidth(); weight < buffimg
  .getwidth(); weight = weight +interval+ imgicon.geticonwidth()) {
  g.drawimage(img, weight - imgicon.geticonwidth(), height
  - imgicon.geticonheight(), null);
 }
 }
 g.setcomposite(alphacomposite.getinstance(alphacomposite.src_over));
 // 7、释放资源
 g.dispose();

 // 8、生成图片
 os = new fileoutputstream(targerpath);
 imageio.write(buffimg, "jpg", os);

 system.out.println("图片完成添加水印图片");

 } catch (exception e) {
 e.printstacktrace();
 } finally {
 try {
 if (null != os)
  os.close();
 } catch (exception e) {
 e.printstacktrace();
 }
 }
 }

 public static void main(string[] args) {

 system.out.println("..添加水印图片开始...");
 /**
 * watermarkpath 水印图片地址 uploadpath 上传成功后文件地址
 */
 //修改默认参数
 //imagemarkutil.setimagemarkoptions(0.0f, 0,
 20);
 //imagemarkutil.watermarkbyimg(watermarkpath, uploadpath);
 system.out.println("..添加水印图片结束...");
 }

}

第三步:在实现上传成功的地方,调用该类,如:

 /**
 * watermarkpath 水印图片地址
 * uploadpath 上传成功后文件地址
 */
 imagemarkutil.watermarkbyimg(watermarkpath, uploadpath);如果你觉得水印间隔太近了,可以通过下面代码设置间隔:

//修改默认参数
 imagemarkutil.setimagemarkoptions(0.0f, 0,
 20);
 imagemarkutil.watermarkbyimg(watermarkpath, uploadpath);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网