java水印图片
‘壹’ java 给图片加文字水印
java给图片加水印,在网上有很多资料,但我想要一个能自适应图片大小,将水印加在图片中间或右下角,这个问题我觉得应该是一个算法的问题
得根据图片大小调整水印文字的字体大小,及显示缩进比例,谁有现成的麻烦分享一份,如果不方便在这里说,可以发到我邮箱:
别人都想去掉水印,题主你怎么还想加水印?
‘贰’ 用Java给jpg图片加文字水印,加的水印盖住了原来的图片,怎么办。
jpg文件上的水印的清除方法:
如果需要将带水印的JPG转换成05H的PDG:
1、 将PDG批量更名为JPG。如果下载的时候就已经是JPG,则此步省略。
2、用ComicEnhancer Pro打开带水印的JPG,色彩选“单色”,水印没了吧?不过这个时候文字多半也会变得很细,可以通过增加“Gamma校正”值,或用“曲线”来加黑。注意“Gamma校正”和“曲线”选一个足矣。调节好以后,转换成TIFF。
3、将TIFF文件更名为PDG,并且符合PDG文件命名规范,然后用高版本DjVuToy的“PDG压缩”功能转换成05H的PDG。注意转换的时候把“转换为快速版”选项去掉。
如果不需要转换成PDG,而是希望在去掉水印的同时尽可能保持清晰:
1、将PDG批量更名为JPG。如果下载的时候就已经是JPG,则此步省略。
2、用ComicEnhancer Pro打开带水印的JPG,将“高亮度”设置为125,看到那神奇的效果了吗?如果希望对文字的影响尽可能小,还可以尝试将“高亮值”设置为210。
3、下面就看你高兴了,可以直接存为JPG,也可以在色彩选“16级灰度”、“8级灰度”、“4级灰度”,然后转换成PNG。灰度级数越少,图像损失越多,文件越小,16级灰度基本上肉眼看不出文字部分有任何损失,4级灰度则很明显,可以结合“曲线”或“Gamma校正”等加以改善。
‘叁’ Java图片处理 文字水印 图片水印 缩放 补白
package util;
import java awt AlphaComposite;
import java awt Color;
import java awt Font;
import java awt Graphics D;
import java awt Image;
import java awt geom AffineTransform;
import java awt image AffineTransformOp;
import java awt image BufferedImage;
import java io File;
import java io IOException;
import javax imageio ImageIO;
/**
* @author Eric Xu
*
*/
public final class ImageUtils {
/**
* 图片水印
* @param pressImg 水印图片
* @param targetImg 目标图片
* @param x 修正值 默认在中间
* @param y 修正值 默认在中间
* @param alpha 透明度
*/
public final static void pressImage(String pressImg String targetImg int x int y float alpha) {
try {
File img = new File(targetImg);
Image src = ImageIO read(img);
int wideth = src getWidth(null);
int height = src getHeight(null);
BufferedImage image = new BufferedImage(wideth height BufferedImage TYPE_INT_RGB);
Graphics D g = image createGraphics();
g drawImage(src wideth height null);
//水印文件
Image src_biao = ImageIO read(new File(pressImg));
int wideth_biao = src_biao getWidth(null);
int height_biao = src_biao getHeight(null);
g setComposite(AlphaComposite getInstance(AlphaComposite SRC_ATOP alpha));
g drawImage(src_biao (wideth wideth_biao) / (height height_biao) / wideth_biao height_biao null);
//水印文件结束
g dispose();
ImageIO write((BufferedImage) image jpg img);
} catch (Exception e) {
e printStackTrace();
}
}
/**
* 文字水印
* @param pressText 水印文字
* @param targetImg 目标图片
* @param fontName 字体名称
* @param fontStyle 字体样式
* @param color 字体颜色
* @param fontSize 字体大小
* @param x 修正值
* @param y 修正值
* @param alpha 透明度
*/
public static void pressText(String pressText String targetImg String fontName int fontStyle Color color int fontSize int x int y float alpha) {
try {
File img = new File(targetImg);
Image src = ImageIO read(img);
int width = src getWidth(null);
int height = src getHeight(null);
BufferedImage image = new BufferedImage(width height BufferedImage TYPE_INT_RGB);
Graphics D g = image createGraphics();
g drawImage(src width height null);
g setColor(color);
g setFont(new Font(fontName fontStyle fontSize));
g setComposite(AlphaComposite getInstance(AlphaComposite SRC_ATOP alpha));
g drawString(pressText (width (getLength(pressText) * fontSize)) / + x (height fontSize) / + y);
g dispose();
ImageIO write((BufferedImage) image jpg img);
} catch (Exception e) {
e printStackTrace();
}
}
/**
* 缩放
* @param filePath 图片路径
* @param height 高度
* @param width 宽度
* @param bb 比例不对时是否需要补白
*/
public static void resize(String filePath int height int width boolean bb) {
try {
double ratio = ; //缩放比例
File f = new File(filePath);
BufferedImage bi = ImageIO read(f);
Image itemp = bi getScaledInstance(width height bi SCALE_SMOOTH);
//计算比例
if ((bi getHeight() > height) || (bi getWidth() > width)) {
if (bi getHeight() > bi getWidth()) {
ratio = (new Integer(height)) doubleValue() / bi getHeight();
} else {
ratio = (new Integer(width)) doubleValue() / bi getWidth();
}
AffineTransformOp op = new AffineTransformOp(AffineTransform getScaleInstance(ratio ratio) null);
itemp = op filter(bi null);
}
if (bb) {
BufferedImage image = new BufferedImage(width height BufferedImage TYPE_INT_RGB);
Graphics D g = image createGraphics();
g setColor(Color white);
g fillRect( width height);
if (width == itemp getWidth(null))
g drawImage(itemp (height itemp getHeight(null)) / itemp getWidth(null) itemp getHeight(null) Color white null);
else
g drawImage(itemp (width itemp getWidth(null)) / itemp getWidth(null) itemp getHeight(null) Color white null);
g dispose();
itemp = image;
}
ImageIO write((BufferedImage) itemp jpg f);
} catch (IOException e) {
e printStackTrace();
}
}
public static void main(String[] args) throws IOException {
pressImage( D:\shuiyin png D:\test jpg f);
pressText( 我是文字水印 D:\test jpg 微软雅黑 Color white f);
resize( D:\test jpg true);
}
public static int getLength(String text) {
int length = ;
for (int i = ; i < text length(); i++) {
if (new String(text charAt(i) + ) getBytes() length > ) {
length += ;
} else {
length += ;
}
}
return length / ;
}
lishixin/Article/program/Java/hx/201311/26874
‘肆’ Java中给图片添加水印图片,怎么让水印图片透明度为百分之50
http://hi..com/hardneedl/blog/item/cbd99b0e4d5f12ec37d122c1.html
‘伍’ 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图片加水印代码 最好有实例!!!先谢了!!
文字水印
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
* 图片质量
* @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); // 添加水印的文字和设置水印文字出现的内容
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 --
* 目标文件
* @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 = 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);
//水印文件结束
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 --
* 目标图片
* @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="www.qhd.com.cn";
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);
}
}
‘柒’ java给tif格式图片加文字水印
packagecom.coderli.image;
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.Image;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.FileOutputStream;
importjavax.imageio.ImageIO;
importcom.sun.image.codec.jpeg.JPEGCodec;
importcom.sun.image.codec.jpeg.JPEGImageEncoder;
@SuppressWarnings("restriction")
publicfinalclassImageUtils{
publicImageUtils(){
}
/**
*打印文字水印图片
*
*@parampressText
*--文字
*@paramtargetImg--
*目标图片
*@paramfontName--
*字体名
*@paramfontStyle--
*字体样式
*@paramcolor--
*字体颜色
*@paramfontSize--
*字体大小
*@paramx--
*偏移量
*@paramy
*/
publicstaticvoidpressText(StringpressText,StringtargetImg,
StringfontName,intfontStyle,Colorcolor,intfontSize,intx,
inty){
try{
File_file=newFile(targetImg);
Imagesrc=ImageIO.read(_file);
intwidth=src.getWidth(null);
intheight=src.getHeight(null);
BufferedImageimage=newBufferedImage(width,height,
BufferedImage.TYPE_INT_RGB);
Graphicsg=image.createGraphics();
g.drawImage(src,0,0,width,height,null);
g.setColor(color);
g.setFont(newFont(fontName,fontStyle,fontSize));
g.drawString(pressText,width-fontSize-x,height-fontSize
/2-y);
g.dispose();
FileOutputStreamout=newFileOutputStream(targetImg);
JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
}catch(Exceptione){
System.out.println(e);
}
}
publicstaticvoidmain(String[]args){
pressText("bbs.coderli.com","f:/1.tiff","TimesNewRomas",Font.PLAIN,Color.BLUE,22,150,20);
}
}
这个方法里用的api是支持tiff格式的,你可以试试。
‘捌’ 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 如何给pdf文件加水印
可以使用Spire.PDF for Java通过Java来添加水印。
首先,您需要在 Java 程序中添加 Spire.Pdf.jar 文件作为依赖项。您可以从这个链接下载 JAR 文件;如果您使用Maven,则可以通过在 pom.xml 文件中添加以下代码导入 JAR 文件。
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository></repositories><dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf</artifactId>
<version>5.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);
//设置背景图片
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.*;
import java.awt.*;
import java.awt.geom.*;
public class Textwatermark {
public static void main(String[] args) {
//创建PdfDocument对象
PdfDocument pdf = new PdfDocument();
//加载示例文档
pdf.loadFromFile("C:\Users\Administrator\Desktop\Sample.pdf");
//获取第一页
PdfPageBase page = pdf.getPages().get(0);
//调用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中打印的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)