把月饼放入代码

2023年 9月 16日 37.3k 0

Springboot启动时会显示一个默认的banner。我们要做的就是把月饼放入代码,在启动时显示。文章最后有完整代码

image-20230913092503770.png

把月饼放入代码总共分几步?

  • 有一个月饼图像
  • 用代码将月饼转成ASCII字符图案
  • 启动时在控制台显示
  • 1、拥有一个月饼图片

    随便到网上搜就可以了,这里提供一个我用的图案,可以自由替代

    图案不要太过于复杂,在有限的空间内显示过于复杂的图像效果不是很好

    月饼.png

    2、转成ASCII字符图案

    简单说以转换的步骤,首先需要读取图片到 BufferedImage图像缓冲区之中,然后将其转为灰度图案,最后再转为ASCII字符图案,下面将分步解决每个问题。

    步骤1:读取图片到 BufferedImage之中

    Java内部就提供了javax.imageio.ImageIO类,可以直接读取和写入图像文件。使用ImageIO.read()方法可以读取一张图片,并将其存储在BufferedImage中。

    String imageFilePath = "替换成你自己的图片路径"; // 替换成你自己的图片路径
    int newWidth = 100;
    try {
        BufferedImage image = ImageIO.read(new File(imageFilePath));
    } catch (IOException e) {
        e.printStackTrace();
    }
    

    步骤二:转为灰度图案

    我们先要获取到原图像的宽和高,然后创建一个与彩色图像大小相同的新黑白图像。使用BufferedImage.TYPE_BYTE_GRAY参数创建一个灰度图像。然后两个嵌套的循环,通过遍历输入图像的每个像素点来执行灰度值的转换。

    Color color = new Color(image.getRGB(x, y));: 这一行获取位于(x, y)坐标处的彩色像素的颜色信息。Color对象包含了红色、绿色和蓝色通道的值。

    计算红、绿、蓝三种颜色通道的灰度值,根据人眼对于颜色的感知度不同,每种颜色值再乘上不同的权重因子。

    将红、绿、蓝通道的灰度值相加,得到像素点的总灰度值。将计算得到的灰度值作为红、绿、蓝通道的值,创建一个新的Color对象,并将该颜色设置为灰度图像中相应位置的像素值。

    通过嵌套循环,将彩色图像的每个像素转换为灰度值,最终生成了一个灰度图像。

    // 将彩色图像的每个像素转换为灰度值,只包含灰度信息,每个像素点都代表图像中对应位置的亮度值
    public static BufferedImage toGrayscale(BufferedImage image) {
        //获取图片宽高
        int width = image.getWidth();
        int height = image.getHeight();
        // 创建一个黑白图像
        BufferedImage grayscaleImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
        // 遍历彩色图像,计算灰度值
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                Color color = new Color(image.getRGB(x, y));
                int red = (int)(color.getRed() * 0.299);
                int green = (int)(color.getGreen() * 0.587);
                int blue = (int)(color.getBlue() * 0.114);
                int luminance = red + green + blue;
                grayscaleImage.setRGB(x, y, new Color(luminance, luminance, luminance).getRGB());
            }
        }
        return grayscaleImage;
    }
    

    步骤三:转为ASCII字符图案

    获取输入图像的宽度和高度。创建了一个StringBuilder对象,用于构建ASCII字符的字符串。两个嵌套的循环,通过遍历输入灰度图像的每个像素点来获取ASCII字符。根据计算得到的灰度值将其映射到ASCII字符集(定义在ASCII_CHARS变量中)中的一个索引。根据灰度值计算得到的ASCII字符添加到asciiStr字符串中,构建ASCII艺术图像的一行。

    asciiStr.append('n');: 这一行在每行结束时添加一个换行符,以确保ASCII艺术图像的每行都位于单独的一行。

    最后返回构建好的字符串

    public static String toAscii(BufferedImage image) {
        int width = image.getWidth();
        int height = image.getHeight();
        StringBuilder asciiStr = new StringBuilder();
    
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                int brightness = 255 - image.getRGB(x, y) & 0xFF;
                int index = brightness * (ASCII_CHARS.length() - 1) / 255;
                asciiStr.append(ASCII_CHARS.charAt(index));
            }
            asciiStr.append('n');
        }
    
        return asciiStr.toString();
    }
    

    步骤四:重设图片大小

    这个步骤可以加上也可以省略

    public static BufferedImage resize(BufferedImage image, int newWidth) {
        int oldWidth = image.getWidth();
        int oldHeight = image.getHeight();
        double aspectRatio = (double)oldHeight / oldWidth;
        int newHeight = (int)(aspectRatio * newWidth);
        BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_BYTE_GRAY);
        resizedImage.getGraphics().drawImage(image.getScaledInstance(newWidth, newHeight, BufferedImage.SCALE_SMOOTH), 0, 0, null);
        return resizedImage;
    }
    

    3、放入控制台中显示

    我们现在已经有一个字符串图形,可以新建一个txt文件将其复制进去。文件目录位置src/main/resources/banner.txt

    也可以选择IO方式自动写入到文件内。具体代码如下

    // 写入banner
    public static void writeToFile(String asciiStr) {
        try {
            FileWriter writer = new FileWriter("src/main/resources/banner.txt");
            writer.write(asciiStr);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

    此刻,我们再重启SpringBoot服务,就会在控制台输出我们刚制作好的图案了。

    image-20230913103327942.png

    最后

    最后祝大家中秋快乐,玩得开心

    下面是完整的代码

    
    public class ImageToAscii {
    
        // ASCII字符集
        public static final String ASCII_CHARS = "@%#*+=-:. ";
    
        public static void main(String[] args) {
            String imageFilePath = "替换成你自己的图片路径"; // 替换成你自己的图片路径
            int newWidth = 100;
            try {
                BufferedImage image = ImageIO.read(new File(imageFilePath));
                BufferedImage grayscaleImage = toGrayscale(image);
                BufferedImage resizedImage = resize(grayscaleImage, newWidth);
                String asciiStr = toAscii(resizedImage);
                ImageToAscii.writeToFile(asciiStr);
                System.out.println(asciiStr);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 将彩色图像的每个像素转换为灰度值,只包含灰度信息,每个像素点都代表图像中对应位置的亮度值
        public static BufferedImage toGrayscale(BufferedImage image) {
            int width = image.getWidth();
            int height = image.getHeight();
            BufferedImage grayscaleImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
    
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    Color color = new Color(image.getRGB(x, y));
                    int red = (int) (color.getRed() * 0.299);
                    int green = (int) (color.getGreen() * 0.587);
                    int blue = (int) (color.getBlue() * 0.114);
                    int luminance = red + green + blue;
                    grayscaleImage.setRGB(x, y, new Color(luminance, luminance, luminance).getRGB());
                }
            }
            return grayscaleImage;
        }
    
    //    重设图片宽高
        public static BufferedImage resize(BufferedImage image, int newWidth) {
            int oldWidth = image.getWidth();
            int oldHeight = image.getHeight();
            double aspectRatio = (double) oldHeight / oldWidth;
            int newHeight = (int) (aspectRatio * newWidth);
            BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_BYTE_GRAY);
            resizedImage.getGraphics().drawImage(image.getScaledInstance(newWidth, newHeight, BufferedImage.SCALE_SMOOTH), 0, 0, null);
            return resizedImage;
        }
    
        public static String toAscii(BufferedImage image) {
            int width = image.getWidth();
            int height = image.getHeight();
            StringBuilder asciiStr = new StringBuilder();
    
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    int brightness = 255 - image.getRGB(x, y) & 0xFF;
                    int index = brightness * (ASCII_CHARS.length() - 1) / 255;
                    asciiStr.append(ASCII_CHARS.charAt(index));
                }
                asciiStr.append('n');
            }
    
            return asciiStr.toString();
        }
    
        // 写入banner
        public static void writeToFile(String asciiStr) {
            try {
                FileWriter writer = new FileWriter("src/main/resources/banner.txt");
                writer.write(asciiStr);
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    相关文章

    JavaScript2024新功能:Object.groupBy、正则表达式v标志
    PHP trim 函数对多字节字符的使用和限制
    新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
    使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
    为React 19做准备:WordPress 6.6用户指南
    如何删除WordPress中的所有评论

    发布评论