|
@@ -0,0 +1,263 @@
|
|
|
+package com.persagy.ibms.data.sdk.util;
|
|
|
+
|
|
|
+import java.awt.BasicStroke;
|
|
|
+import java.awt.Color;
|
|
|
+import java.awt.Font;
|
|
|
+import java.awt.Graphics2D;
|
|
|
+import java.awt.RenderingHints;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+
|
|
|
+public class ImageUtil {
|
|
|
+ public static void drawLaugh(int size, 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);
|
|
|
+ g2.fillRect(0, 0, width, height);
|
|
|
+
|
|
|
+ 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);
|
|
|
+ g2.fillArc(size - size / 4 - size / 8, size / 4, size / 8, size / 8, 0, 360);
|
|
|
+
|
|
|
+ g2.dispose();
|
|
|
+ ImageIO.write(bi, "BMP", os);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void drawCry(int size, 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);
|
|
|
+ g2.fillRect(0, 0, width, height);
|
|
|
+
|
|
|
+ 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);
|
|
|
+ g2.fillArc(size - size / 4 - size / 8, size / 4, size / 8, size / 8, 0, 360);
|
|
|
+
|
|
|
+ g2.dispose();
|
|
|
+ ImageIO.write(bi, "BMP", os);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void drawChinese(String text, boolean vertical, int fontSize, boolean isTextBlack, OutputStream os) throws Exception {
|
|
|
+ int width;
|
|
|
+ int height;
|
|
|
+ if (vertical) {
|
|
|
+ width = fontSize + 4;
|
|
|
+ height = fontSize * text.length() + 4;
|
|
|
+ } else {
|
|
|
+ width = fontSize * text.length() + 4;
|
|
|
+ height = fontSize + 4;
|
|
|
+ }
|
|
|
+ BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
|
|
+
|
|
|
+ Graphics2D g2 = (Graphics2D) bi.getGraphics();
|
|
|
+ if (isTextBlack) {
|
|
|
+ g2.setColor(Color.WHITE);
|
|
|
+ } else {
|
|
|
+ g2.setColor(Color.black);
|
|
|
+ }
|
|
|
+ g2.fillRect(0, 0, width, height);
|
|
|
+ if (isTextBlack) {
|
|
|
+ g2.setColor(Color.black);
|
|
|
+ } else {
|
|
|
+ g2.setColor(Color.WHITE);
|
|
|
+ }
|
|
|
+ g2.setStroke(new BasicStroke(2.0f));// �Ӵ�
|
|
|
+ // g2.drawRect(1, 1, width - 2, height - 2); // �߿�
|
|
|
+
|
|
|
+ // 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������
|
|
|
+ // int titleWidth = fm.stringWidth(text);
|
|
|
+ // 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);
|
|
|
+ } else {
|
|
|
+ g2.drawString(text.substring(i, i + 1), fontSize * i, fontSize - 8);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ g2.dispose();
|
|
|
+ ImageIO.write(bi, "BMP", os);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void drawEnglish(String text, int fontSize, boolean isTextBlack, OutputStream os) throws Exception {
|
|
|
+ int width;
|
|
|
+ int height;
|
|
|
+ width = fontSize / 2 * text.length() + 8;
|
|
|
+ height = fontSize + 8;
|
|
|
+ BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
|
|
+
|
|
|
+ Graphics2D g2 = (Graphics2D) bi.getGraphics();
|
|
|
+ if (isTextBlack) {
|
|
|
+ g2.setColor(Color.WHITE);
|
|
|
+ } else {
|
|
|
+ g2.setColor(Color.black);
|
|
|
+ }
|
|
|
+ g2.fillRect(0, 0, width, height);
|
|
|
+ if (isTextBlack) {
|
|
|
+ g2.setColor(Color.black);
|
|
|
+ } else {
|
|
|
+ g2.setColor(Color.WHITE);
|
|
|
+ }
|
|
|
+ g2.setStroke(new BasicStroke(2.0f));// �Ӵ�
|
|
|
+ // g2.drawRect(1, 1, width - 2, height - 2); // �߿�
|
|
|
+
|
|
|
+ // 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������
|
|
|
+ // int titleWidth = fm.stringWidth(text);
|
|
|
+ // int titleWidthX = (width - titleWidth) / 2;// ����
|
|
|
+ g2.drawString(text, 0, fontSize - 8);
|
|
|
+
|
|
|
+ g2.dispose();
|
|
|
+ ImageIO.write(bi, "BMP", os);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<String> toStringList(BufferedImage bi) {
|
|
|
+ List<String> result = new ArrayList<String>();
|
|
|
+ int width = bi.getWidth();
|
|
|
+ int height = bi.getHeight();
|
|
|
+ int minX = bi.getMinX();
|
|
|
+ int minY = bi.getMinY();
|
|
|
+ int[] rgb1 = new int[3];
|
|
|
+ int[] rgb2 = new int[3];
|
|
|
+ for (int y = minY + 1; y < height; y = y + 2) {
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ for (int x = minX; x < width; x++) {
|
|
|
+ int pixel1 = bi.getRGB(x, y - 1);
|
|
|
+ rgb1[0] = (pixel1 & 0xff0000) >> 16; // r
|
|
|
+ rgb1[1] = (pixel1 & 0xff00) >> 8; // g
|
|
|
+ rgb1[2] = (pixel1 & 0xff); // b
|
|
|
+ int pixel2 = bi.getRGB(x, y);
|
|
|
+ rgb2[0] = (pixel2 & 0xff0000) >> 16; // r
|
|
|
+ rgb2[1] = (pixel2 & 0xff00) >> 8; // g
|
|
|
+ rgb2[2] = (pixel2 & 0xff); // b
|
|
|
+ int totalValue = 0;
|
|
|
+ for (int value : rgb1) {
|
|
|
+ totalValue += value;
|
|
|
+ }
|
|
|
+ for (int value : rgb2) {
|
|
|
+ totalValue += value;
|
|
|
+ }
|
|
|
+ String str;
|
|
|
+ if (totalValue < 256) {
|
|
|
+ str = "��";
|
|
|
+ } else if (totalValue < 768) {
|
|
|
+ str = "��";
|
|
|
+ } else {
|
|
|
+ str = " ";
|
|
|
+ }
|
|
|
+ sb.append(str);
|
|
|
+ }
|
|
|
+ result.add(sb.toString());
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<String> toStringList(String text, boolean vertical, int fontSize, boolean isTextBlack) throws Exception {
|
|
|
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
|
+ drawChinese(text, vertical, fontSize, isTextBlack, os);
|
|
|
+ os.close();
|
|
|
+ InputStream is = new ByteArrayInputStream(os.toByteArray());
|
|
|
+ BufferedImage bi = ImageIO.read(is);
|
|
|
+ is.close();
|
|
|
+ List<String> strList = toStringList(bi);
|
|
|
+ return strList;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+ {
|
|
|
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
|
+ drawEnglish("good!", 64, false, os);
|
|
|
+ os.close();
|
|
|
+ InputStream is = new ByteArrayInputStream(os.toByteArray());
|
|
|
+ BufferedImage bi = ImageIO.read(is);
|
|
|
+ is.close();
|
|
|
+ List<String> strList = toStringList(bi);
|
|
|
+ for (String str : strList) {
|
|
|
+ System.out.println(str);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ {
|
|
|
+ List<String> strList = toStringList("ţ��", false, 64, false);
|
|
|
+ for (String str : strList) {
|
|
|
+ System.out.println(str);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ {
|
|
|
+ OutputStream os = new FileOutputStream("cow.bmp");
|
|
|
+ drawChinese("ţ��", false, 64, false, os);
|
|
|
+ os.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ {
|
|
|
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
|
+ drawLaugh(128, os);
|
|
|
+ os.close();
|
|
|
+ InputStream is = new ByteArrayInputStream(os.toByteArray());
|
|
|
+ BufferedImage bi = ImageIO.read(is);
|
|
|
+ is.close();
|
|
|
+ List<String> strList = toStringList(bi);
|
|
|
+ for (String str : strList) {
|
|
|
+ System.out.println(str);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ {
|
|
|
+ OutputStream os = new FileOutputStream("Laugh.bmp");
|
|
|
+ drawLaugh(128, os);
|
|
|
+ os.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ {
|
|
|
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
|
+ drawCry(128, os);
|
|
|
+ os.close();
|
|
|
+ InputStream is = new ByteArrayInputStream(os.toByteArray());
|
|
|
+ BufferedImage bi = ImageIO.read(is);
|
|
|
+ is.close();
|
|
|
+ List<String> strList = toStringList(bi);
|
|
|
+ for (String str : strList) {
|
|
|
+ System.out.println(str);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ {
|
|
|
+ OutputStream os = new FileOutputStream("Cry.bmp");
|
|
|
+ drawCry(128, os);
|
|
|
+ os.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|