/
RazmikAr
/
Course_paper
Обзор
Документация
Войти
/
RazmikAr
/
Course_paper
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
ImageConvert.java
86 строк
3 KB
RazmikAr
Course_paper
19 фев 2026, 22:05
Верифицирован
19 фев 2026, 22:05
709dc6e
Код
Авторство
О чём код?
package ru.netology.graphics.image; import ru.netology.graphics.image.TextColorSchema; import ru.netology.graphics.image.BadImageSizeException; import ru.netology.graphics.image.NewSchema; import ru.netology.graphics.image.TextGraphicsConverter; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.URL; public class ImageConvert implements TextGraphicsConverter { private int maxWidth = Integer.MAX_VALUE; private int maxHeight = Integer.MAX_VALUE; private double maxRatio = Double.MAX_VALUE; private TextColorSchema schema = new NewSchema(); @Override public String convert(String url) throws IOException, BadImageSizeException { BufferedImage img = ImageIO.read(new URL(url)); if (img == null) { throw new IOException("Неверная ссылка для скачивания: " + url); } int width = img.getWidth(); int height = img.getHeight(); double normRatio = (double) width / height; if (normRatio > maxRatio) { throw new BadImageSizeException(normRatio, maxRatio); } int newWidth = width; int newHeight = height; if (width > maxWidth || height > maxHeight) { double scale = Math.min((double) maxWidth / width, (double) maxHeight / height); newWidth = (int) (width * scale); newHeight = (int) (height * scale); } BufferedImage sizeImg = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB); sizeImg.getGraphics().drawImage( img.getScaledInstance(newWidth, newHeight, java.awt.Image.SCALE_SMOOTH), 0, 0, null ); StringBuilder result = new StringBuilder(); for (int y = 0; y < newHeight; y++) { for (int x = 0; x < newWidth; x++) { int rgb = sizeImg.getRGB(x, y); result.append(schema.convert(rgb)); } result.append('\n'); } return result.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; } }