图片上加水印
来源:互联网 发布:程序员学习排名 编辑:程序博客网 时间:2024/06/03 02:10
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 class WatermarkUtil {
/**
* 把图片印刷到图片上
* @param pressImg -- 水印文件
* @param targetImg -- 目标文件
* @param x -- x偏移量
* @param y -- y偏移量
*/
public final static void pressImage(String pressImg, String targetImg,
int x, int y) {
FileOutputStream out = null;
try {
File targetFile = new File(targetImg);
Image imgTarget = ImageIO.read(targetFile);
int width = imgTarget.getWidth(null);
int height = imgTarget.getHeight(null);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(imgTarget, 0, 0, width, height, null);
// 水印文件
File pressFile = new File(pressImg);
Image imgPress = ImageIO.read(pressFile);
int widthPress = imgPress.getWidth(null);
int heightPress = imgPress.getHeight(null);
g.drawImage(imgPress, width - widthPress - x, height - heightPress - y, widthPress, heightPress, null);
g.dispose();
out = new FileOutputStream(targetImg);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) try{out.close();} catch(Exception e){};
}
}
/**
* 打印文字水印图片
*
* @param pressText -- 文字
* @param targetImg -- 目标图片
* @param fontName -- 字体名
* @param fontStyle -- 字体样式
* @param color -- 字体颜色
* @param fontSize -- 字体大小
* @param x -- x偏移量
* @param y -- y偏移量
*/
public static void pressText(String pressText, String targetImg,
String fontName, int fontStyle, int color, int fontSize, int x, int y) {
FileOutputStream out = null;
try {
File targetFile = new File(targetImg);
Image imgTarget = ImageIO.read(targetFile);
int width = imgTarget.getWidth(null);
int height = imgTarget.getHeight(null);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(imgTarget, 0, 0, width, height, null);
g.setColor(Color.RED);
g.setFont(new Font(fontName, fontStyle, fontSize));
g.drawString(pressText, width - fontSize - x, height - fontSize / 2 - y);
g.dispose();
out = new FileOutputStream(targetImg);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) try{out.close();} catch(Exception e){};
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
pressText("蒙奇•D•路飞", "d:/EDM.jpg", "宋体", Font.ITALIC, 0xFF0000, 16, 200, 10);
//pressImage("d:/createShopMarker.png", "d:/EDM.jpg", 200, 10);
}
}
- 图片上加水印
- 在图片上加水印
- java程序向图片上加水印
- java程序向图片上加水印
- php将文字写在图片上,图片加水印
- jsp加图片水印
- 图片加水印类
- 图片加水印
- 图片加水印类
- ASP图片加水印
- 图片加水印
- 给图片加水印
- 图片加水印
- 图片加水印
- C# 图片加水印
- 给图片加水印
- 图片加水印
- .net图片加水印
- PE文件输入表获取过程
- 使用ADO.NET Entity Framework 4.1进行Code First模式的开发
- tableView 学习笔记
- oracle参数deprecated
- use file list
- 图片上加水印
- memcached设置
- oracle parallel execution example
- Linux JDK 版本识别
- 用Oracle并行查询发挥多CPU的威力
- 优化SQL语句的一些规则
- lcd 中的 DE mode 和 Sync mode 是什么机制?两者有何区别 ?
- Android平台下FreePascal的交叉编译
- 我的网易博客