Java添加水印(图片水印,文字水印)
Java添加⽔印(图⽚⽔印,⽂字⽔印)
因为项⽬中考虑到添加图⽚版权的保护,特意看了下⽔印的处理...以下有两种⽅式:
第⼀种是添加⽂字⽔印:
import java.awt.*;
import java.awt.image.*;
import java.io.*;
好听的女生歌曲import javax.swing.*;
import com.dec.jpeg.*;
public class WaterSet {
/
** *//**
* 给图⽚添加⽔印
*
* @param filePath
*            需要添加⽔印的图⽚的路径
* @param markContent
*            ⽔印的⽂字
* @param markContentColor
*            ⽔印⽂字的颜⾊
* @param qualNum
*            图⽚质量
* @return
*/
public boolean createMark(String filePath, String markContent,
冬奥会奖牌榜排名2022Color markContentColor, float qualNum) {
ImageIcon imgIcon = new ImageIcon(filePath);
Image theImg = Image();
int width = Width(null);
int height = Height(null);
BufferedImage bimage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = ateGraphics();
g.setColor(markContentColor);
g.setBackground(Color.white);
g.drawImage(theImg, 0, 0, null);
g.drawString(markContent, width / 5, height / 5); // 添加⽔印的⽂字和设置⽔印⽂字出现的内容
g.dispose();
钱学森的名言
try {
FileOutputStream out = new FileOutputStream(filePath);
JPEGImageEncoder encoder = ateJPEGEncoder(out);
JPEGEncodeParam param = DefaultJPEGEncodeParam(bimage);
param.setQuality(qualNum, true);
out.close();
} catch (Exception e) {
return false;
}
return true;
}
}
第⼆种是添加图⽚⽔印和⽂字⽔印两种⽅法,⽔印图⽚可以是GIF,PNG透明的⽂件,我⼀般采⽤的是PNG的,因为它的质量和GIF相⽐要⾼⼀些:
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.dec.jpeg.JPEGCodec;
import com.dec.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 = ad(_file);
int wideth = Width(null);
int height = Height(null);
BufferedImage image = new BufferedImage(wideth, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = ateGraphics();
g.drawImage(src, 0, 0, wideth, height, null);药厂实习
//⽔印⽂件
File _filebiao = new File(pressImg);
Image src_biao = ad(_filebiao);
int wideth_biao = Width(null);
int height_biao = Height(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 = ateJPEGEncoder(out);            de(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 = ad(_file);
int wideth = Width(null);
int height = Height(null);
BufferedImage image = new BufferedImage(wideth, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = ateGraphics();
g.drawImage(src, 0, 0, wideth, height, null);
// String s="www.qhd";
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 = ateJPEGEncoder(out);            de(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);
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。