Java 給圖片加 文字水印

?

package com.fh.util;

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 javax.imageio.ImageIO;

//import java.io.FileOutputStream;

//import com.sun.image.codec.jpeg.JPEGCodec;

//import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**

* 說明:圖片水印處理類

* 作者:FH Admin

* from:www.fhadmin.org

*/

public class Watermark {

private static String strFWATERM,strIWATERM;

static{

strFWATERM = Tools.readTxtFile(Const.FWATERM); //讀取文字水印配置

strIWATERM = Tools.readTxtFile(Const.IWATERM); //讀取圖片水印配置

}

/**

* 刷新

*/

public static void fushValue(){

strFWATERM = Tools.readTxtFile(Const.FWATERM); //讀取文字水印配置

strIWATERM = Tools.readTxtFile(Const.IWATERM); //讀取圖片水印配置

}

/**

* @param imagePath 圖片全路徑

*/

? public static void setWatemark(String imagePath){

? //文字水印

? if(null != strFWATERM && !"".equals(strFWATERM)){

String strFW[] = strFWATERM.split(",fh,");

if(strFW.length == 5){

if("yes".equals(strFW[0])){

pressText(strFW[1].toString(), imagePath, "", 1, Color.RED,Integer.parseInt(strFW[2]), Integer.parseInt(strFW[3]), Integer.parseInt(strFW[4])); //文字

}

}

}

? //圖片水印

if(null != strIWATERM && !"".equals(strIWATERM)){

String strIW[] = strIWATERM.split(",fh,");

if(strIW.length == 4){

if("yes".equals(strIW[0])){

pressImage(PathUtil.getClasspath() + Const.FILEPATHIMG+strIW[1], imagePath, Integer.parseInt(strIW[2]), Integer.parseInt(strIW[3]));

}

}

}

? }


? ? /**

? ? * 把圖片印刷到圖片上

? ? *

? ? * @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);

? ? ? ? ? ? g.drawImage(src_biao, x, y, wideth_biao, height_biao, null);

? ? ? ? ? ? //水印文件結(jié)束

? ? ? ? ? ? g.dispose();

? ? ? ? ? ? /*FileOutputStream out = new FileOutputStream(targetImg);

? ? ? ? ? ? JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

? ? ? ? ? ? encoder.encode(image);

? ? ? ? ? ? out.close();*/

? ? ? ? ? ? String formatName = targetImg.substring(targetImg.lastIndexOf(".") + 1);

? ? ? ? ? ? ImageIO.write(image, /*"GIF"*/ formatName /* format desired */ , new File(targetImg) /* target */ );


? ? ? ? } 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, Color 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);

? ? ? ? ? ? g.setColor(color);

? ? ? ? ? ? g.setFont(new Font(fontName, fontStyle, fontSize));

? ? ? ? ? ? g.drawString(pressText, x, y);

? ? ? ? ? ? g.dispose();

? ? ? ? ? ? /*FileOutputStream out = new FileOutputStream(targetImg);

? ? ? ? ? ? JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

? ? ? ? ? ? encoder.encode(image);

? ? ? ? ? ? out.close();*/

? ? ? ? ? ? String formatName = targetImg.substring(targetImg.lastIndexOf(".") + 1);

? ? ? ? ? ? ImageIO.write(image, /*"GIF"*/ formatName /* format desired */ , new File(targetImg) /* target */ );

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? System.out.println(e);

? ? ? ? }

? ? }?


}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末饰剥,一起剝皮案震驚了整個濱河市水援,隨后出現(xiàn)的幾起案子趴生,更是在濱河造成了極大的恐慌甲棍,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,427評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件拔妥,死亡現(xiàn)場離奇詭異,居然都是意外死亡达箍,警方通過查閱死者的電腦和手機没龙,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來缎玫,“玉大人硬纤,你說我怎么就攤上這事≡吣ィ” “怎么了筝家?”我有些...
    開封第一講書人閱讀 165,747評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長邻辉。 經(jīng)常有香客問我溪王,道長,這世上最難降的妖魔是什么值骇? 我笑而不...
    開封第一講書人閱讀 58,939評論 1 295
  • 正文 為了忘掉前任莹菱,我火速辦了婚禮,結(jié)果婚禮上吱瘩,老公的妹妹穿的比我還像新娘道伟。我一直安慰自己,他們只是感情好使碾,可當(dāng)我...
    茶點故事閱讀 67,955評論 6 392
  • 文/花漫 我一把揭開白布蜜徽。 她就那樣靜靜地躺著,像睡著了一般票摇。 火紅的嫁衣襯著肌膚如雪拘鞋。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,737評論 1 305
  • 那天矢门,我揣著相機與錄音掐禁,去河邊找鬼怜械。 笑死,一個胖子當(dāng)著我的面吹牛傅事,可吹牛的內(nèi)容都是我干的缕允。 我是一名探鬼主播,決...
    沈念sama閱讀 40,448評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼蹭越,長吁一口氣:“原來是場噩夢啊……” “哼障本!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起响鹃,我...
    開封第一講書人閱讀 39,352評論 0 276
  • 序言:老撾萬榮一對情侶失蹤驾霜,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后买置,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體粪糙,經(jīng)...
    沈念sama閱讀 45,834評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,992評論 3 338
  • 正文 我和宋清朗相戀三年忿项,在試婚紗的時候發(fā)現(xiàn)自己被綠了蓉冈。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,133評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡轩触,死狀恐怖寞酿,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情脱柱,我是刑警寧澤伐弹,帶...
    沈念sama閱讀 35,815評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站榨为,受9級特大地震影響惨好,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜随闺,卻給世界環(huán)境...
    茶點故事閱讀 41,477評論 3 331
  • 文/蒙蒙 一昧狮、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧板壮,春花似錦逗鸣、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至笨使,卻和暖如春卿樱,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背硫椰。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評論 1 272
  • 我被黑心中介騙來泰國打工繁调, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留萨蚕,地道東北人。 一個月前我還...
    沈念sama閱讀 48,398評論 3 373
  • 正文 我出身青樓蹄胰,卻偏偏與公主長得像岳遥,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子裕寨,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,077評論 2 355

推薦閱讀更多精彩內(nèi)容