java给图片加水印,文字水印
2007-06-19 19:01
375 查看
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
* @param 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 - x, height - height_biao -y, 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);
// String s="www.qhd.com.cn";
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("C:/shuiyin/shuiyin.gif", "c:/shuiyin/DSC02342.JPG", 20 ,20);
}
}
public ImageUtils() ...{
}
public final static String getPressImgPath()...{
return ApplicationContext.getRealPath("/template/data/util/shuiyin.gif");
}
/** *//**
* 把图片印刷到图片上
* @param pressImg -- 水印文件
* @param targetImg -- 目标文件
* @param x
* @param 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 - x, height - height_biao -y, 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);
// String s="www.qhd.com.cn";
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("C:/shuiyin/shuiyin.gif", "c:/shuiyin/DSC02342.JPG", 20 ,20);
}
}
相关文章推荐
- java图片文字水印、图片水印、缩放、补白处理
- java图片处理 文字水印 图片水印 缩放 补白
- Java添加水印+图片水印+文字水印
- java实现缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印等
- 用java实现给图片增加图片水印或者文字水印(也支持视频图像帧添加水印)
- JAVA给图片添加图片水印或文字水印
- javacpp-opencv图像处理之2:实时视频添加图片水印,实现不同大小图片叠加,图像透明度控制,文字和图片双水印
- java程序给指定的图片添加水印文字并完美消除文字锯齿
- java给图片添加图片水印,文字水印,马赛克
- java图片处理 文字水印 图片水印 缩放 补白
- pdf加水印,文字和图片,超链接, java itext, 这里有你想要的一切
- java图片添加水印,包括文字水印和图片水印
- 使用java 给指定图片添加(水印)文字(不需要导入多余jar包)
- java图片处理 (文字水印、图片水印、缩放、补白)
- 使用Java添加图片水印和文字水印
- jsp,java加图片水印,文字水印
- java给图片加水印和文字
- Java实现 图片水印或者文字水印
- Java 实现图片水印之文字水印(SpringMVC + Jsp)
- java给图片添加文字水印并消除锯齿