沒想到到星期一上班脖子就不得勁,順便查了點資料,發現個山寨版的頸椎矯正圖,覺得挺有意思。
如下圖:
於是回家後想到自己也做個玩。
問題是,咱爺們不說程序員吧,好歹也是個壘碼的,直接PS文字圖未免有礙觀瞻,於是抽空寫了個 Java自動生成版的。
代碼如下:
package org.test;
import java.awt.AlphaComposite;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
* Copyright 2008
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
* @project loonframework
* @author chenpeng
* @email:[email protected]
* @version 0.1
*/
public class MessageImage extends Canvas {
/**
*
*/
private static final long serialVersionUID = 1L;
private BufferedImage fontImage;
private Graphics2D g2d;
private Image backImage;
final static private int WIDTH = 600;
final static private int HEIGHT = 480;
public MessageImage(final String messages) {
fontImage = new BufferedImage(WIDTH, HEIGHT, 2);
g2d = fontImage.createGraphics();
try {
backImage=ImageIO.read(new File("back.png"));
} catch (IOException e) {
e.printStackTrace();
}
g2d.drawImage(backImage, 0, 0, null);
setAlpha(g2d, 0.7);
int size = 25;
int newLine = (WIDTH / size) - 10;
char[] messageChars = messages.toCharArray();
boolean d = true;
StringBuilder sbr = new StringBuilder();
int count = 0;
int len = messageChars.length - 1;
String fontStyle = "幼 圓";
Color color = Color.white;
for (int i = 0, j = 0; i <= len; i++, j++) {
sbr.append(messageChars[i]);
if (j == newLine || (messageChars[i] == '\n')) {
g2d.drawImage(createImageMessages(1, false, sbr.toString(),
color, fontStyle, 1, size, d), count += 30,
(HEIGHT - (sbr.length() * size)) - (size * 4), null);
d = !d;
sbr.delete(0, sbr.length());
j = 0;
} else if (i == len) {
g2d.drawImage(createImageMessages(1, false, sbr.toString(),
color, fontStyle, 1, size, d), count += 30,
(HEIGHT - (sbr.length() * size)) - (size * 4), null);
}
}
setAlpha(g2d, 0.6);
String mes = "Java版頸椎自動矯正 圖";
fontStyle = "華文新魏";
size = 20;
g2d.drawImage(createImageMessages(0, true, mes, Color.red, fontStyle,
1, size, false), WIDTH - (mes.length() * size) - (size * 2),
HEIGHT - (size * 2), null);
}
/**
* 創建一組圖片文字
*
* @param aspect
* @param isRow
* @param messages
* @param color
* @param name
* @param style
* @param size
* @param d
* @return
*/
public static BufferedImage createImageMessages(final int aspect,
final boolean isRow, final String messages, final Color color,
final String name, final int style, final int size, final boolean d) {
final int flength = messages.length();
final int nowSize = size * flength;
BufferedImage fontImages;
if (isRow)
fontImages = new BufferedImage(nowSize, size, 2);
else
fontImages = new BufferedImage(size, nowSize, 2);
Graphics2D graphics2d = fontImages.createGraphics();
char[] messageChars = messages.toCharArray();
if (d) {
char[] temp = new char[flength];
for (int i = 0, j = flength - 1; i < flength; i++, j--) {
temp[j] = messageChars[i];
}
messageChars = temp;
}
if (isRow)
for (int i = 0; i < flength; i++) {
graphics2d.drawImage (createImageMessage(aspect,
messageChars [i], color, name, style, size, d),
i * size, 0, null);
}
else
for (int i = 0; i < flength; i++) {
graphics2d.drawImage(createImageMessage(aspect,
messageChars[i], color, name, style, size, d), 0, i
* size, null);
}
graphics2d.dispose();
System.gc();
return fontImages;
}
/**
* 創建單獨圖片文字
*
* @param aspect
* @param message
* @param color
* @param name
* @param style
* @param size
* @param d
* @return
*/
public static BufferedImage createImageMessage (final int aspect,
final char message, final Color color, final String name,
final int style, final int size, final boolean d) {
final int nowSize = size + 1;
BufferedImage fontImage = new BufferedImage(nowSize, nowSize, 2);
Graphics2D graphics2d = fontImage.createGraphics();
graphics2d.setColor(color);
graphics2d.setFont(new Font(name, style, size));
// 設定圖像顯示狀態
RenderingHints hints = new RenderingHints(
RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
hints.put (RenderingHints.KEY_DITHERING,
RenderingHints.VALUE_DITHER_ENABLE);
hints.put (RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_SPEED);
hints.put (RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
hints.put (RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON);
hints.put (RenderingHints.KEY_COLOR_RENDERING,
RenderingHints.VALUE_COLOR_RENDER_QUALITY);
hints.put (RenderingHints.KEY_DITHERING,
RenderingHints.VALUE_DITHER_DISABLE);
graphics2d.setRenderingHints (hints);
graphics2d.drawString(String.valueOf(message), 0, size - 3);
graphics2d.dispose();
switch (aspect) {
case 0:
break;
case 1:
fontImage = MessageImage.rotateImage(fontImage, 90, d);
break;
case 2:
fontImage = MessageImage.rotateImage(fontImage, 180, d);
break;
case 3:
fontImage = MessageImage.rotateImage(fontImage, 360, d);
break;
}
return fontImage;
}
public void paint(Graphics g) {
g.drawImage(fontImage, 0, 0, null);
}
/**
* 水平翻轉當前圖 像
*
* @return
*/
public static BufferedImage rotateImage(final BufferedImage image) {
int w = image.getWidth();
int h = image.getHeight();
BufferedImage img;
Graphics2D graphics2d;
(graphics2d = (img = new BufferedImage(w, h, image.getColorModel()
.getTransparency ())).createGraphics()).drawImage(image, 0, 0,
w, h, w, 0, 0, h, null);
graphics2d.dispose();
return img;
}
/**
* 旋轉圖像為指定角度
*
* @param degree
* @return
*/
public static BufferedImage rotateImage(final BufferedImage image,
final int angdeg, final boolean d) {
int w = image.getWidth();
int h = image.getHeight();
int type = image.getColorModel().getTransparency ();
BufferedImage img;
Graphics2D graphics2d;
(graphics2d = (img = new BufferedImage(w, h, type)).createGraphics())
.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate(d ? -Math.toRadians(angdeg) : Math.toRadians(angdeg),
w / 2, h / 2);
graphics2d.drawImage(image, 0, 0, null);
graphics2d.dispose();
return img;
}
final static public void setAlpha(final Graphics2D g, final double d) {
AlphaComposite alphacomposite = AlphaComposite
.getInstance(3, (float) d);
g.setComposite(alphacomposite);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run () {
Frame frame = new Frame();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame
.add(new MessageImage(
"每天面對計算機\n不是壘碼、就是泡論壇、再不然就是和腦殘作斗爭,爆個哈韓吧, 攻擊個棒子網什麼的,再閒時就寫寫Blog,轉轉Google,"
+ "總之24小時離不開電腦,離不開網絡(已被納入我 國精神病症狀|||),長時間不活動,這頸椎可怎麼受的了?沒辦法,想點辦法解決吧。"
+ "最近有人說圖片能治 療頸椎病,我卻偏不信那個邪,就幾句話能費人多大的力氣去看?說到底還真能累死活人不成?"
+ "這左轉右繞 上竄下跳的,除了費點眼睛,怎麼可能把脖子一塊運動起來?您說是不是這道理?"));
frame.setSize(WIDTH + 5, HEIGHT + 25);
frame.setResizable(false);
frame.setTitle("Java版頸椎自 動矯正圖");
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
效果圖如下:
仔細說起來,這就和各大小說門戶生成圖片版小說的原理一般無二,毫無技術難度可言,不過畢竟寫 了,也就丟出來在Blog湊個數。
那位有時間的話(或者等以後鄙人閒的沒事),可以做個自動導出,弄套資治通鑒全文之類的圖片做 頸椎矯正,試驗下連續的看完整本頸椎是輕松了,還是碎掉了……
本文出自 “Java究竟怎麼玩” 博客,請務必保留此出處 http://cping1982.blog.51cto.com/601635/116597