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给图片添加水印,网站开发过程中可能需要对上传的图片添加属于本网站的水印,这是一个工具类,能实现添加文字水印和添加图片水印。
JSP过滤器方式实现无损坏给图片添加水印
1.VB写的小程序给图片增加水印,默认把图片拍摄日期作为水印;
微信小程序用Canvas给图片加水印,拼接图片,制作名片PHelper-master.zip
Qt编写的给图片加水印的工具,选择图片所在文件夹,即可批量自动给图片加水印,加水印过程中没有用多线程,会卡住界面。但适合自己批量加水印的小工具。
Flex_给图片加水印,Flex_给图片加水印。Flex_给图片加水印
给图片添加水印 很多网站要的图片有都带有水印,这一有个例子大家可看参考一下
可经批量给图片加水印 方便,快捷,易操作
在Java中给图片添加水印是一种常见的保护图片的方法,特别是在需要防止图片被盗用或者滥用的情况下。为了实现这一功能,Java提供了多种方式,其中常用的是使用ImageIO和Apache的Commons Imaging库。 ImageIO是Java...
在线图片加水印,canvas加水印,在线给图片加水印、照片上添加文字,通过前端canvas给你的证件加上水印,防止证件被盗。 演示地址:http://watermark.dxcweb.com/
按键精灵手机版IOS,图片加水印源码,可实现给图片加水印。把水印图片加到主图片里面。暂时只支持IOS系统。
包括了C#给图片添加水印需要用到的类库,以及详细的代码
pb给图片加水印的方法 PowerBuilder 9.0代码
应为项目需要,给图片加图片水印,查阅了许多资料都不理想,最后使用ImageEN 5.2.0 FullSource控件解决,效果比较满意。 案例使用delphi7+ImageEN 5.2.0 FullSource制作,里面有ImageEN控件的安装方法。
自己开发的处理图片 压缩 等比例缩放 加水印文字 加水印图片 自定义加水印的位置。很不错的哦,需要本机有.net framework4.0环境支持。平时有时候需要用到图片的处理这个就不错。