/
Sepa
/
new
Обзор
Документация
Войти
/
Sepa
/
new
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
TextGraphicsConverterRealization.java
98 строк
4 KB
Sepa
upload files
28 сен 2025, 14:56
28 сен 2025, 14:56
47d8c74
Код
Авторство
О чём код?
package ru.netology.graphics.image; 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 TextGraphicsConverterRealization implements TextGraphicsConverter { TextColorSchema schema = new TextColorSchemaRealization(); private int maxWidth = Integer.MAX_VALUE; private int maxHeight = Integer.MAX_VALUE; private double maxRatio = Double.MAX_VALUE; public String convert(String url) throws IOException, BadImageSizeException { try { BufferedImage img = ImageIO.read(new URL(url)); double ratio = (double) img.getWidth() / img.getHeight(); if (ratio > maxRatio) { throw new BadImageSizeException(ratio, maxRatio); } int newWidth = img.getWidth(); int newHeight = img.getHeight(); if (newWidth > maxWidth) { double widthRatio = (double) maxWidth / newWidth; newHeight = (int) (newHeight * widthRatio); newWidth = maxWidth; } if (newHeight > maxHeight) { double heightRatio = (double) maxHeight / newHeight; newWidth = (int) (newWidth * heightRatio); newHeight = maxHeight; } 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); graphics.dispose(); WritableRaster bwRaster = bwImg.getRaster(); char[][] asciiArt = new char[newHeight][newWidth]; int[] pixelData = new int[3]; for (int h = 0; h < newHeight; h++) { for (int w = 0; w < newWidth; w++) { int color = bwRaster.getPixel(w, h, pixelData)[0]; char c = schema.convert(color); asciiArt[h][w] = c; } } StringBuilder result = new StringBuilder(); for (int h = 0; h < newHeight; h++) { for (int w = 0; w < newWidth; w++) { result.append(asciiArt[h][w]); result.append(asciiArt[h][w]); } result.append("\n"); } return result.toString(); } catch (IOException e) { throw new IOException("Ошибка при загрузке изображения по URL: " + url, e); } } public void setMaxWidth(int width) { if (width <= 0) { throw new IllegalArgumentException("Максимальная ширина должна быть положительным числом. Получено: " + width); } this.maxWidth = width; } public void setMaxHeight(int height) { if (height <= 0) { throw new IllegalArgumentException("Максимальная высота должна быть положительным числом. Получено:" + height); } this.maxHeight = height; } public void setMaxRatio(double maxRatio) { if (maxRatio <= 0) { throw new IllegalArgumentException("Максимально соотношение сторон должно быть положительным числом. Получено :" + maxRatio); } this.maxRatio = maxRatio; } public void setTextColorSchema(TextColorSchema schema) { if (schema == null) { throw new IllegalArgumentException("Цветовая схема не может быть null"); } this.schema = schema; } }