Created
October 31, 2014 07:19
-
-
Save AvatarQing/2400c5c59640fb0c229d to your computer and use it in GitHub Desktop.
Copyright Generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.diao.tools.copyright; | |
import java.awt.Color; | |
import java.awt.Font; | |
import java.awt.Graphics2D; | |
import java.awt.font.LineBreakMeasurer; | |
import java.awt.font.TextLayout; | |
import java.awt.image.BufferedImage; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.text.AttributedString; | |
import javax.imageio.ImageIO; | |
public class Generator { | |
public static void main(String[] args) { | |
final String fontName = "新宋体"; | |
final int fontSize = 12; | |
// 图片的宽度 | |
int imageWidth = 500; | |
// 图片的高度 | |
int imageHeight = 1000; | |
BufferedImage image = new BufferedImage(imageWidth, imageHeight, | |
BufferedImage.TYPE_INT_RGB); | |
Graphics2D graphics = (Graphics2D) image.getGraphics(); | |
try { | |
Font font = new Font(fontName, Font.PLAIN, fontSize); | |
graphics.setFont(font); | |
graphics.setColor(Color.WHITE); | |
graphics.fillRect(0, 0, imageWidth, imageHeight); | |
// 设置黑色字体,同样可以graphics.setColor(Color.black); | |
graphics.setColor(new Color(0, 0, 0)); | |
String text = readString(); | |
AttributedString styledText = new AttributedString(text); | |
LineBreakMeasurer measurer = new LineBreakMeasurer( | |
styledText.getIterator(), graphics.getFontRenderContext()); | |
int startIndex = styledText.getIterator().getBeginIndex(); | |
int endIndex = styledText.getIterator().getEndIndex(); | |
measurer.setPosition(startIndex); | |
float paddingLeft = imageWidth / 8f; | |
float paddingRight = imageWidth / 8f; | |
float boundWidth = imageWidth - paddingLeft - paddingRight; | |
float x = paddingLeft, y = 0; | |
while (measurer.getPosition() < endIndex) { | |
TextLayout layout = measurer.nextLayout(boundWidth); | |
y += layout.getAscent(); | |
float dx = layout.isLeftToRight() ? 0 : boundWidth | |
- layout.getAdvance(); | |
layout.draw(graphics, x + dx, y); | |
y += layout.getDescent() + layout.getLeading(); | |
} | |
ImageIO.write(image, "PNG", new File("D:\\abc.png")); | |
System.out.println("DONE"); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
// 释放资源 | |
graphics.dispose(); | |
} | |
private static String readString() { | |
String filePath = "D:\\origin.txt"; | |
String encoding = "GBK"; | |
StringBuilder result = new StringBuilder(); | |
try { | |
InputStreamReader read = new InputStreamReader(new FileInputStream( | |
new File(filePath)), encoding); | |
BufferedReader br = new BufferedReader(read); | |
String temp; | |
while ((temp = br.readLine()) != null) { | |
result.append(temp); | |
result.append("\n"); | |
} | |
br.close(); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return result.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment