Parcourir la source

增加字符画

menglu il y a 3 ans
Parent
commit
7eb25e94bb

+ 41 - 2
ibms-data-sdk/src/main/java/com/persagy/ibms/data/sdk/service/DaemonThread.java

@@ -1,12 +1,17 @@
 package com.persagy.ibms.data.sdk.service;
 
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.InputStream;
 import java.net.URI;
 import java.util.Date;
 import java.util.List;
 import java.util.concurrent.CopyOnWriteArrayList;
 
 import javax.annotation.PostConstruct;
+import javax.imageio.ImageIO;
 
 import org.springframework.stereotype.Component;
 
@@ -18,6 +23,7 @@ import com.persagy.ibms.core.util.RepositoryComputeThread;
 import com.persagy.ibms.data.sdk.test.CheckTest;
 import com.persagy.ibms.data.sdk.util.Constant;
 import com.persagy.ibms.data.sdk.util.DeamonUtil;
+import com.persagy.ibms.data.sdk.util.ImageUtil;
 import com.persagy.ibms.data.sdk.util.RWDLoadUtil;
 import com.persagy.ibms.data.sdk.util.RWDRepositoryUtil;
 import com.persagy.ibms.data.sdk.util.RepositoryContainer;
@@ -170,8 +176,6 @@ public class DaemonThread extends Thread {
 				path_zkt_physical_world = tmp_zkt_physical_world;
 				path_ibms_physical_world = tmp_ibms_physical_world;
 				DaemonThread.initialized = true;
-				// 查询可变集合
-				// RecursiveUtil.print_variable_set("", Repository.objectData);
 
 				if (Constant.iot_websocket_url != null) {
 					log.warn("iot-websocket");
@@ -197,9 +201,44 @@ public class DaemonThread extends Thread {
 					// log.info("连接成功");
 					Thread.sleep(1000L);
 				}
+				boolean isTextBlack = false;
+				if (LogOfRun.errorList.size() == 0) {
+					List<String> strList = ImageUtil.toStringList("牛逼", false, 64, isTextBlack);
+					for (String str : strList) {
+						log.warn(str);
+					}
+				} else {
+					ByteArrayOutputStream os = new ByteArrayOutputStream();
+					ImageUtil.drawLaugh(72, isTextBlack, os);
+					os.close();
+					InputStream is = new ByteArrayInputStream(os.toByteArray());
+					BufferedImage bi = ImageIO.read(is);
+					is.close();
+					List<String> strList = ImageUtil.toStringList(bi);
+					for (String str : strList) {
+						log.warn(str);
+					}
+				}
+
 				LogOfRun.status = "success";
 				RepositoryContainer.RepositoryProject.AddSuccessLogOfRun(LogOfRun);
 			} catch (Exception e) {
+				boolean isTextBlack = false;
+				try {
+					ByteArrayOutputStream os = new ByteArrayOutputStream();
+					ImageUtil.drawCry(72, isTextBlack, os);
+					os.close();
+					InputStream is = new ByteArrayInputStream(os.toByteArray());
+					BufferedImage bi = ImageIO.read(is);
+					is.close();
+					List<String> strList = ImageUtil.toStringList(bi);
+					for (String str : strList) {
+						log.warn(str);
+					}
+				} catch (Exception e1) {
+					e1.printStackTrace();
+				}
+
 				String message = LogUtil.GetExceptionStackTrace(e);
 				log.error(e.getMessage(), e);
 				LogOfRun.error(null, message);

+ 43 - 0
ibms-data-sdk/src/main/java/com/persagy/ibms/data/sdk/service/RefreshThread.java

@@ -1,14 +1,21 @@
 package com.persagy.ibms.data.sdk.service;
 
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
 import java.util.Date;
+import java.util.List;
 
 import javax.annotation.PostConstruct;
+import javax.imageio.ImageIO;
 
 import org.springframework.stereotype.Component;
 
 import com.persagy.ibms.core.util.LogOfRun;
 import com.persagy.ibms.core.util.LogUtil;
 import com.persagy.ibms.data.sdk.util.Constant;
+import com.persagy.ibms.data.sdk.util.ImageUtil;
 import com.persagy.ibms.data.sdk.util.RWDRepositoryUtil;
 import com.persagy.ibms.data.sdk.util.RepositoryContainer;
 import com.persagy.ibms.data.sdk.util.RepositoryImpl;
@@ -155,9 +162,45 @@ public class RefreshThread extends Thread {
 					DaemonThread.path_physical_world = this.path_physical_world;
 					DaemonThread.path_zkt_physical_world = this.path_zkt_physical_world;
 					DaemonThread.path_ibms_physical_world = this.path_ibms_physical_world;
+
+					boolean isTextBlack = false;
+					if (LogOfRun.errorList.size() == 0) {
+						List<String> strList = ImageUtil.toStringList("牛逼", false, 64, isTextBlack);
+						for (String str : strList) {
+							log.warn(str);
+						}
+					} else {
+						ByteArrayOutputStream os = new ByteArrayOutputStream();
+						ImageUtil.drawLaugh(72, isTextBlack, os);
+						os.close();
+						InputStream is = new ByteArrayInputStream(os.toByteArray());
+						BufferedImage bi = ImageIO.read(is);
+						is.close();
+						List<String> strList = ImageUtil.toStringList(bi);
+						for (String str : strList) {
+							log.warn(str);
+						}
+					}
+
 					LogOfRun.status = "success";
 					RepositoryContainer.RepositoryProject.AddSuccessLogOfRun(LogOfRun);
 				} catch (Exception e) {
+					boolean isTextBlack = false;
+					try {
+						ByteArrayOutputStream os = new ByteArrayOutputStream();
+						ImageUtil.drawCry(72, isTextBlack, os);
+						os.close();
+						InputStream is = new ByteArrayInputStream(os.toByteArray());
+						BufferedImage bi = ImageIO.read(is);
+						is.close();
+						List<String> strList = ImageUtil.toStringList(bi);
+						for (String str : strList) {
+							log.warn(str);
+						}
+					} catch (Exception e1) {
+						e1.printStackTrace();
+					}
+
 					String message = LogUtil.GetExceptionStackTrace(e);
 					log.error(e.getMessage(), e);
 					LogOfRun.error(null, message);

+ 63 - 42
ibms-data-sdk/src/main/java/com/persagy/ibms/data/sdk/util/ImageUtil.java

@@ -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();
 		}
 	}