|
@@ -17,19 +17,28 @@ import java.util.List;
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
|
public class ImageUtil {
|
|
|
- public static void drawLaugh(int size, OutputStream os) throws Exception {
|
|
|
+ static final String base = "@#&$%*o!;. ";
|
|
|
+
|
|
|
+ public static void drawLaugh(int size, boolean isTextBlack, OutputStream os) throws Exception {
|
|
|
int width = size;
|
|
|
int height = size;
|
|
|
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
|
|
int biankuang = size / 16;
|
|
|
|
|
|
Graphics2D g2 = (Graphics2D) bi.getGraphics();
|
|
|
- g2.setColor(Color.black);
|
|
|
+ if (isTextBlack) {
|
|
|
+ g2.setColor(Color.WHITE);
|
|
|
+ } else {
|
|
|
+ g2.setColor(Color.black);
|
|
|
+ }
|
|
|
g2.fillRect(0, 0, width, height);
|
|
|
-
|
|
|
- g2.setColor(Color.white);
|
|
|
- g2.setStroke(new BasicStroke(biankuang));// �Ӵ�
|
|
|
- g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // �����
|
|
|
+ if (isTextBlack) {
|
|
|
+ g2.setColor(Color.black);
|
|
|
+ } else {
|
|
|
+ g2.setColor(Color.WHITE);
|
|
|
+ }
|
|
|
+ g2.setStroke(new BasicStroke(biankuang));// 线加粗
|
|
|
+ g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 抗锯齿
|
|
|
g2.drawArc(biankuang, biankuang, size - biankuang * 2, size - biankuang * 2, 0, 360);
|
|
|
g2.drawArc(biankuang + size / 8, biankuang + size / 8, size - biankuang * 2 - size / 4, size - biankuang * 2 - size / 4, 225, 90);
|
|
|
g2.fillArc(size / 4, size / 4, size / 8, size / 8, 0, 360);
|
|
@@ -39,19 +48,26 @@ public class ImageUtil {
|
|
|
ImageIO.write(bi, "BMP", os);
|
|
|
}
|
|
|
|
|
|
- public static void drawCry(int size, OutputStream os) throws Exception {
|
|
|
+ public static void drawCry(int size, boolean isTextBlack, OutputStream os) throws Exception {
|
|
|
int width = size;
|
|
|
int height = size;
|
|
|
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
|
|
int biankuang = size / 16;
|
|
|
|
|
|
Graphics2D g2 = (Graphics2D) bi.getGraphics();
|
|
|
- g2.setColor(Color.black);
|
|
|
+ if (isTextBlack) {
|
|
|
+ g2.setColor(Color.WHITE);
|
|
|
+ } else {
|
|
|
+ g2.setColor(Color.black);
|
|
|
+ }
|
|
|
g2.fillRect(0, 0, width, height);
|
|
|
-
|
|
|
- g2.setColor(Color.white);
|
|
|
- g2.setStroke(new BasicStroke(biankuang));// �Ӵ�
|
|
|
- g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // �����
|
|
|
+ if (isTextBlack) {
|
|
|
+ g2.setColor(Color.black);
|
|
|
+ } else {
|
|
|
+ g2.setColor(Color.WHITE);
|
|
|
+ }
|
|
|
+ g2.setStroke(new BasicStroke(biankuang));// 线加粗
|
|
|
+ g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 抗锯齿
|
|
|
g2.drawArc(biankuang, biankuang, size - biankuang * 2, size - biankuang * 2, 0, 360);
|
|
|
g2.drawArc(biankuang + size / 8, biankuang + size / 8 + size / 2, size - biankuang * 2 - size / 4, size - biankuang * 2 - size / 4, 45, 90);
|
|
|
g2.fillArc(size / 4, size / 4, size / 8, size / 8, 0, 360);
|
|
@@ -85,18 +101,18 @@ public class ImageUtil {
|
|
|
} else {
|
|
|
g2.setColor(Color.WHITE);
|
|
|
}
|
|
|
- g2.setStroke(new BasicStroke(2.0f));// �Ӵ�
|
|
|
- // g2.drawRect(1, 1, width - 2, height - 2); // �߿�
|
|
|
+ g2.setStroke(new BasicStroke(2.0f));// 线加粗
|
|
|
+ // g2.drawRect(1, 1, width - 2, height - 2); // 边框
|
|
|
|
|
|
- // g2.setStroke(new BasicStroke(0.0f)); // �߿���Ҫ�Ӵ�
|
|
|
+ // g2.setStroke(new BasicStroke(0.0f)); // 边框不需要加粗
|
|
|
// g2.drawLine(0, 80, 820, 80);
|
|
|
|
|
|
- // g2.setFont(new Font("����", Font.BOLD, fontSize));
|
|
|
- g2.setFont(new Font("����", Font.BOLD, fontSize));
|
|
|
- g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // �����
|
|
|
- // FontMetrics fm = g2.getFontMetrics(titleFont);// �������ֳ���,������е�X������
|
|
|
+ // g2.setFont(new Font("宋体", Font.BOLD, fontSize));
|
|
|
+ g2.setFont(new Font("楷体", Font.BOLD, fontSize));
|
|
|
+ g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 抗锯齿
|
|
|
+ // FontMetrics fm = g2.getFontMetrics(titleFont);// 计算文字长度,计算居中的X点坐标
|
|
|
// int titleWidth = fm.stringWidth(text);
|
|
|
- // int titleWidthX = (width - titleWidth) / 2;// ����
|
|
|
+ // int titleWidthX = (width - titleWidth) / 2;// 居中
|
|
|
for (int i = 0; i < text.length(); i++) {
|
|
|
if (vertical) {
|
|
|
g2.drawString(text.substring(i, i + 1), 0, fontSize * (i + 1) - 8);
|
|
@@ -128,17 +144,17 @@ public class ImageUtil {
|
|
|
} else {
|
|
|
g2.setColor(Color.WHITE);
|
|
|
}
|
|
|
- g2.setStroke(new BasicStroke(2.0f));// �Ӵ�
|
|
|
- // g2.drawRect(1, 1, width - 2, height - 2); // �߿�
|
|
|
+ g2.setStroke(new BasicStroke(2.0f));// 线加粗
|
|
|
+ // g2.drawRect(1, 1, width - 2, height - 2); // 边框
|
|
|
|
|
|
- // g2.setStroke(new BasicStroke(0.0f)); // �߿���Ҫ�Ӵ�
|
|
|
+ // g2.setStroke(new BasicStroke(0.0f)); // 边框不需要加粗
|
|
|
// g2.drawLine(0, 80, 820, 80);
|
|
|
|
|
|
g2.setFont(new Font("Arial", Font.BOLD, fontSize));
|
|
|
- g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // �����
|
|
|
- // FontMetrics fm = g2.getFontMetrics(titleFont);// �������ֳ���,������е�X������
|
|
|
+ g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 抗锯齿
|
|
|
+ // FontMetrics fm = g2.getFontMetrics(titleFont);// 计算文字长度,计算居中的X点坐标
|
|
|
// int titleWidth = fm.stringWidth(text);
|
|
|
- // int titleWidthX = (width - titleWidth) / 2;// ����
|
|
|
+ // int titleWidthX = (width - titleWidth) / 2;// 居中
|
|
|
g2.drawString(text, 0, fontSize - 8);
|
|
|
|
|
|
g2.dispose();
|
|
@@ -153,6 +169,7 @@ public class ImageUtil {
|
|
|
int minY = bi.getMinY();
|
|
|
int[] rgb1 = new int[3];
|
|
|
int[] rgb2 = new int[3];
|
|
|
+ int total_rgb = 256 * 6;
|
|
|
for (int y = minY + 1; y < height; y = y + 2) {
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
for (int x = minX; x < width; x++) {
|
|
@@ -171,15 +188,18 @@ public class ImageUtil {
|
|
|
for (int value : rgb2) {
|
|
|
totalValue += value;
|
|
|
}
|
|
|
- String str;
|
|
|
- if (totalValue < 256) {
|
|
|
- str = "��";
|
|
|
- } else if (totalValue < 768) {
|
|
|
- str = "��";
|
|
|
- } else {
|
|
|
- str = " ";
|
|
|
- }
|
|
|
- sb.append(str);
|
|
|
+ int charIndex = (int) (1.0 * totalValue / total_rgb * base.length());
|
|
|
+ char ch = base.charAt(charIndex);
|
|
|
+ sb.append(ch);
|
|
|
+ // String str;
|
|
|
+ // if (totalValue < 256) {
|
|
|
+ // str = "█";
|
|
|
+ // } else if (totalValue < 768) {
|
|
|
+ // str = "▓";
|
|
|
+ // } else {
|
|
|
+ // str = " ";
|
|
|
+ // }
|
|
|
+ // sb.append(str);
|
|
|
}
|
|
|
result.add(sb.toString());
|
|
|
}
|
|
@@ -198,9 +218,10 @@ public class ImageUtil {
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
+ boolean isTextBlack = true;
|
|
|
{
|
|
|
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
|
- drawEnglish("good!", 64, false, os);
|
|
|
+ drawEnglish("good!", 64, isTextBlack, os);
|
|
|
os.close();
|
|
|
InputStream is = new ByteArrayInputStream(os.toByteArray());
|
|
|
BufferedImage bi = ImageIO.read(is);
|
|
@@ -212,7 +233,7 @@ public class ImageUtil {
|
|
|
}
|
|
|
|
|
|
{
|
|
|
- List<String> strList = toStringList("ţ��", false, 64, false);
|
|
|
+ List<String> strList = toStringList("牛逼", false, 64, isTextBlack);
|
|
|
for (String str : strList) {
|
|
|
System.out.println(str);
|
|
|
}
|
|
@@ -220,13 +241,13 @@ public class ImageUtil {
|
|
|
|
|
|
{
|
|
|
OutputStream os = new FileOutputStream("cow.bmp");
|
|
|
- drawChinese("ţ��", false, 64, false, os);
|
|
|
+ drawChinese("牛逼", false, 64, isTextBlack, os);
|
|
|
os.close();
|
|
|
}
|
|
|
|
|
|
{
|
|
|
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
|
- drawLaugh(128, os);
|
|
|
+ drawLaugh(72, isTextBlack, os);
|
|
|
os.close();
|
|
|
InputStream is = new ByteArrayInputStream(os.toByteArray());
|
|
|
BufferedImage bi = ImageIO.read(is);
|
|
@@ -238,13 +259,13 @@ public class ImageUtil {
|
|
|
}
|
|
|
{
|
|
|
OutputStream os = new FileOutputStream("Laugh.bmp");
|
|
|
- drawLaugh(128, os);
|
|
|
+ drawLaugh(72, isTextBlack, os);
|
|
|
os.close();
|
|
|
}
|
|
|
|
|
|
{
|
|
|
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
|
- drawCry(128, os);
|
|
|
+ drawCry(72, isTextBlack, os);
|
|
|
os.close();
|
|
|
InputStream is = new ByteArrayInputStream(os.toByteArray());
|
|
|
BufferedImage bi = ImageIO.read(is);
|
|
@@ -256,7 +277,7 @@ public class ImageUtil {
|
|
|
}
|
|
|
{
|
|
|
OutputStream os = new FileOutputStream("Cry.bmp");
|
|
|
- drawCry(128, os);
|
|
|
+ drawCry(72, isTextBlack, os);
|
|
|
os.close();
|
|
|
}
|
|
|
}
|