原始圖片
要加的浮水印
製作好的圖片
原始碼
import java.io.IOException;
import java.awt.Graphics;
import java.awt.*;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ImageProducer;
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;
import com.sun.jimi.core.Jimi;
import com.sun.jimi.core.JimiException;
import com.sun.jimi.core.JimiWriter;
import com.sun.jimi.core.options.JPGOptions;
/**
* java做浮水印,字型和圖片,調節透明度.
*
* @author myjava_024,老紫竹 JAVA世紀網(java2000.net)
*
*/
public class T {
/**
* 將任何格式的圖片轉換成JPG圖片 source原圖片路徑 dest 轉換後圖片的路徑
*/
public void toReduce(String source, String dest) {
try {
JPGOptions options = new JPGOptions();
options.setQuality(100);
// 圖片格式在jimi的解碼格式範圍內
ImageProducer image = Jimi.getImageProducer(source);
// 圖片編碼
JimiWriter writer = Jimi.createJimiWriter(dest);
writer.setSource(image);
// 對輸出檔案的屬性進行相關的設定,每種格式的屬性都不一樣
writer.setOptions(options);
writer.putImage(dest);
} catch (JimiException je) {
System.err.println("Error: " + je);
je.printStackTrace();
}
}
/**
* 實現對圖片的抽取縮減並控制產生圖的大小 oldfile原圖片路徑 newfile縮減後的 圖片路徑 wideths 縮減後圖片的寬
* heights縮減後圖片的高
*/
public void changDimension(String oldfile, String newfile, int wideths,
int heights) throws JimiException, IOException {
String temp = newfile;
File inputfile = null;
this.toReduce(oldfile, temp);
// 讀入檔案
inputfile = new File(temp);
// 構造Image對象
Image src = ImageIO.read(inputfile);
// double wideth = (double) src.getWidth(null); // 得到源圖寬
// double height = (double) src.getHeight(null); // 得到源圖長
int iWideth = wideths;
int iHeight = heights;
BufferedImage tag = null;
try {
tag = new BufferedImage(iWideth, iHeight, BufferedImage.TYPE_INT_RGB);
// 繪製縮小後的圖
tag.getGraphics().drawImage(src, 0, 0, iWideth, iHeight, null);
JimiWriter writer = Jimi.createJimiWriter(newfile);
writer.setSource(tag);
writer.putImage(newfile);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 給圖片添加文字 oldfile newfile就不用多說了 text 就是給圖片加的文字
*/
public void Imagefont(String oldfile, String newfile, String text) {
Image img;
String filename = oldfile.substring(oldfile.lastIndexOf(".") + 1);
String temp = newfile;
try {
if (!filename.equals("jpg")) {
this.toReduce(oldfile, newfile);
img = ImageIO.read(new File(temp));
} else {
img = ImageIO.read(new File(oldfile));
}
int width = img.getWidth(null);
int height = img.getHeight(null);
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(img, 0, 0, width, height, null);
g.setColor(Color.red);
g.setFont(new Font("Courier", Font.HANGING_BASELINE, 40));
g.drawString(text, width - 360, height - 72);
g.dispose();
FileOutputStream os = new FileOutputStream(temp);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
encoder.encode(image);
} catch (Exception e) {
e.getMessage();
}
}
/**
* 給圖片添加浮水印,並控制浮水印圖片的位置及透明度 oldfile newfile我也不說了 syoldfile 要添加的浮水印圖片的路徑 t
* 浮水印圖品添加的位置 to 浮水印圖片的透明度
*/
public void Imagese(String oldfile, String newfile, String syoldfile, int t,
float to) {
// 檔案類型
String filename = oldfile.substring(oldfile.lastIndexOf(".") + 1);
System.out.println(filename);
Image img;
String temp = newfile;
float alpha = to;
try {
if (!filename.equals("jpg")) {
toReduce(oldfile, newfile);
img = ImageIO.read(new File(temp));
System.out.println("其他類型圖片建立產生浮水印的圖片");
} else {
img = ImageIO.read(new File(oldfile));
}
int width = img.getWidth(null);
int height = img.getHeight(null);
int ws = width / 3;
int hs = height / 3;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.drawImage(img, 0, 0, width, height, null);
Image logo = ImageIO.read(new File(syoldfile));
int lw = logo.getWidth(null);
int lh = logo.getHeight(null);
// 設定圖片透明度
g
.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
alpha));
g.drawImage(logo, null, null);
switch (t) {
case 1:
g.drawImage(logo, 0, 0, lw, lh, null);
System.out.println("布局一");
break;
case 2:
g.drawImage(logo, ws, 0, lw, lh, null);
System.out.println("布局二");
break;
case 3:
g.drawImage(logo, 2 * ws, 0, lw, lh, null);
System.out.println("布局三");
break;
case 4:
g.drawImage(logo, 0, hs, lw, lh, null);
System.out.println("布局四");
break;
case 5:
g.drawImage(logo, ws, hs, lw, lh, null);
System.out.println("布局五");
break;
case 6:
g.drawImage(logo, 2 * ws, hs, lw, lh, null);
System.out.println("布局六");
break;
case 7:
g.drawImage(logo, 0, 2 * hs, lw, lh, null);
System.out.println("布局七");
break;
case 8:
g.drawImage(logo, ws, 2 * hs, lw, lh, null);
System.out.println("布局八");
break;
case 9:
g.drawImage(logo, 2 * ws, 2 * hs, lw, lh, null);
System.out.println("布局九");
break;
default:
break;
}
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
g.dispose();
// 產生圖片
FileOutputStream oss = new FileOutputStream(temp);
JPEGImageEncoder encoders = JPEGCodec.createJPEGEncoder(oss);
encoders.encode(image);
} catch (Exception e) {
e.getMessage();
e.printStackTrace();
}
}
public static void main(String[] args) {
T test = new T();
// alpha表示透明度,alpha 必須是範圍 [0.0, 1.0] 之內(包含邊界值)的一個浮點數字
float alpha = 0.1f;
// 產生浮水印圖
test.Imagese("d:/eclipse.jpg", "d:/eclipse_java2000.jpg",
"d:/java2000.jpg", 1, alpha);
/*
* test.Imagese("d:/001.jpg", "d:/aa/001.jpg", "d:/002.jpg", 1, alpha);
* test.Imagese("d:/001.jpg", "d:/aa/002.jpg", "d:/002.jpg", 2, alpha);
* test.Imagese("d:/001.jpg", "d:/aa/003.jpg", "d:/002.jpg", 3, alpha);
* test.Imagese("d:/001.jpg", "d:/aa/004.jpg", "d:/002.jpg", 4, alpha);
* test.Imagese("d:/001.jpg", "d:/aa/005.jpg", "d:/002.jpg", 5, alpha);
* test.Imagese("d:/001.jpg", "d:/aa/006.jpg", "d:/002.jpg", 6, alpha);
* test.Imagese("d:/001.jpg", "d:/aa/007.jpg", "d:/002.jpg", 7, alpha);
* test.Imagese("d:/001.jpg", "d:/aa/008.jpg", "d:/002.jpg", 8, alpha);
* test.Imagese("d:/001.jpg", "d:/aa/009.jpg", "d:/002.jpg", 9, alpha);
*/
}
}