prueba 2
This commit is contained in:
parent
5fb87af3e6
commit
85ac7e9566
|
@ -0,0 +1,56 @@
|
|||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class AsciiArtGenerator {
|
||||
|
||||
// Caracteres ordenados de menor a mayor intensidad
|
||||
private static final String ASCII_CHARS = "@%#*+=-:. ";
|
||||
|
||||
public static void main(String[] args) {
|
||||
if (args.length < 1) {
|
||||
System.out.println("Uso: java AsciiArtGenerator <ruta de la imagen>");
|
||||
return;
|
||||
}
|
||||
|
||||
String imagePath = args[0];
|
||||
try {
|
||||
BufferedImage image = ImageIO.read(new File(imagePath));
|
||||
String asciiArt = convertToAscii(image);
|
||||
System.out.println(asciiArt);
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error al cargar la imagen: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static String convertToAscii(BufferedImage image) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int width = image.getWidth();
|
||||
int height = image.getHeight();
|
||||
|
||||
// Redimensionar la imagen para que quepa en la consola
|
||||
int newWidth = 100;
|
||||
int newHeight = (int) (height * ((double) newWidth / width) / 2); // Dividir por 2 para ajustar la proporción
|
||||
|
||||
BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
|
||||
resizedImage.getGraphics().drawImage(image, 0, 0, newWidth, newHeight, null);
|
||||
|
||||
for (int y = 0; y < newHeight; y++) {
|
||||
for (int x = 0; x < newWidth; x++) {
|
||||
int pixel = resizedImage.getRGB(x, y);
|
||||
int r = (pixel >> 16) & 0xff;
|
||||
int g = (pixel >> 8) & 0xff;
|
||||
int b = pixel & 0xff;
|
||||
int gray = (r + g + b) / 3; // Convertir a escala de grises
|
||||
|
||||
// Mapear el valor de gris a un carácter ASCII
|
||||
int index = gray * (ASCII_CHARS.length() - 1) / 255;
|
||||
sb.append(ASCII_CHARS.charAt(index));
|
||||
}
|
||||
sb.append("\n");
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue