|
| 1 | +package com.wdbyte.io.image; |
| 2 | + |
| 3 | +import java.awt.*; |
| 4 | +import java.awt.image.BufferedImage; |
| 5 | +import java.io.File; |
| 6 | +import java.io.IOException; |
| 7 | + |
| 8 | +import javax.imageio.ImageIO; |
| 9 | + |
| 10 | +/** |
| 11 | + * <p> |
| 12 | + * 根据图片生成字符图案 |
| 13 | + * 1.图片大小缩放 |
| 14 | + * 2.遍历图片像素点 |
| 15 | + * 3.获取图片像素点亮度 |
| 16 | + * 4.匹配字符 |
| 17 | + * 5.输出图案 |
| 18 | + * |
| 19 | + * @website https://www.wdbyte.com |
| 20 | + */ |
| 21 | +public class GeneratorTextImageMini { |
| 22 | + |
| 23 | + public static void main(String[] args) throws Exception { |
| 24 | + //BufferedImage image = resizeImage("/Users/darcy/Downloads/sanli.jpg", 120); |
| 25 | + BufferedImage image = ImageIO.read(new File("/Users/darcy/Downloads/刘看山.jpg")); |
| 26 | + printImage(image); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * 图片缩放 |
| 31 | + * |
| 32 | + * @param srcImagePath 图片路径 |
| 33 | + * @param targetWidth 目标宽度 |
| 34 | + * @return |
| 35 | + * @throws IOException |
| 36 | + */ |
| 37 | + public static BufferedImage resizeImage(String srcImagePath, int targetWidth) throws IOException { |
| 38 | + Image srcImage = ImageIO.read(new File(srcImagePath)); |
| 39 | + int targetHeight = getTargetHeight(targetWidth, srcImage); |
| 40 | + BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB); |
| 41 | + Graphics2D graphics2D = resizedImage.createGraphics(); |
| 42 | + graphics2D.drawImage(srcImage, 0, 0, targetWidth, targetHeight, null); |
| 43 | + graphics2D.dispose(); |
| 44 | + return resizedImage; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * 根据指定宽度,计算等比例高度 |
| 49 | + * |
| 50 | + * @param targetWidth 目标宽度 |
| 51 | + * @param srcImage 图片信息 |
| 52 | + * @return |
| 53 | + */ |
| 54 | + private static int getTargetHeight(int targetWidth, Image srcImage) { |
| 55 | + int targetHeight = srcImage.getHeight(null); |
| 56 | + if (targetWidth < srcImage.getWidth(null)) { |
| 57 | + targetHeight = Math.round((float)targetHeight / ((float)srcImage.getWidth(null) / (float)targetWidth)); |
| 58 | + } |
| 59 | + return targetHeight; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * 图片打印 |
| 64 | + * |
| 65 | + * @param image |
| 66 | + * @throws IOException |
| 67 | + */ |
| 68 | + public static void printImage(BufferedImage image) throws IOException { |
| 69 | + final char[] PIXEL_CHAR_ARRAY = {'W', '@', '#', '8', '&', '*', 'o', ':', '.', ' '}; |
| 70 | + //final char[] PIXEL_CHAR_ARRAY = {',', '.', ' '}; |
| 71 | + int width = image.getWidth(); |
| 72 | + int height = image.getHeight(); |
| 73 | + for (int i = 0; i < height; i++) { |
| 74 | + for (int j = 0; j < width; j++) { |
| 75 | + int rgb = image.getRGB(j, i); |
| 76 | + Color color = new Color(rgb); |
| 77 | + int red = color.getRed(); |
| 78 | + int green = color.getGreen(); |
| 79 | + int blue = color.getBlue(); |
| 80 | + // 一个用于计算RGB像素点灰度的公式 |
| 81 | + Double grayscale = 0.2126 * red + 0.7152 * green + 0.0722 * blue; |
| 82 | + double index = grayscale / (Math.ceil(255 / PIXEL_CHAR_ARRAY.length) + 0.5); |
| 83 | + System.out.print(PIXEL_CHAR_ARRAY[(int)(Math.floor(index))]); |
| 84 | + } |
| 85 | + System.out.println(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments