【www.gdgbn.com--js教程】

以前用到的java给图片加水印效果代码都有些小问题,今天我们修正了里面的问题 增加了补白的功能 重构了代码

import java.awt.alphacomposite;
import java.awt.color;
import java.awt.font;
import java.awt.graphics2d;
import java.awt.image;
import java.awt.geom.affinetransform;
import java.awt.image.affinetransformop;
import java.awt.image.bufferedimage;
import java.io.file;
import java.io.ioexception;
import javax.imageio.imageio;
/**
 * @author eric xu
 *
 */
public final class imageutils {
 /**
  * 图片水印
  * @param pressimg 水印图片
  * @param targetimg 目标图片
  * @param x 修正值 默认在中间
  * @param y 修正值 默认在中间
  * @param alpha 透明度
  */
 public final static void pressimage(string pressimg, string targetimg, int x, int y, float alpha) {
  try {
   file img = new file(targetimg);
   image src = imageio.read(img);
   int wideth = src.getwidth(null);
   int height = src.getheight(null);
   bufferedimage image = new bufferedimage(wideth, height, bufferedimage.type_int_rgb);
   graphics2d g = image.creategraphics();
   g.drawimage(src, 0, 0, wideth, height, null);
   //水印文件
   image src_biao = imageio.read(new file(pressimg));
   int wideth_biao = src_biao.getwidth(null);
   int height_biao = src_biao.getheight(null);
   g.setcomposite(alphacomposite.getinstance(alphacomposite.src_atop, alpha));
   g.drawimage(src_biao, (wideth - wideth_biao) / 2, (height - height_biao) / 2, wi             deth_biao, height_biao, null);
   //水印文件结束
   g.dispose();
   imageio.write((bufferedimage) image, "jpg", img);
  } catch (exception e) {
   e.printstacktrace();
  }
 }
 /**
  * 文字水印
  * @param presstext 水印文字
  * @param targetimg 目标图片
  * @param fontname 字体名称
  * @param fontstyle 字体样式
  * @param color 字体颜色
  * @param fontsize 字体大小
  * @param x 修正值
  * @param y 修正值
  * @param alpha 透明度
  */
 public static void presstext(string presstext, string targetimg, string fontname, int fontstyle,    color color, int fontsize, int x, int y, float alpha) {
  try {
   file img = new file(targetimg);
   image src = imageio.read(img);
   int width = src.getwidth(null);
   int height = src.getheight(null);
   bufferedimage image = new bufferedimage(width, height, bufferedimage.type_int_rgb);
   graphics2d g = image.creategraphics();
   g.drawimage(src, 0, 0, width, height, null);
   g.setcolor(color);
   g.setfont(new font(fontname, fontstyle, fontsize));
   g.setcomposite(alphacomposite.getinstance(alphacomposite.src_atop, alpha));
   g.drawstring(presstext, (width - (getlength(presstext) * fontsize)) / 2 + x, (he             ight - fontsize) / 2 + y);
   g.dispose();
   imageio.write((bufferedimage) image, "jpg", img);
  } catch (exception e) {
   e.printstacktrace();
  }
 }
 /**
  * 缩放
  * @param filepath 图片路径
  * @param height 高度
  * @param width 宽度
  * @param bb 比例不对时是否需要补白
  */
 public static void resize(string filepath, int height, int width, boolean bb) {
  try {
   double ratio = 0.0; //缩放比例
   file f = new file(filepath);
   bufferedimage bi = imageio.read(f);
   image itemp = bi.getscaledinstance(width, height, bi.scale_smooth);
   //计算比例
   if ((bi.getheight() > height) || (bi.getwidth() > width)) {
    if (bi.getheight() > bi.getwidth()) {
     ratio = (new integer(height)).doublevalue() / bi.getheight();
    } else {
     ratio = (new integer(width)).doublevalue() / bi.getwidth();
    }
    affinetransformop op = new                affinetransformop(affinetransform.getscaleinstance(ratio, ratio), null);
    itemp = op.filter(bi, null);
   }
   if (bb) {
    bufferedimage image =
                 new bufferedimage(width, height, bufferedimage.type_int_rgb);
    graphics2d g = image.creategraphics();
    g.setcolor(color.white);
    g.fillrect(0, 0, width, height);
    if (width == itemp.getwidth(null))
     g.drawimage(itemp, 0, (height - itemp.getheight(null)) / 2,                   itemp.getwidth(null), itemp.getheight(null), color.white, null);
    else
  g.drawimage(itemp, (width - itemp.getwidth(null)) / 2, 0, itemp.getwidth(null), i                    temp.getheight(null), color.white, null);
    g.dispose();
    itemp = image;
   }
   imageio.write((bufferedimage) itemp, "jpg", f);
  } catch (ioexception e) {
   e.printstacktrace();
  }
 }
 public static void main(string[] args) throws ioexception {
  pressimage("g:imgtestsy.jpg", "g:imgtesttest1.jpg", 0, 0, 0.5f);
  presstext("我是文字水印", "g:imgtesttest1.jpg", "黑体", 36, color.white, 80, 0, 0, 0.3f);
  resize("g:imgtesttest1.jpg", 500, 500, true);
 }
 public static int getlength(string text) {
  int length = 0;
  for (int i = 0; i < text.length(); i++) {
   if (new string(text.charat(i) + "").getbytes().length > 1) {
    length += 2;
   } else {
    length += 1;
   }
  }
  return length / 2;
 }
}


                        getlength 这个方法用来得到文字的长度 全角一个字 半角算半个字 但是感觉这种方法不太好

本文来源:http://www.gdgbn.com/wangyezhizuo/29717/