「java给图片加水印」java给图片加水印文字方式
今天给各位分享java给图片加水印的知识,其中也会对java给图片加水印文字方式进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
java(最好jsp)给图片加水印
Java给图片加水印
/**
* 方法描述:b给图片增加水印./b/br
* 备 注: 在图片上写字符串
* 创 建 人: bo.gaobo/br
* 创建日期: 2012-09-07/br
* @param originalUrl 原始图片存储路径
* @param oldImg 原图片
* @param str 增加的字符串
* @param xLocation x坐标
* @param yLocation y坐标
* @param fontColor 颜色
* @param fontSize 字号
* @param typeFace 字体
* @param fileType 文件类型
*/
public static BufferedImage addStringToImg(String originalUrl, BufferedImage oldImg,String str,int xLocation,int yLocation, Color fontColor, int fontSize, String typeFace, String fileType) throws IOException{
FileOutputStream output = new FileOutputStream(originalUrl);
BufferedImage buffImg = oldImg;
Graphics2D g = buffImg.createGraphics();
g = buffImg.createGraphics();
g.drawImage(buffImg, null, 0, 0);
g.setColor(fontColor); //设置字体颜色
g.setFont(new Font(typeFace, Font.PLAIN, fontSize)); //设置字体和字号
g.drawString(str, xLocation, yLocation); //把字符串放在对应的坐标处
g.dispose();
ImageIO.write(buffImg, fileType, output); //设置文件类型
output.close();
return buffImg;
}
java怎么给word文档加水印
可以使用Free Spire.Doc for Java在word文档中添加文本水印或图片水印。Free Spire.Doc for Java下载链接:网页链接
1.添加文本水印——代码如下:
import com.spire.doc.*;
import com.spire.doc.documents.WatermarkLayout;
import java.awt.*;
public class WordTextWatermark {
public static void main(String[] args) {
Document document = new Document();
document.loadFromFile("Sample.docx");
insertTextWatermark(document.getSections().get(0));
}
private static void insertTextWatermark(Section section) {
TextWatermark txtWatermark = new TextWatermark();
txtWatermark.setText("内部使用");
txtWatermark.setFontSize(40);
txtWatermark.setColor(Color.red);
txtWatermark.setLayout(WatermarkLayout.Diagonal);
section.getDocument().setWatermark(txtWatermark);
}
}
2.添加图片水印——代码如下:
import com.spire.doc.*;
public class WordImageWatermark {
public static void main(String[] args) throws Exception{
Document document = new Document();
document.loadFromFile("Sample.docx");
PictureWatermark picture = new PictureWatermark();
picture.setPicture("logo.png");
picture.setScaling(5);
picture.isWashout(false);
document.setWatermark(picture);
document.saveToFile("out/result2.docx",FileFormat.Docx )
}
}
java中打印的pdf怎么添加图片水印
添加水印可以参考使用控件来添加的方法,如下:
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);
//设置背景图片
page.setBackgroundImage("C:\\Users\\Administrator\\Desktop\\logo.png");
//设置背景区域
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.PdfImage;
import com.spire.pdf.graphics.PdfTilingBrush;
import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Rectangle2D;
public class AddBackground {
public static void main(String[] args) {
//创建PdfDocument对象,并加载PDF测试文档
PdfDocument pdf = new PdfDocument();
pdf.loadFromFile("test.pdf");
//遍历文档每一页,加载图片,并设置成平铺背景(水印)
for (int i = 0; i pdf.getPages().getCount();i++)
{
PdfPageBase page = pdf.getPages().get(i);
Dimension2D dimension2D = new Dimension();
dimension2D.setSize(page.getCanvas().getSize().getWidth()/4, page.getCanvas().getSize().getHeight()/3);
PdfTilingBrush brush = new PdfTilingBrush(dimension2D);
brush.getGraphics().setTransparency(0.2f);
brush.getGraphics().translateTransform(brush.getSize().getWidth()/10,brush.getSize().getHeight()/10);
brush.getGraphics().rotateTransform(30);
PdfImage image = PdfImage.fromImage("logo.png");
brush.getGraphics().drawImage(image,brush.getSize().getWidth()-image.getWidth()/2,(brush.getSize().getHeight())/2);
Rectangle2D rectangle2D = new Rectangle2D.Float();
rectangle2D.setFrame(new Point(0,0),page.getCanvas().getClientSize());
page.getCanvas().drawRectangle(brush,rectangle2D);
}
//保存文档
pdf.saveToFile("SetTiledBackground.pdf");
pdf.dispose();
}
}
注:这里使用的是free Spire.Pdf.jar(以上代码参考自文章1、文章2)
Java上传图片到OSS怎么添加水印?
首先,图片上的水印图片只能使用当前存储空间内的图片,如果没有,需要先传到当前空间内。
其次,水印图片的格式仅支持png,jpg,webp三种。
java里面上传水印,可以使用提供的sdk里面的watermark方法,这个函数有5个参数,分别是t,g,x,y,voffset.其中第一个参数表示透明度,其它参数表示位置。
当然了,它还可以指定水印文字,具体可以参考阿里云官方提供的文档,代码示例可以去github上找到对应操作的代码。
具体代码如下
// add watermark into the image
style = "image/watermark,text_SGVsbG8g5Zu-54mH5pyN5YqhIQ";
request = new GetObjectRequest(bucketName, key);
request.setProcess(style);
java给图片加水印的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java给图片加水印文字方式、java给图片加水印的信息别忘了在本站进行查找喔。