|
| 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 GeneratorTextImage { |
| 22 | + |
| 23 | + public static void main(String[] args) throws Exception { |
| 24 | + BufferedImage image = resizeImage("/Users/darcy/Downloads/sanli.jpg", 120); |
| 25 | + printImage(image); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * 图片缩放 |
| 30 | + * |
| 31 | + * @param srcImagePath 图片路径 |
| 32 | + * @param targetWidth 目标宽度 |
| 33 | + * @return |
| 34 | + * @throws IOException |
| 35 | + */ |
| 36 | + public static BufferedImage resizeImage(String srcImagePath, int targetWidth) throws IOException { |
| 37 | + Image srcImage = ImageIO.read(new File(srcImagePath)); |
| 38 | + int targetHeight = getTargetHeight(targetWidth, srcImage); |
| 39 | + BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB); |
| 40 | + Graphics2D graphics2D = resizedImage.createGraphics(); |
| 41 | + graphics2D.drawImage(srcImage, 0, 0, targetWidth, targetHeight, null); |
| 42 | + graphics2D.dispose(); |
| 43 | + return resizedImage; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * 图片缩放 |
| 48 | + * |
| 49 | + * @param srcImagePath 图片路径 |
| 50 | + * @param targetWidth 目标宽度 |
| 51 | + * @return |
| 52 | + * @throws IOException |
| 53 | + */ |
| 54 | + public static BufferedImage resizeImage2(String srcImagePath, int targetWidth) throws IOException { |
| 55 | + Image srcImage = ImageIO.read(new File(srcImagePath)); |
| 56 | + int targetHeight = getTargetHeight(targetWidth, srcImage); |
| 57 | + Image image = srcImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_DEFAULT); |
| 58 | + BufferedImage bufferedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB); |
| 59 | + bufferedImage.getGraphics().drawImage(image, 0, 0, null); |
| 60 | + return bufferedImage; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * 根据指定宽度,计算等比例高度 |
| 65 | + * |
| 66 | + * @param targetWidth 目标宽度 |
| 67 | + * @param srcImage 图片信息 |
| 68 | + * @return |
| 69 | + */ |
| 70 | + private static int getTargetHeight(int targetWidth, Image srcImage) { |
| 71 | + int targetHeight = srcImage.getHeight(null); |
| 72 | + if (targetWidth < srcImage.getWidth(null)) { |
| 73 | + targetHeight = Math.round((float)targetHeight / ((float)srcImage.getWidth(null) / (float)targetWidth)); |
| 74 | + } |
| 75 | + return targetHeight; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * 图片打印 |
| 80 | + * |
| 81 | + * @param image |
| 82 | + * @throws IOException |
| 83 | + */ |
| 84 | + public static void printImage(BufferedImage image) throws IOException { |
| 85 | + final char[] PIXEL_CHAR_ARRAY = {'W', '@', '#', '8', '&', '*', 'o', ':', '.', ' '}; |
| 86 | + int width = image.getWidth(); |
| 87 | + int height = image.getHeight(); |
| 88 | + for (int i = 0; i < height; i++) { |
| 89 | + for (int j = 0; j < width; j++) { |
| 90 | + int rgb = image.getRGB(j, i); |
| 91 | + Color color = new Color(rgb); |
| 92 | + int red = color.getRed(); |
| 93 | + int green = color.getGreen(); |
| 94 | + int blue = color.getBlue(); |
| 95 | + // 一个用于计算RGB像素点灰度的公式 |
| 96 | + Double grayscale = 0.2126 * red + 0.7152 * green + 0.0722 * blue; |
| 97 | + double index = grayscale / (Math.ceil(255 / PIXEL_CHAR_ARRAY.length) + 0.5); |
| 98 | + System.out.print(PIXEL_CHAR_ARRAY[(int)(Math.floor(index))]); |
| 99 | + } |
| 100 | + System.out.println(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments