當前位置:首頁 » 文件管理 » java上傳圖片水印

java上傳圖片水印

發布時間: 2022-08-28 05:55:37

㈠ 求java圖片批量上傳,改小,加水印處理代碼,50分相送

//添加水印,filePath 源圖片路徑, watermark 水印圖片路徑
public static boolean createMark(String filePath,String watermark) {
ImageIcon imgIcon=new ImageIcon(filePath);
Image theImg =imgIcon.getImage();
ImageIcon waterIcon=new ImageIcon(watermark);
Image waterImg =waterIcon.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(Color.red);
g.setBackground(Color.white);
g.drawImage(theImg, 0, 0, null );
g.drawImage(waterImg, 100, 100, null );
g.drawString("12233",10,10); //添加文字
g.dispose();
try{
FileOutputStream out=new FileOutputStream(filePath);
JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
param.setQuality(50f, true);
encoder.encode(bimage, param);
out.close();
}catch(Exception e){ return false; }
return true;
}

㈡ 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 給圖片加文字水印

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;
}

㈤ 在JSP中怎麼給圖片加水印如題 謝謝了

加水印: 這是一個用Photoshop的圖層模式規則重復水印的方法,效果挺不錯,而且也不復雜。天極設計在線特別提醒:本例的關鍵操作是水印取樣和圖層模式的運用。 1、用Photoshop打開要去掉水印的圖片。 2、按快捷鍵M 切換到選擇工具。 3、按著滑鼠拖動選擇要去掉的水印。 4、按快捷鍵 Ctrl+J 把第三步選擇的水印建立一個新的圖層。 5、移動新建的圖層可見水印圖層 6、再選擇圖層模式為顏色減淡。 7、再按快捷鍵 Ctrl+i 把圖層改為反相狀態 8、移動水印圖層對准底圖上的水印即可完成 9、復制水印圖層復蓋底圖的水印就可去除所有水印 去水印: 選擇仿製圖章工具 ,並在選項欄中執行下列操作: 選取畫筆和設置畫筆選項。(請參閱使用畫筆。) 指定混合模式、不透明度和流量。(請參閱設置繪畫和編輯工具的選項。) 確定想要對齊樣本像素的方式。如果選擇「對齊的」,您可以松開滑鼠按鈕,當前的取樣點不會丟失。這樣,無論您多少次停止和繼續繪畫,都可以連續應用樣本像素。如果取消選擇「對齊的」,則每次停止和繼續繪畫時,都將從初始取樣點開始應用樣本像素。 選擇「用於所有圖層」可以從所有可視圖層對數據進行取樣;取消選擇「用於所有圖層」將只從現用圖層取樣。 通過在任意打開的圖像中定位指針,然後按住 Alt 鍵並點按 (Windows) 或按住 Option 鍵並點按 (Mac OS) 來設置取樣點。 注釋:如果要從一幅圖像中取樣並應用到另一圖像,則這兩幅圖像的顏色模式必須相同。 在圖像中拖移。 圖案圖章工具使您可以用圖案繪畫。可以從圖案庫中選擇圖案或者創建您自己的圖案。

㈥ 用java給圖片加水印的問題

很明顯是你自己在圖片加水印時沒有任何限制的加水印

一般情況下,加一次,然後記住本次操作,用資料庫或者什麼其他的,下次訪問該圖片時,就不要再加了。

你給它穿了n多的衣服它不變色才怪

㈦ 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怎麼給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文件加水印

可以使用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圖片水印的一些問題,請仔細看下問題補充。

你直接讀取壓縮後的文件流根本不是圖片格式,而是經過壓縮處理的二進制,Image類識別不了,所以為空,而如果你讀完每個壓縮文件後寫成單獨的圖片即是完整的解壓縮的圖片文件,這個時候可以進行Image的相關操作。

熱點內容
嵌入式系統高級c語言編程 發布:2024-10-13 09:16:26 瀏覽:84
天刀與伺服器斷開是什麼鬼 發布:2024-10-13 09:12:12 瀏覽:71
python金融量化 發布:2024-10-13 09:12:11 瀏覽:82
搭建hive需要什麼伺服器 發布:2024-10-13 09:07:16 瀏覽:398
c靜態成員函數的訪問 發布:2024-10-13 09:03:08 瀏覽:528
伺服器怎麼固定ip 發布:2024-10-13 09:03:08 瀏覽:902
伺服器ip大全 發布:2024-10-13 08:26:17 瀏覽:150
安卓手機相冊照片在哪個文件 發布:2024-10-13 08:25:35 瀏覽:772
如何升級安卓系統51 發布:2024-10-13 08:24:59 瀏覽:636
ftp命令下載文件更新時間不變 發布:2024-10-13 08:15:48 瀏覽:134