以前用到的java给图片加水印效果代码都有些小问题,今天我们修正了里面的问题 增加了补白的功能 重构了代码
import java.awt.alphacomposite;
import java.awt.color;
import java.awt.font;
import java.awt.graphics2d;
import java.awt.p_w_picpath;
import java.awt.geom.affinetransform;
import java.awt.p_w_picpath.affinetransformop;
import java.awt.p_w_picpath.bufferedp_w_picpath;
import java.io.file;
import java.io.ioexception;
import javax.p_w_picpathio.p_w_picpathio;
/**
* @author eric xu
*
*/
public final class p_w_picpathutils {
/**
* 图片水印
* @param pressimg 水印图片
* @param targetimg 目标图片
* @param x 修正值 默认在中间
* @param y 修正值 默认在中间
* @param alpha 透明度
*/
public final static void pressp_w_picpath(string pressimg, string targetimg, int x, int y, float alpha) {
try {
file img = new file(targetimg);
p_w_picpath src = p_w_picpathio.read(img);
int wideth = src.getwidth(null);
int height = src.getheight(null);
bufferedp_w_picpath p_w_picpath = new bufferedp_w_picpath(wideth, height, bufferedp_w_picpath.type_int_rgb);
graphics2d g = p_w_picpath.creategraphics();
g.drawp_w_picpath(src, 0, 0, wideth, height, null);
//水印文件
p_w_picpath src_biao = p_w_picpathio.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.drawp_w_picpath(src_biao, (wideth - wideth_biao) / 2, (height - height_biao) / 2, wi deth_biao, height_biao, null);
//水印文件结束
g.dispose();
p_w_picpathio.write((bufferedp_w_picpath) p_w_picpath, "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);
p_w_picpath src = p_w_picpathio.read(img);
int width = src.getwidth(null);
int height = src.getheight(null);
bufferedp_w_picpath p_w_picpath = new bufferedp_w_picpath(width, height, bufferedp_w_picpath.type_int_rgb);
graphics2d g = p_w_picpath.creategraphics();
g.drawp_w_picpath(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();
p_w_picpathio.write((bufferedp_w_picpath) p_w_picpath, "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; //缩放比例 www.3ppt.com
file f = new file(filepath);
bufferedp_w_picpath bi = p_w_picpathio.read(f);
p_w_picpath 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) {
bufferedp_w_picpath p_w_picpath =
new bufferedp_w_picpath(width, height, bufferedp_w_picpath.type_int_rgb);
graphics2d g = p_w_picpath.creategraphics();
g.setcolor(color.white);
g.fillrect(0, 0, width, height);
if (width == itemp.getwidth(null))
g.drawp_w_picpath(itemp, 0, (height - itemp.getheight(null)) / 2, itemp.getwidth(null), itemp.getheight(null), color.white, null);
else
g.drawp_w_picpath(itemp, (width - itemp.getwidth(null)) / 2, 0, itemp.getwidth(null), i temp.getheight(null), color.white, null);
g.dispose();
itemp = p_w_picpath;
}
p_w_picpathio.write((bufferedp_w_picpath) itemp, "jpg", f);
} catch (ioexception e) {
e.printstacktrace();
}
}
public static void main(string[] args) throws ioexception {
pressp_w_picpath("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 这个方法用来得到文字的长度 全角一个字 半角算半个字 但是感觉这种方法不太好