/
Letta21
/
Converter
Обзор
Документация
Войти
/
Letta21
/
Converter
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
graphics/image/ImageToTextConverter.java
98 строк
3 KB
Letta21
upload files
19 ноя 2025, 00:23
19 ноя 2025, 00:23
cb6a26b
Код
Авторство
О чём код?
package ru.netology.graphics.image; import ru.netology.graphics.image.BadImageSizeException; import ru.netology.graphics.image.ImplemTextColorSchema; import ru.netology.graphics.image.TextColorSchema; import ru.netology.graphics.image.TextGraphicsConverter; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.awt.image.WritableRaster; import java.io.IOException; import java.net.URL; public class ImageToTextConverter implements TextGraphicsConverter { private int maxWidth; private int maxHeight; private double maxRatio; private TextColorSchema schema; public ImageToTextConverter() { // Устанавливаем схему по умолчанию при создании объекта this.schema = new ImplemTextColorSchema(); } @Override public String convert(String url) throws IOException, BadImageSizeException { BufferedImage img = ImageIO.read(new URL(url)); if (maxRatio > 0) { double currentWidth = img.getWidth(); double currentHeight = img.getHeight(); double currentRatio = currentWidth > currentHeight ? currentWidth / currentHeight : currentHeight / currentWidth; if (currentRatio > maxRatio) { throw new BadImageSizeException(currentRatio, maxRatio); } } int currentWidth = img.getWidth(); int currentHeight = img.getHeight(); int newWidth = currentWidth; int newHeight = currentHeight; double widthRatio = (maxWidth > 0 && currentWidth > maxWidth) ? (double) currentWidth / maxWidth : 1.0; double heightRatio = (maxHeight > 0 && currentHeight > maxHeight) ? (double) currentHeight / maxHeight : 1.0; double scalingFactor = Math.max(widthRatio, heightRatio); if (scalingFactor > 1.0) { newWidth = (int) (currentWidth / scalingFactor); newHeight = (int) (currentHeight / scalingFactor); } Image scaledImage = img.getScaledInstance(newWidth, newHeight, BufferedImage.SCALE_SMOOTH); BufferedImage bwImg = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_BYTE_GRAY); Graphics2D graphics = bwImg.createGraphics(); graphics.drawImage(scaledImage, 0, 0, null); WritableRaster bwRaster = bwImg.getRaster(); StringBuilder sb = new StringBuilder(); int[] pixelColor = new int[3]; for (int h = 0; h < newHeight; h++) { for (int w = 0; w < newWidth; w++) { int color = bwRaster.getPixel(w, h, pixelColor)[0]; char c = schema.convert(color); // Добавляем символ дважды для лучшей читаемости sb.append(c).append(c); } sb.append("\n"); // Добавляем перенос строки в конце каждого ряда } return sb.toString(); } @Override public void setMaxWidth(int width) { this.maxWidth = width; } @Override public void setMaxHeight(int height) { this.maxHeight = height; } @Override public void setMaxRatio(double maxRatio) { this.maxRatio = maxRatio; } @Override public void setTextColorSchema(TextColorSchema schema) { this.schema = schema; } }