因工做須要在j2ee的環境中作電子印章 因而用水印作了一個 支持圓形帶五角星 方形印章

/**java

* 輸出圖片的公用方法數據庫

數組

* @param message字體

*            弧度上的字 (基本都是從數據庫讀取)spa

* @param centerName.net

*            中間要顯示的名字code

* @param widthorm

*            中間字體的寬度圖片

* @param height字符串

*            中間字體的高度

* @return

* @throws FileNotFoundException

* @throws IOException

*/

public static BufferedImage bufferedImageUtil(String message,

String centerName, int width, int height)

throws FileNotFoundException, IOException {

BufferedImage image = null;

if (message != null) {

final int CENTERX = 90;

final int CENTERY = 90;

final int radius = 76;

// 獲取get_png文件夾

ActionContext ac = ActionContext.getContext();

ServletContext sc = (ServletContext) ac

.get(ServletActionContext.SERVLET_CONTEXT);

String filePath = sc.getRealPath(File.separator + "style"

+ File.separator + "imgs");

String path = filePath;

image = ImageIO.read(new FileInputStream(path + File.separator

+ "newIcon.jpg"));

Graphics2D g2 = image.createGraphics();// 獲得圖形上下文

g2.setColor(Color.RED); // 設置畫筆顏色

// 設置字體

g2.setFont(new Font("宋體", Font.LAYOUT_LEFT_TO_RIGHT, 15));// 寫入簽名

if (centerName != null) {

g2.drawString(centerName, width, height);

}

// 根據輸入字符串獲得字符數組

String[] messages2 = message.split("", 0);

String[] messages = new String[messages2.length - 1];

System.arraycopy(messages2, 1, messages, 0, messages2.length - 1);

// 輸入的字數

int ilength = messages.length;

// 設置字體屬性

int fontsize = 16;

Font f = new Font("", Font.BOLD, fontsize);

FontRenderContext context = g2.getFontRenderContext();

Rectangle2D bounds = f.getStringBounds(message, context);

// 字符寬度=字符串長度/字符數

double char_interval = (bounds.getWidth() / ilength);

// 上坡度

double ascent = -bounds.getY();

int first = 0, second = 0;

boolean odd = false;

if (ilength % 2 == 1) {

first = (ilength - 1) / 2;

odd = true;

} else {

first = (ilength) / 2 - 1;

second = (ilength) / 2;

odd = false;

}

double radius2 = radius - ascent;

double x0 = CENTERX;

double y0 = CENTERY - radius + ascent;

// 旋轉角度

double a = 2 * Math.asin(char_interval / (2 * radius2));

if (odd) {

g2.setFont(f);

g2.drawString(messages[first],

(float) (x0 - char_interval / 2), (float) y0);

// 中心點的右邊

for (int i = first + 1; i < ilength; i++) {

double aa = (i - first) * a;

double ax = radius2 * Math.sin(aa);

double ay = radius2 - radius2 * Math.cos(aa);

AffineTransform transform = AffineTransform

.getRotateInstance(aa);// ,x0 + ax, y0 + ay);

Font f2 = f.deriveFont(transform);

g2.setFont(f2);

g2

.drawString(messages[i],

(float) (x0 + ax - char_interval / 2

* Math.cos(aa)),

(float) (y0 + ay - char_interval / 2

* Math.sin(aa)));

}

// 中心點的左邊

for (int i = first - 1; i > -1; i--) {

double aa = (first - i) * a;

double ax = radius2 * Math.sin(aa);

double ay = radius2 - radius2 * Math.cos(aa);

AffineTransform transform = AffineTransform

.getRotateInstance(-aa);// ,x0 + ax, y0 + ay);

Font f2 = f.deriveFont(transform);

g2.setFont(f2);

g2

.drawString(messages[i],

(float) (x0 - ax - char_interval / 2

* Math.cos(aa)),

(float) (y0 + ay + char_interval / 2

* Math.sin(aa)));

}

} else {

// 中心點的右邊

for (int i = second; i < ilength; i++) {

double aa = (i - second + 0.5) * a;

double ax = radius2 * Math.sin(aa);

double ay = radius2 - radius2 * Math.cos(aa);

AffineTransform transform = AffineTransform

.getRotateInstance(aa);// ,x0 + ax, y0 + ay);

Font f2 = f.deriveFont(transform);

g2.setFont(f2);

g2

.drawString(messages[i],

(float) (x0 + ax - char_interval / 2

* Math.cos(aa)),

(float) (y0 + ay - char_interval / 2

* Math.sin(aa)));

}

// 中心點的左邊

for (int i = first; i > -1; i--) {

double aa = (first - i + 0.5) * a;

double ax = radius2 * Math.sin(aa);

double ay = radius2 - radius2 * Math.cos(aa);

AffineTransform transform = AffineTransform

.getRotateInstance(-aa);// ,x0 + ax, y0 + ay);

Font f2 = f.deriveFont(transform);

g2.setFont(f2);

g2

.drawString(messages[i],

(float) (x0 - ax - char_interval / 2

* Math.cos(aa)),

(float) (y0 + ay + char_interval / 2

* Math.sin(aa)));

}

}

g2.dispose();

}

return image;

}

---------------------------------上述爲圓形帶五角星的章-------------------------

/**

* 方形名字章

* @param message

*            要刻的名字

* @return

* @throws FileNotFoundException

* @throws IOException

*/

public static BufferedImage getSquarePng(String message)

throws FileNotFoundException, IOException {

BufferedImage image = null;

if (message != null) {

ActionContext ac = ActionContext.getContext();

ServletContext sc = (ServletContext) ac

.get(ServletActionContext.SERVLET_CONTEXT);

String filePath = sc.getRealPath(File.separator + "style"

+ File.separator + "imgs");

String path = filePath;

image = ImageIO.read(new FileInputStream(path + File.separator

+ "squarePng.png"));

Graphics2D g2 = image.createGraphics();// 獲得圖形上下文

g2.setColor(Color.RED); // 設置畫筆顏色

// 設置字體

g2.setFont(new Font("宋體", Font.LAYOUT_LEFT_TO_RIGHT, 80));// 寫入簽名

if (message != null) {

String newMessage = null;

// 若是三個 則 XXX章 若是兩個則 XX之章 大於3個 則 取前三

if (message.length() == 2) {

newMessage = message + "之章";

} else if (message.length() == 3) {

newMessage = message + "章";

} else if (message.length() > 3) {

newMessage = message.substring(0, 3) + "章";

}

char[] charArray = newMessage.toCharArray();

g2.drawString(new String(new char[] { charArray[0] }), 90, 70);  // 寫文字

g2.drawString(new String(new char[] { charArray[1] }), 90, 160); // 寫文字

g2.drawString(new String(new char[] { charArray[2] }), 10, 70);  // 寫文字

g2.drawString(new String(new char[] { charArray[3] }), 10, 160); // 寫文字

}

}

return image;

}

---------------------------------方形章---------------------------------------

---------------------------------調用方法------------------------------------

/**

* 獲取方形印章

* @return

*/

public String getSquarePngByUserName() {

String info = null;

BufferedImage image = null;

try {

ActionContext ac = ActionContext.getContext();

HttpServletResponse response = (HttpServletResponse) ac

.get(ServletActionContext.HTTP_RESPONSE);

response.setContentType("image/JPEG");

OutputStream output = response.getOutputStream();

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);

Users user = (Users) SiteAction.getEntity("loginUser");

if (user != null) {

String userName = user.getUName();

if (userName != null) {

image = OutPicture.getSquarePng(userName);

}

}

if (image != null) {

encoder.encode(image);

}

output.flush();

output.close();

} catch (Exception e) {

return ERROR;

}

return info;

}


// 工商行政管理局(圓形


)

public String noticeOfRegistrationPng() {

String info = null;

try {

ActionContext ac = ActionContext.getContext();

HttpServletResponse response = (HttpServletResponse) ac

.get(ServletActionContext.HTTP_RESPONSE);

response.setContentType("image/JPEG");

OutputStream output = response.getOutputStream();

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);

BufferedImage image = OutPicture.bufferedImageUtil("工商行政管理局", null,

48, 150);

if (image != null) {

encoder.encode(image);

}

output.flush();

output.close();

} catch (Exception e) {

info = Action.ERROR;

}

return info;

}

--------------------------------效果--------------------------------

相關文章
相關標籤/搜索