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)