本来上周双休日没什么事情,准备干点遗留了很久的正经事,结果半途忍不住手贱看新闻,于是被11.28事件触动,随大流的针对棒子及棒子粉抽疯了两天。
没想到到星期一上班脖子就不得劲,顺便查了点资料,发现个山寨版的颈椎矫正图,觉得挺有意思。
如下图:

于是回家后想到自己也做个玩。
问题是,咱爷们不说程序员吧,好歹也是个垒码的,直接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:ceponline@yahoo.com.cn
- * @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凑个数。
那位有时间的话(或者等以后鄙人闲的没事),可以做个自动导出,弄套资治通鉴全文之类的图片做颈椎矫正,试验下连续的看完整本颈椎是轻松了,还是碎掉了……