?? pdfimgcreateutil.java
字號:
package com.cn.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.font.TextAttribute;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;
import javax.swing.ImageIcon;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class PDFIMGCreateUtil {
public static void main(String[] a) {
// 參數分別是,原文件路徑;輸出路徑;打印文字;字色;量化表[也就是壓縮率,0.0為高壓縮,圖片不清晰,1.0為最低壓縮,圖片清晰]; 字體;
// 字大小; 字型[0為普通字型,1為粗字型,2為斜體字型,3為粗斜體字型,4為粗下劃線字型,5為斜下劃線字型,6為粗斜下劃線字型,7為下劃線字型];字距;坐標x;坐標y)
// for(int i=0;i<10;i=i+2){
PDFIMGCreateUtil.fontMark("d:\\"+0+".jpg", "d:\\"+1+".jpg", "abcd你好",
new Color(255, 0, 100), 1, "宋體", 96, 6, 1000, 10, 10);
// }
// System.out.println(d.createMark("e8.jpg","e81.jpg","",null,
// 1,"",16));
}
public static boolean fontMark(String filePath, String filePath1,
String markContent, Color markContentColor, float qualNum,
String fontType, int fontSize, int shape, int distance, float x, float y) {
// System.out.println(markContentColor.toString());
ImageIcon imgIcon = new ImageIcon(filePath);
Image theImg = imgIcon.getImage();
// Image可以獲得 輸入圖片的信息
int width = theImg.getWidth(null);
int height = theImg.getHeight(null);
// 800 800 為畫出圖片的大小
BufferedImage bimage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 2d 畫筆
Graphics2D g = bimage.createGraphics();
g.setColor(markContentColor);
g.setBackground(Color.white);
// 畫出圖片-----------------------------------
g.drawImage(theImg, 0, 0, null);
// 畫出圖片-----------------------------------
// --------對要顯示的文字進行處理--------------
AttributedString ats = new AttributedString(markContent);
// 三個參數分別為,字體,字型(Font.PLAIN為普通樣式,Font.BOLD為粗體,Font.ITALIC為斜體,Font.BOLD|Font.ITALIC為粗斜體),字大小
Font f = null;
// 添加字距
ats.addAttribute(TextAttribute.KERNING,TextAttribute.KERNING_ON+25);
// 0為普通字型,1為粗字型,2為斜體字型,3為粗斜體字型,4為粗下劃線字型,5為斜下劃線字型,6為粗斜下劃線字型,7為下劃線字型
switch (shape) {
case 0:
f = new Font(fontType, Font.PLAIN, fontSize);
break;
case 1:
f = new Font(fontType, Font.BOLD, fontSize);
break;
case 2:
f = new Font(fontType, Font.ITALIC, fontSize);
break;
case 3:
f = new Font(fontType, Font.BOLD | Font.ITALIC, fontSize);
break;
case 4:
f = new Font(fontType, Font.BOLD, fontSize);
ats.addAttribute(TextAttribute.UNDERLINE,
TextAttribute.UNDERLINE_ON, 0, markContent.length());
break;
case 5:
f = new Font(fontType, Font.ITALIC, fontSize);
ats.addAttribute(TextAttribute.UNDERLINE,
TextAttribute.UNDERLINE_LOW_TWO_PIXEL, 0, markContent
.length());
break;
case 6:
f = new Font(fontType, Font.BOLD | Font.ITALIC, fontSize);
ats.addAttribute(TextAttribute.UNDERLINE,
TextAttribute.UNDERLINE_ON, 0, markContent.length());
break;
case 7:
f = new Font(fontType, Font.PLAIN, fontSize);
ats.addAttribute(TextAttribute.UNDERLINE,
TextAttribute.UNDERLINE_ON, 0, markContent.length());
default:
f = new Font(fontType, Font.PLAIN, fontSize);
break;
}
ats.addAttribute(TextAttribute.FONT, f, 0, markContent.length());
AttributedCharacterIterator iter = ats.getIterator();
// ----------------------
g.drawString(iter, x, y+fontSize);
// 添加水印的文字和設置水印文字出現的內容 ----位置(坐標)
g.dispose();// 畫筆結束
try {
// 輸出 文件 到指定的路徑
FileOutputStream out = new FileOutputStream(filePath1);
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;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -