Imagen a Texto
Imagen a Texto
Todos hemos visto alguna vez el ART ASCII, en la cual pasan una imagen a su representacion en texto. Hoy les traigo una aplicacion de este tipo.
Hay varias formas de lograr el mismo objetivo, esta es solo una forma, no es la mas eficiente, no es la mas bonita, ni comoda, es solo una forma mas.
Imagenes


Codigo
Clase Principal
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import javax.swing.*;
public class Principal extends JFrame{
PanelMovimiento pm;
JScrollPane scp;
public Principal(){
pm=new PanelMovimiento();
pm.setPreferredSize(new Dimension(1600,600));
scp=new JScrollPane(pm);
scp.setAutoscrolls(false);
scp.setWheelScrollingEnabled(false);
scp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
//scp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
add(scp);
JMenuBar barra=new JMenuBar();
JMenu archivo=new JMenu("Archivo");
JMenuItem cargarImagen=new JMenuItem("Cargar Imagen");
cargarImagen.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
JFileChooser selector=new JFileChooser();
int r=selector.showOpenDialog(null);
if(r==JFileChooser.APPROVE_OPTION){
try {
ImageIcon img=new ImageIcon(selector.getSelectedFile().toURL());
pm.Inicializar(Principal.this, img);
pm.updateUI();
Principal.this.repaint();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
JMenuItem salir=new JMenuItem("Salir");
salir.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
archivo.add(cargarImagen);
archivo.add(salir);
barra.add(archivo);
setJMenuBar(barra);
}
public static void main(String arg[]){
Principal p=new Principal();
p.setVisible(true);
p.setBounds(0, 0, 820, 660);
p.setLocationRelativeTo(null);
p.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Clase PanelMovimiento
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class PanelMovimiento extends JPanel{
Principal p;
PanelImagen pi;
PanelTexto pt;
int x=0;
int y=0;
ImageIcon img;
public PanelMovimiento(){
setLayout(new FlowLayout(FlowLayout.CENTER));
}
public void Inicializar(Principal prin, ImageIcon imga){
p=prin;
img=imga;
pi=new PanelImagen(this, img);
pt=new PanelTexto(this);
add(pi);
add(pt);
}
}
Clase PanelImagen
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class PanelImagen extends JPanel{
ImageIcon img;
PanelMovimiento pm;
public PanelImagen(PanelMovimiento panelm, ImageIcon imagen){
pm=panelm;
img=imagen;
setLayout(new BorderLayout());
setPreferredSize(new Dimension(790, 590));
add(new JLabel(img), BorderLayout.CENTER);
}
}
Clase PanelTexto
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class PanelTexto extends JPanel{
JTextArea texto;
PanelMovimiento pm;
int umbral = 127;
int negro = -16777216;
int blanco = -1;
String txt = "";
private String[] caracteres = {" ","'","*",",",".","\"","_","[","]","J","7","*","L","*","/","" + (char) 92};
private Color color;
public PanelTexto(PanelMovimiento panelm){
pm=panelm;
setLayout(new BorderLayout());
setPreferredSize(new Dimension(790, 590));
texto=new JTextArea();
texto.setFont(new Font("Lucida Console",Font.PLAIN, 8));
texto.setWrapStyleWord(true);
texto.setLineWrap(true);
texto.setAutoscrolls(true);
BufferedImage bi=toBufferedImage(panelm.img.getImage());
texto.setText(ConvertToAscii(ABlancoYNegro(bi)));
add(texto, BorderLayout.CENTER);
}
public String ConvertToAscii(BufferedImage foto){
BufferedImage f = foto;
int x = f.getWidth();
int y = f.getHeight();
for(int j=0;j<y ;j=j+3 ){
for(int i=0;i<x;i=i+2 ){
if( (i < (x-1)) && (j < (y-1)) ){
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 ){
txt = txt + caracteres[0];
continue;
}
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 ){
txt = txt + caracteres[1];
continue;
}
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 ){
txt = txt + caracteres[2];
continue;
}
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 ){
txt = txt + caracteres[3];
continue;
}
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 ){
txt = txt + caracteres[4];
continue;
}
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 ){
txt = txt + caracteres[5];
continue;
}
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 ){
txt = txt + caracteres[6];
continue;
}
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 ){
txt = txt + caracteres[7];
continue;
}
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 ){
txt = txt + caracteres[8];
continue;
}
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 ){
txt = txt + caracteres[9];
continue;
}
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 ){
txt = txt + caracteres[10];
continue;
}
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 ){
txt = txt + caracteres[11];
continue;
}
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 ){
txt = txt + caracteres[12];
continue;
}
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 ){
txt = txt + caracteres[13];
continue;
}
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 ){
txt = txt + caracteres[14];
continue;
}
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 ){
txt = txt + caracteres[15];
continue;
}
}
}
txt = txt + "\n";
}
return txt;
}
private BufferedImage ABlancoYNegro(BufferedImage f){
int r,g,b;
BufferedImage bn = new BufferedImage(f.getWidth(),f.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
for(int i=0;i<f.getWidth();i++){
for(int j=0;j<f.getHeight();j++){
color = new Color(f.getRGB(i, j));
r = color.getRed();
g = color.getGreen();
b = color.getBlue();
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;
}
public BufferedImage toBufferedImage(Image image) {
image = new ImageIcon(image).getImage();
BufferedImage bufferedImage = new BufferedImage(image.getWidth(null),image.getHeight(null),BufferedImage.TYPE_INT_RGB );
Graphics g = bufferedImage.createGraphics();
g.drawImage(image,0,0,null);
g.dispose();
return( bufferedImage );
}
}
Descargar
Podrán descargar el proyecto directamente desde aquí. Se incluyen los archivos fuentes, las imágenes, y el JAR.
Recuerden visitar en Java Zone.
- roger1345's blog
- Inicie sesión o regístrese para enviar comentarios



Comentarios
Exelente
O.o brillante idea XD
y mas genial aun poderlo hacer en codigo
por cierto gracias, el codigo
por cierto gracias, el codigo me sirvio de guia para una cosa que queria hacer con una imagen XD
Wow, like a boss.. Simple e
Wow, like a boss.. jajaj
Simple e ingenioso
+1
roger1345 +1 :D
Que chido que lo compartes con la comunidad. Siempre pones cosas muy interesantes. Dan ganas de echarle una manita de gato jejeje.
De donde eres mi estimado, estaria bien verte en alguna reunion de las que se arman en el D.F.
Bueno pues saludos y gracias por compartir tu código.