Existen varias formas de realizar este Ascii-Art, por ejemplo:
- Ascii Art de imagenes en Blanco y Negro
- Acii Art de imagenes en escala de grises
- Acii Art de los bordes de una imagen
¿Como funciona?
Cada 4 pixeles leidos, corresponden a un caracter ascii y estos seran unidos a una cadena de texto, cuando se llegue al final de una columna, debe agregarse un "salto de carro" (\n) y continuarse con la siguiente hilera.
import java.awt.Color; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; /** * @web http://jc-mouse.blogspot.com/ * @author Mouse */ public class i2ascii { private int r,g,b; private Color color; private int umbral = 127; private int negro = -16777216; private int blanco = -1; private String ascii = ""; // los caracteres por los que se reemplazan los pixeles // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15=\ private String[] caracter = {"`","'","°",",",".","\"","_","[","]","J","7","P","L","8","/","" + (char) 92}; private BufferedImage foto; public i2ascii(){ try { //se carga una foto foto = ImageIO.read(getClass().getResource("jcmouse.jpg")); //foto = ImageIO.read(getClass().getResource("jcmouse.jpg")); //se convierte la foto en blanco y negro foto = set_Blanco_y_Negro_con_Umbral(foto); } catch (IOException ex) { Logger.getLogger(i2ascii.class.getName()).log(Level.SEVERE, null, ex); } } public String ConvertToAscii(){ BufferedImage f = foto; int x = f.getWidth(); int y = f.getHeight(); //se recorre la matriz de la imagen fila x fila de tres en tres for(int j=0;j<y ;j=j+3 ){ //se recorre la matriz de la imagen col x col de dos en dos for(int i=0;i<x;i=i+2 ){ if( (i < (x-1)) && (j < (y-1)) ){ //__ 0 //__ if( f.getRGB(i, j)==blanco && f.getRGB(i+1, j)==blanco && f.getRGB(i, j+1)==blanco && f.getRGB(i+1, j+1)==blanco ){ ascii = ascii + caracter[0]; continue; } //_X 1 //__ if( f.getRGB(i, j)==blanco && f.getRGB(i+1, j)==negro && f.getRGB(i, j+1)==blanco && f.getRGB(i+1, j+1)==blanco ){ ascii = ascii + caracter[1]; continue; } //X_ 2 //__ if( f.getRGB(i, j)==negro && f.getRGB(i+1, j)==blanco && f.getRGB(i, j+1)==blanco && f.getRGB(i+1, j+1)==blanco ){ ascii = ascii + caracter[2]; continue; } //__ 3 //X_ if( f.getRGB(i, j)==blanco && f.getRGB(i+1, j)==blanco && f.getRGB(i, j+1)==negro && f.getRGB(i+1, j+1)==blanco ){ ascii = ascii + caracter[3]; continue; } //__ 4 //_X if( f.getRGB(i, j)==blanco && f.getRGB(i+1, j)==blanco && f.getRGB(i, j+1)==blanco && f.getRGB(i+1, j+1)==negro ){ ascii = ascii + caracter[4]; continue; } //XX 5 //__ if( f.getRGB(i, j)==negro && f.getRGB(i+1, j)==negro && f.getRGB(i, j+1)==blanco && f.getRGB(i+1, j+1)==blanco ){ ascii = ascii + caracter[5]; continue; } //__ 6 //XX if( f.getRGB(i, j)==blanco && f.getRGB(i+1, j)==blanco && f.getRGB(i, j+1)==negro && f.getRGB(i+1, j+1)==negro ){ ascii = ascii + caracter[6]; continue; } //X_ 7 //X_ if( f.getRGB(i, j)==negro && f.getRGB(i+1, j)==blanco && f.getRGB(i, j+1)==negro && f.getRGB(i+1, j+1)==blanco ){ ascii = ascii + caracter[7]; continue; } //_X 8 //_X if( f.getRGB(i, j)==blanco && f.getRGB(i+1, j)==negro && f.getRGB(i, j+1)==blanco && f.getRGB(i+1, j+1)==negro ){ ascii = ascii + caracter[8]; continue; } //_X 9 //XX if( f.getRGB(i, j)==blanco && f.getRGB(i+1, j)==negro && f.getRGB(i, j+1)==negro && f.getRGB(i+1, j+1)==negro ){ ascii = ascii + caracter[9]; continue; } //XX 10 //_X if( f.getRGB(i, j)==negro && f.getRGB(i+1, j)==negro && f.getRGB(i, j+1)==blanco && f.getRGB(i+1, j+1)==negro ){ ascii = ascii + caracter[10]; continue; } //XX 11 //X_ if( f.getRGB(i, j)==negro && f.getRGB(i+1, j)==negro && f.getRGB(i, j+1)==negro && f.getRGB(i+1, j+1)==blanco ){ ascii = ascii + caracter[11]; continue; } //X_ 12 //XX if( f.getRGB(i, j)==negro && f.getRGB(i+1, j)==blanco && f.getRGB(i, j+1)==negro && f.getRGB(i+1, j+1)==negro ){ ascii = ascii + caracter[12]; continue; } //XX 13 //XX if( f.getRGB(i, j)==negro && f.getRGB(i+1, j)==negro && f.getRGB(i, j+1)==negro && f.getRGB(i+1, j+1)==negro ){ ascii = ascii + caracter[13]; continue; } //_X 14 //X_ if( f.getRGB(i, j)==blanco && f.getRGB(i+1, j)==negro && f.getRGB(i, j+1)==negro && f.getRGB(i+1, j+1)==blanco ){ ascii = ascii + caracter[14]; continue; } //X_ 15 //_X if( f.getRGB(i, j)==negro && f.getRGB(i+1, j)==blanco && f.getRGB(i, j+1)==blanco && f.getRGB(i+1, j+1)==negro ){ ascii = ascii + caracter[15]; continue; } }// fin si } //se concatena la fila resultante y se añade un salto de linea ascii = ascii + "\n"; } //se retorna la imagen transformada en ASCII return ascii; } //Metodo dado una imagen, la convierte en imagen en blanco y negro private BufferedImage set_Blanco_y_Negro_con_Umbral(BufferedImage f){ BufferedImage bn = new BufferedImage(f.getWidth(),f.getHeight(), BufferedImage.TYPE_BYTE_BINARY); //se traspasan los colores Pixel a Pixel for(int i=0;i<f.getWidth();i++){ for(int j=0;j<f.getHeight();j++){ color = new Color(f.getRGB(i, j)); //se extraen los valores RGB r = color.getRed(); g = color.getGreen(); b = color.getBlue(); //dependiendo del valor del umbral, se van separando los // valores RGB a 0 y 255 r =(r>umbral)? 255: 0; g =(g>umbral)? 255: 0; b =(b>umbral)? 255: 0; bn.setRGB(i, j, new Color(r,g,b).getRGB()); } } return bn; } }
Implementar esta clase en una interfaz, es sencilla asi que no creo que sea necesario mostrar como se hace esto. Al ser este un algoritmo simple y utlizar imagenes en blanco y negro, las imagenes resultantes, tambien seran sencillas, es por eso que es mucho mejor utilizar esta tecnica Blanco/Negro en imagenes simples como logotipos o letras.
``````````````````````````````````````````````````````````_______888888_____```````````````````````` ``````````````````````````````````````````````````__888888888888888888888888888888__```````````````` ````````````````````````````````````````````__8888888888888888888888888888888888888888`````````````` ````````````````````````````````````````_888888888888888888888888888888888888888888888`````````````` ````````````````````````````````````_J88888888888888888888888888888888888888888888888[`````````````` `````````````````````````````````_888888888888888888888888888888888888888888888888888[`````````````` ``````````````````````````````_88888888888888888888888""""````````````""""88888888888°`````````````` ```````````````````````````.J888888888888888888P"`````````````````````````````""88888``````````````` `````````````````````````_8888888888888888P"``````````````````````````````````````"88``````````````` ```````````````````````J88888888888888P"````````````````````````````88L_,``````````````````````````` `````````````````````J8888888888888"````````````````````````````````88888888__`````````````````````` ```````````````````J888888888888"```````````````````````````````````888888888888_,`````````````````` `````````````````J88888888888"``````````````````````````````````````888888888888888L```````````````` ```````````````.8888888888"`````````````````````````````````````````88888888888888888L,````````````` `````````````.888888888P°```````````````````````````````````````````88888888888888888888,``````````` ````````````J88888888P````````````````````````````````````````````````"""7888888888888888L`````````` ``````````.88888888P````````````````.___J88888888888888L___``````````````````'"888888888888,```````` `````````.8888888P``````````````_J8888""````````````````'"8888L_`````````````````"8888888888,``````` ````````]8888888°````````````_888"```][``````````````````8[``'"888,````````````````'788888888[`````` ```````J888888P````````````J88°``````888````````````````88P``````"88,`````````````````78888888[````` ``````J888888°````````````.8°`````````8`````````````````'8`````````]8``````````````````'8888888[```` `````J888888°`````````````]8```````.888,````````````````.888,``````]8````````````````````7888888,``` ````]888888```````````````]8``````]P.8888,````````````.8888`7,`````]8`````````````````````7888888``` ```.88888P````````````````]8[`````8`]88888[``````````]88888[][`````]8[`````````````````````]88888[`` ```88888P``````````````````8[`````8`]888888``````````888888[`8`````]8```````````````````````]88888`` ``]88888```````````````````8[`````8`]888888``````````888888°][`````]8````````````````````````78888[` ``88888````````````````````8[`````][`888888`````````]888888`J[`````]8`````````````````````````88888` `]8888°````````````````````8[``````'8_`'"88``````````88"°._8°``````J8`````````````````````````]8888` `8888P`````````````````````8L`````````""88"``````````788""`````````8P``````````````````````````8888[ ]8888``````````````````````]8,``````````````__````_,``````````````.8[``````````````````````````]888[ ]888[```````````````````````"8888_,````````'88[``88P`````````._J88P°```````````````````````````]8888 J888°```````````````````````````'"88°```````'8```'P````````"88"`````````````````````````````````8888 8888`````````````````````````````]8`````````````````````````'8[`````````````````````````````````8888 888[``````````````````````.J888_`'8L```J[``````.,``````J[```88`.J888L```````````````````````````8888 888[`````````````````````.88°`'"[`'888888``][``][``8[``88888P`]8"°`'8[``````````````````````````888[ 888[`````````````````````]8°````[```'"``8``]L``][``8```8``"```8`````8[`````````````````````````]888[ 888[``````````````````````8,```]````````8[``8``][``8``]8``````][````8L,````````````````````````]888` 888[`````````````````````J8L```"88_,````][``7``][``8``JP```_88P°````'788,``````````````````````8888` ]888```````````````````.88°```````'"8L_`]8888L_JLJJ8J88L_88"```````````78[````````````````````]888[` ]888```````````````````]8`````.,``````'"888P"78888""888P°````````_8`````8P````````````````````8888`` `888[```````````````````8L,```/'78_,``````"78L,``_88"°```````_J8"°'L``.JP````````````````````J888[`` `7888````````````````````'"88"`````"88_,``````"7P"```````.J8P°``````"8P°````````````````````]8888``` `]888[````````````````````````````````'"88_``````````._88"`````````````````````````````````.8888[``` ``8888,````````````````````````````````_88"``````````'"8_,````````````````````````````````.8888P```` ``]8888```````````````````___```````_8P"``````.J8L,``````'78_`````JP8L_``````````````````.88888````` ```8888L````````````````J8°``[``._8"``````._88P°`'"88L,``````"7L,][````7L```````````````.88888°````` ```'8888L``````````````8P````]JP"``````_88P"`````````'788_,``````"°`````8[`````````````J88888°`````` ````]8888L`````````````8[``````````_J88"`````````````````"788_,```````.J8°````````````J88888°``````` `````788888,```````````88L,````.J88P°```````__``````````````'"88L,```]8P````````````J888888°```````` ``````788888,```````````"88[````8```````````""``````````````````P````]8```````````.8888888`````````` ```````788888L````````````88````],``````````88``J888L``````````][````.8`````````.J888888P``````````` ````````7888888L``````````]88__88```````````88`]8[`""```````````8L_`.J8```````.J8888888°```````````` `````````]8888888L`````````7888"````````````88`]8,`.,````````````"8888°`````.J8888888P`````````````` ``````````'88888888L````````````````````````88`'88_88`````````````````````J88888888P```````````````` ````````````"888888888_````````````````````.88`````````````````````````_888888888P`````````````````` ``````````````78888888888_````````````````]88[`````````````````````_J8888888888"```````````````````` ````````````````7888888888888_,```````````````````````````````._888888888888P°`````````````````````` ``````````````````'788888888888888L__,````````````````.___888888888888888P°````````````````````````` `````````````````````"888888888888888888888888888888888888888888888888"````````````````````````````` ````````````````````````'"888888888888888888888888888888888888888P"````````````````````````````````` `````````````````````````````""88888888888888888888888888888""`````````````````````````````````````` ```````````````````````````````````"""78888888888888P"""````````````````````````````````````````````
Su uso tambien es valido en imagenes complejas como fotografias, aunque el resultado puede variar.
4 comentarios:
gracias excelente programa e informacion espero los siguientes tipos gracias
y parta gusrdar la imagen no tiene un menu
Hola, antes que nada muchas gracias por esta información. Tengo una duda por que el Color negro y blanco se manejan como negativos?. Saludos
disculpa mouse ¿ q clase de la api de java me recomiendas para transformar una imagen rectangular a una imagen polar en blanco y negro.
Publicar un comentario