From b3b17b231e2f2840332ce6eb96f791865fdec6d5 Mon Sep 17 00:00:00 2001
From: zhubaomin <zhubaomin>
Date: 星期一, 14 四月 2025 16:38:16 +0800
Subject: [PATCH] 代码优化
---
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/AsciiPic.java | 91 +++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 91 insertions(+), 0 deletions(-)
diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/AsciiPic.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/AsciiPic.java
new file mode 100644
index 0000000..53f71b7
--- /dev/null
+++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/AsciiPic.java
@@ -0,0 +1,91 @@
+package com.dy.common.util;
+
+import javax.imageio.ImageIO;
+import javax.swing.*;
+import java.awt.*;
+import java.awt.image.BufferedImage;
+import java.io.FileInputStream;
+import java.io.IOException;
+
+/**
+ * 鍥剧墖杞寲Ascii
+ */
+public class AsciiPic {
+
+ /**
+ * 灏唅mage杞崲鎴� BufferedImage
+ *
+ */
+ public static BufferedImage toBufferedImage(Image image) {
+ if (image instanceof BufferedImage) {
+ return (BufferedImage)image;
+ }
+
+ // 鍔犺浇鎵�鏈夊儚绱�
+ image = new ImageIcon(image).getImage();
+ BufferedImage bImage = null;
+ GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
+ try {
+ int transparency = Transparency.OPAQUE;
+
+ // 鍒涘缓buffer鍥惧儚
+ GraphicsDevice gs = ge.getDefaultScreenDevice();
+ GraphicsConfiguration gc = gs.getDefaultConfiguration();
+ bImage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
+ } catch (HeadlessException e) {
+ e.printStackTrace();
+ }
+ if (bImage == null) {
+ int type = BufferedImage.TYPE_INT_RGB;
+ bImage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
+ }
+ // 澶嶅埗
+ Graphics g = bImage.createGraphics();
+ // 璧嬪��
+ g.drawImage(image, 0, 0, null);
+ g.dispose();
+ return bImage;
+ }
+
+ public static Image creatImage(String ImgPath) {
+ Image srcImg = null;
+ try {
+ srcImg = ImageIO.read(new FileInputStream(ImgPath));
+ } catch (Exception e1) {
+ e1.printStackTrace();
+ }
+ Image smallImg = null ;
+ if(srcImg != null){
+ //鍙栨簮鍥�
+ int width = 60; //鍋囪瑕佺缉灏忓埌200鐐瑰儚绱�
+ int height = srcImg.getHeight(null)*60/srcImg.getWidth(null);//鎸夋瘮渚嬶紝灏嗛珮搴︾缉鍑�
+ smallImg = srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH);//缂╁皬
+ }
+ return smallImg;
+ }
+
+ /**
+ * @param bfImage 鍥剧墖璺緞
+ */
+ public static void createAsciiPic(BufferedImage bfImage) throws IOException {
+ final String base = "@#&$O";// 瀛楃涓茬敱澶嶆潅鍒扮畝鍗�
+ for (int y = 0; y < bfImage.getHeight(); y += 2) {
+ for (int x = 0; x < bfImage.getWidth(); x++) {
+ final int pixel = bfImage.getRGB(x, y);
+ final int r = (pixel & 0xff0000) >> 16, g = (pixel & 0xff00) >> 8, b = pixel & 0xff;
+ final float gray = 0.299f * r + 0.578f * g + 0.114f * b;
+ final int index = Math.round(gray * (base.length() + 1) / 255);
+ System.out.print(index >= base.length() ? " " : String.valueOf(base.charAt(index)));
+ }
+ System.out.println();
+ }
+ }
+
+ public static void main(final String[] args) {
+ try {
+ AsciiPic.createAsciiPic(toBufferedImage(creatImage("DY.png")));
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+}
\ No newline at end of file
--
Gitblit v1.8.0