java 生成文字水印图片

有时候为了表明图片来源,或者是为了版权等要对图片加上水印,以表明出处。
本例主要是在一张图片上打上文字水印。
效果图:

[img]http://dl2.iteye.com/upload/attachment/0096/5336/32a9194c-3cc1-32a0-a187-46c6fa603360.jpg[/img]

直接上代码了:

/**
* <br>
* do what you want to do and never stop it.
* <br>
*/
package com.luch.utils;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
* @author Jack
* 2014-4-27
* <br>
*/
public class WaterMark {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
new WaterMark();
}

public WaterMark() throws IOException {
String logoText = "取钱专用章";
Font font = new Font("MS Song", Font.BOLD, 10);
BufferedImage bufferedImage = ImageIO.read(new File("F:\\Tem\\IMG_20130106_163114.jpg"));
BufferedImage bufImge = waterMark(bufferedImage, logoText , font,-20);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufImge, "jpg", baos);
File file = new File("F:\\Tem\\watermark.jpg");
if(file.exists()) {
file.delete();
}
FileOutputStream out = new FileOutputStream(file, true);
out.write(baos.toByteArray());
out.flush();
out.close();
}

/**
* 指定字体进行水印
*
* @param bufferedImage
* @param logoText
* @param font
* @param degree
* @return
*/
public BufferedImage waterMark(BufferedImage bufferedImage, String logoText, Font font, Integer degree) {
if (bufferedImage == null) {
return null;
}
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
int fontSize = (int) Math.sqrt(width * width + height * height) / (logoText.length() / 2 + 5);
font = font.deriveFont(Font.BOLD, fontSize);
double radians = Math.atan(height * 1.0 / width);

Graphics2D graphics = bufferedImage.createGraphics();
// 设置对线段的锯齿状边缘处理
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics.drawImage(bufferedImage, 0, 0, null);
if (null != degree) {
// 设置水印旋转
graphics.translate(width / 2.0, height / 2.0);
graphics.rotate(-radians);// , width + fontSize / 2, 0);
}
// 设置颜色
graphics.setColor(Color.black);
// 设置 Font
graphics.setFont(font);
float alpha = 0.5f;
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
graphics.drawString(logoText, -width / 2, 0);
graphics.dispose();
return bufferedImage;
}

}




顺便也把图片水印的代码放在一起好了。这不部分代码是从网上来的。
效果图:

[img]http://dl2.iteye.com/upload/attachment/0096/5358/1a07e67b-1592-3bed-a40e-4689a500e055.jpg[/img]


/**
* 把图片印刷到图片上
*
* @param pressImg --
* 水印文件
* @param targetImg --
* 目标文件
* @param x
* --x坐标
* @param y
* --y坐标
* @throws IOException
*/
public static void pressImage(String pressImg, String targetImg,
int x, int y) throws IOException{
//目标文件
File file = new File(targetImg);
Image target = ImageIO.read(file);
int height = target.getHeight(null);
int width = target.getWidth(null);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.drawImage(target, 0,0, width, height,null);
//水印文件
File fileMark = new File (pressImg);
Image water = ImageIO.read(fileMark);
int height_water = water.getHeight(null);
int width_water = water.getWidth(null);
System.out.println(height_water + "\t" + width_water);
// 左右\上下\宽\高
g.drawImage(water,x,
y, width/5, height/5, null);
//水印结束
g.dispose();
FileOutputStream fos = new FileOutputStream(targetImg);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
encoder.encode(image);
fos.close();

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值