|
| 1 | +package net.codingme.banner.util; |
| 2 | + |
| 3 | +import java.awt.*; |
| 4 | +import java.awt.image.BufferedImage; |
| 5 | +import java.io.File; |
| 6 | +import java.io.FileOutputStream; |
| 7 | +import java.io.IOException; |
| 8 | + |
| 9 | +import javax.imageio.ImageIO; |
| 10 | + |
| 11 | +/** |
| 12 | + * <p> |
| 13 | + * 根据图片生成字符图案 |
| 14 | + * 1.图片大小缩放 |
| 15 | + * 2.遍历图片像素点 |
| 16 | + * 3.获取图片像素点亮度 |
| 17 | + * 4.匹配字符 |
| 18 | + * 5.输出图案 |
| 19 | + * |
| 20 | + * @author niujinpeng |
| 21 | + * @website www.codingme.net |
| 22 | + * @date 2019-02-25 23:03:01 |
| 23 | + */ |
| 24 | + |
| 25 | +public class GeneratorTextImage { |
| 26 | + private static final char[] PIXEL = {'@', '#', '8', '&', 'o', ':', '*', '.', ' '}; |
| 27 | + |
| 28 | + public static void main(String[] args) throws Exception { |
| 29 | + // 图片缩放 |
| 30 | + BufferedImage bufferedImage = makeSmallImage("src/main/resources/banner.jpg"); |
| 31 | + // 输出 |
| 32 | + printImage(bufferedImage); |
| 33 | + } |
| 34 | + |
| 35 | + public static void printImage(BufferedImage image) throws IOException { |
| 36 | + int width = image.getWidth(); |
| 37 | + int height = image.getHeight(); |
| 38 | + for (int i = 0; i < height; i++) { |
| 39 | + for (int j = 0; j < width; j++) { |
| 40 | + int rgb = image.getRGB(j, i); |
| 41 | + Color color = new Color(rgb); |
| 42 | + int red = color.getRed(); |
| 43 | + int green = color.getGreen(); |
| 44 | + int blue = color.getBlue(); |
| 45 | + // 一个用于计算RGB像素点亮度的公式 |
| 46 | + Double luminace = 0.2126 * red + 0.7152 * green + 0.0722 * blue; |
| 47 | + double index = luminace / (Math.ceil(255 / PIXEL.length) + 0.5); |
| 48 | + System.out.print(PIXEL[(int)(Math.floor(index))]); |
| 49 | + } |
| 50 | + System.out.println(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public static BufferedImage makeSmallImage(String srcImageName) throws Exception { |
| 55 | + File srcImageFile = new File(srcImageName); |
| 56 | + if (srcImageFile == null) { |
| 57 | + System.out.println("文件不存在"); |
| 58 | + return null; |
| 59 | + } |
| 60 | + FileOutputStream fileOutputStream = null; |
| 61 | + BufferedImage tagImage = null; |
| 62 | + Image srcImage = null; |
| 63 | + try { |
| 64 | + srcImage = ImageIO.read(srcImageFile); |
| 65 | + int srcWidth = srcImage.getWidth(null);// 原图片宽度 |
| 66 | + int srcHeight = srcImage.getHeight(null);// 原图片高度 |
| 67 | + int dstMaxSize = 90;// 目标缩略图的最大宽度/高度,宽度与高度将按比例缩写 |
| 68 | + int dstWidth = srcWidth;// 缩略图宽度 |
| 69 | + int dstHeight = srcHeight;// 缩略图高度 |
| 70 | + float scale = 0; |
| 71 | + // 计算缩略图的宽和高 |
| 72 | + if (srcWidth > dstMaxSize) { |
| 73 | + dstWidth = dstMaxSize; |
| 74 | + scale = (float)srcWidth / (float)dstMaxSize; |
| 75 | + dstHeight = Math.round((float)srcHeight / scale); |
| 76 | + } |
| 77 | + srcHeight = dstHeight; |
| 78 | + if (srcHeight > dstMaxSize) { |
| 79 | + dstHeight = dstMaxSize; |
| 80 | + scale = (float)srcHeight / (float)dstMaxSize; |
| 81 | + dstWidth = Math.round((float)dstWidth / scale); |
| 82 | + } |
| 83 | + // 生成缩略图 |
| 84 | + tagImage = new BufferedImage(dstWidth, dstHeight, BufferedImage.TYPE_INT_RGB); |
| 85 | + tagImage.getGraphics().drawImage(srcImage, 0, 0, dstWidth, dstHeight, null); |
| 86 | + return tagImage; |
| 87 | + } finally { |
| 88 | + if (fileOutputStream != null) { |
| 89 | + try { |
| 90 | + fileOutputStream.close(); |
| 91 | + } catch (Exception e) { |
| 92 | + } |
| 93 | + fileOutputStream = null; |
| 94 | + } |
| 95 | + tagImage = null; |
| 96 | + srcImage = null; |
| 97 | + System.gc(); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments