用Java實現支持圓形帶五角星 方形電子印章

jopen 10年前發布 | 97K 次閱讀 Java Java開發

本來這不是一個特別難的問題 但是剛開始的時候一直就想通過純的代碼實現 而不借助于水印 花了有一天的時間 這的確不應該 但是從中我們也可以認識到 對于代碼 千萬不能鉆牛角尖 正所謂 跳跳大道公羅馬 不多說 下面寫代碼 給需要的朋友一些幫助

 /**

  • 輸出圖片的公用方法

  • @param message

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

  • @param centerName

  • 中間要顯示的名字

  • @param width

  • 中間字體的寬度

  • @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;

}</pre>
</span>

--------------------------------效果--------------------------------用Java實現支持圓形帶五角星 方形電子印章

</div>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!