文字水印
創(chuàng)新互聯(lián)公司主打移動網(wǎng)站、網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)、網(wǎng)站改版、網(wǎng)絡(luò)推廣、網(wǎng)站維護(hù)、域名申請、等互聯(lián)網(wǎng)信息服務(wù),為各行業(yè)提供服務(wù)。在技術(shù)實(shí)力的保障下,我們?yōu)榭蛻舫兄Z穩(wěn)定,放心的服務(wù),根據(jù)網(wǎng)站的內(nèi)容與功能再決定采用什么樣的設(shè)計(jì)。最后,要實(shí)現(xiàn)符合網(wǎng)站需求的內(nèi)容、功能與設(shè)計(jì),我們還會規(guī)劃穩(wěn)定安全的技術(shù)方案做保障。
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import com.sun.image.codec.jpeg.*;
public class WaterSet {
/**
* 給圖片添加水印
*
* @param filePath
* 需要添加水印的圖片的路徑
* @param markContent
* 水印的文字
* @param markContentColor
* 水印文字的顏色
* @param qualNum
* 圖片質(zhì)量
* @return
*/
public boolean createMark(String filePath, String markContent,
Color markContentColor, float qualNum) {
ImageIcon imgIcon = new ImageIcon(filePath);
Image theImg = imgIcon.getImage();
int width = theImg.getWidth(null);
int height = theImg.getHeight(null);
BufferedImage bimage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = bimage.createGraphics();
g.setColor(markContentColor);
g.setBackground(Color.white);
g.drawImage(theImg, 0, 0, null);
g.drawString(markContent, width / 5, height / 5); // 添加水印的文字和設(shè)置水印文字出現(xiàn)的內(nèi)容
g.dispose();
try {
FileOutputStream out = new FileOutputStream(filePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
param.setQuality(qualNum, true);
encoder.encode(bimage, param);
out.close();
} catch (Exception e) {
return false;
}
return true;
}
}
圖片水印
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 --
* 目標(biāo)文件
* @param x
* --x坐標(biāo)
* @param y
* --y坐標(biāo)
*/
public final static void pressImage(String pressImg, String targetImg,
int x, int y) {
try {
//目標(biāo)文件
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);
//水印文件結(jié)束
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 --
* 目標(biāo)圖片
* @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="";
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);
}
}
package com.jc.ts.services;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
*添加圖片水印的服務(wù)類
* */
public class WaterMark {
/**
* @param sizeContext添加水印文字
* @param request 請求流對象
* @param request 響應(yīng)流對象
* */
@SuppressWarnings("deprecation")
public static void createMarkSize(String sizeContext,HttpServletRequest request,HttpServletResponse response) {
try {
String path=request.getRealPath(request.getServletPath());
FileInputStream in=new FileInputStream(path);
Image src=ImageIO.read(in);
int w=src.getWidth(null);
int h=src.getHeight(null);
BufferedImage img=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);//構(gòu)建畫板
Graphics g=img.getGraphics();//得到畫筆
g.drawImage(src,0,0,w,h,null);//把源圖片寫入畫板
g.setColor(Color.red);
g.drawString(sizeContext,10,5); // 添加文字
g.dispose();//生成圖片
JPEGImageEncoder e=JPEGCodec.createJPEGEncoder(response.getOutputStream());
e.encode(img);
response.getOutputStream().close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ImageFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param localPath 添加水印LOGO路徑
* @param request 請求流對象
* @param request 響應(yīng)流對象
**/
@SuppressWarnings("deprecation")
public static void createMarkLogo(String localPath,HttpServletRequest request,HttpServletResponse response) {
try {
FileInputStream file=new FileInputStream(localPath);
Image fimg=ImageIO.read(file);
int fw=fimg.getWidth(null);
int fh=fimg.getHeight(null);
String path=request.getRealPath(request.getServletPath());
FileInputStream in=new FileInputStream(path);
Image src=ImageIO.read(in);
int w=src.getWidth(null);
int h=src.getHeight(null);
BufferedImage img=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);//構(gòu)建畫板
Graphics g=img.getGraphics();//得到畫筆
g.drawImage(src,0,0,w,h,null);//把原圖片寫入畫板
g.drawImage(fimg,w-20,h-15,fw,fh,null);//把水印圖片寫入畫板
g.dispose();//生成圖片
JPEGImageEncoder e=JPEGCodec.createJPEGEncoder(response.getOutputStream());
e.encode(img);
response.getOutputStream().close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ImageFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* @param localPath 添加水印圖片路徑
* @param request 請求流對象
* @param request 響應(yīng)流對象
* @param width 水印圖片的寬度
* @param height 水印圖片的長度
**/
@SuppressWarnings("deprecation")
public static void createMarkLogo(String localPath,HttpServletRequest request,HttpServletResponse response,int width,int height) {
try {
FileInputStream file=new FileInputStream(localPath);
Image fimg=ImageIO.read(file);
int fw=fimg.getWidth(null);
int fh=fimg.getHeight(null);
String path=request.getRealPath(request.getServletPath());
FileInputStream in=new FileInputStream(path);
Image src=ImageIO.read(in);
int w=src.getWidth(null);//w為你過濾圖片的寬度
int h=src.getHeight(null);//h為你過濾圖片的長度
BufferedImage img=new BufferedImage(w+width,h+height,BufferedImage.TYPE_INT_RGB);//構(gòu)建畫板(畫板的寬度為兩個(gè)圖片之和)
Graphics g=img.getGraphics();//得到畫筆
g.drawImage(src,0,0,w,h,null);//把原圖片寫入畫板
g.drawImage(fimg,width,height,fw,fh,null);//把水印圖片寫入畫板
g.dispose();//生成圖片
JPEGImageEncoder e=JPEGCodec.createJPEGEncoder(response.getOutputStream());
e.encode(img);
response.getOutputStream().close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ImageFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
注意第三個(gè)方法的注釋地方g.drawImage(fimg,width,height,fw,fh,null);根據(jù)參數(shù)你在調(diào)調(diào)(放原圖下面的)
過濾器調(diào)用
package com.jc.ts.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jc.ts.services.WaterMark;
public class WaterFilter implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
HttpServletRequest request=(HttpServletRequest)arg0;
HttpServletResponse response=(HttpServletResponse)arg1;
//WaterMark.createMarkSize("南京ts", request, response);
//WaterMark.createMarkLogo("D:\\workspace\\mybook\\WebRoot\\images\\logo\\book.jpg", request, response);
WaterMark.createMarkLogo("D:\\workspace\\mybook\\WebRoot\\images\\logo\\book.jpg", request, response,20,30);
//注意路徑為絕對路徑且三個(gè)效果不能同時(shí)執(zhí)行
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
web.xml配置(寫在servlet上面)
filter
descriptionThis is the description of my J2EE component/description
display-nameThis is the display name of my J2EE component/display-name
filter-nameWaterFilter/filter-name
filter-classcom.jc.ts.filter.WaterFilter/filter-class
/filter
可以使用Spire.PDF for Java通過Java來添加水印。
首先,您需要在 Java 程序中添加 Spire.Pdf.jar 文件作為依賴項(xiàng)。您可以從這個(gè)鏈接下載 JAR 文件;如果您使用?Maven,則可以通過在 pom.xml 文件中添加以下代碼導(dǎo)入 JAR 文件。
repositories
repository
idcom.e-iceblue/id
url;/url
/repository/repositoriesdependencies
dependency
groupIde-iceblue/groupId
artifactIdspire.pdf/artifactId
version5.3.1/version
/dependency/dependencies
1.添加圖片水印
代碼如下:
import com.spire.pdf.*;
import java.awt.geom.Rectangle2D;
public class watermark {
public static void main(String[] args) {
//加載PDF文檔
PdfDocument doc = new PdfDocument();
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.pdf");
//獲取第一頁
PdfPageBase page = doc.getPages().get(0); ?
//設(shè)置背景圖片
page.setBackgroundImage("C:\\Users\\Administrator\\Desktop\\logo.png");
//設(shè)置背景區(qū)域
Rectangle2D.Float rect = new Rectangle2D.Float();
rect.setRect(280, 300, 150, 150);
page.setBackgroundRegion(rect);
//保存文檔
doc.saveToFile("output/imageWaterMark.pdf");
doc.close();
}
}
2.添加文本水印
代碼如下:
import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.*;
public class Textwatermark {
public static void main(String[] args) {
//創(chuàng)建PdfDocument對象
PdfDocument pdf = new PdfDocument();
//加載示例文檔
pdf.loadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.pdf");
//獲取第一頁
PdfPageBase page = pdf.getPages().get(0);
//調(diào)用insertWatermark方法插入文本水印
insertWatermark(page, "E-ICEBLUE");
//保存文檔
pdf.saveToFile("out/textWaterMark.pdf");
}
static void insertWatermark(PdfPageBase page, String watermark) {
Dimension2D dimension2D = new Dimension();
dimension2D.setSize(page.getCanvas().getClientSize().getWidth() / 2, page.getCanvas().getClientSize().getHeight() / 3);
PdfTilingBrush brush = new PdfTilingBrush(dimension2D);
brush.getGraphics().setTransparency(0.3F);
brush.getGraphics().save();
brush.getGraphics().translateTransform((float) brush.getSize().getWidth() / 2, (float) brush.getSize().getHeight() / 2);
brush.getGraphics().rotateTransform(-45);
brush.getGraphics().drawString(watermark, new PdfFont(PdfFontFamily.Helvetica, 24), PdfBrushes.getViolet(), 0, 0, new PdfStringFormat(PdfTextAlignment.Center));
brush.getGraphics().restore();
brush.getGraphics().setTransparency(1);
Rectangle2D loRect = new Rectangle2D.Float();
loRect.setFrame(new Point2D.Float(0, 0), page.getCanvas().getClientSize());
page.getCanvas().drawRectangle(brush, loRect);
}
}
希望對您有幫助。
目前能夠支持Java語言去操作word文檔的框架很多,什么Jacob,jcom,poi,Java2word等等,但是那一種是支持給word加水印的框架呢?最好是開源的框架。